Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaRecord failed to start Android 7.1.1 emulator

Briefly, I cannot get MediaRecorder#record to work on Android 7.1.1 emulator and don't understand why. Here are the further details:

Environment: Operating System: Mac OS X El Capitan version 10.11.6

IDE: AndroidStudio version 2.2.3

Problem: I have written a sample app to demonstrate the issue. This app consists of a button with an OnClickListener which will initialize and configure MediaRecorder instance, prepare and start recording. After enabling the RECORD_AUDIO permission in settings, the code below runs correctly on the Android 7.0 API 24 Emulator (x86_64 Graphics: Hardware) downloaded by and used by the AVD manager, but yields following error on Android 7.1.1 API 25 (with Google APIs) Emulator (x86_64 Graphics: Hardware):

Error only on Android 7.1.1 API 25 java.lang.RuntimeException: start failed. at android.media.MediaRecorder.start(Native Method)

Question Does anyone know what the issue is? Is there problem with my setup that does not cater for Android 7.1.1? This has been replicated on 2 Macs so far.

Code:

MainActivity.java:

package com.abstractx1.testrecord;

import android.media.MediaRecorder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button recordButton = (Button) findViewById(R.id.recordButton);

        recordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String path = getFilesDir().getAbsolutePath() + "/" + "AudioRecording.3gp";

                MediaRecorder mediaRecorder = new MediaRecorder();
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
                mediaRecorder.setOutputFile(path);

                try {
                    mediaRecorder.prepare();
                    mediaRecorder.start();
                } catch (Exception e) {
                    Log.e("DEBUG-MainActivity", "Error", e);
                    Log.e("DEBUG-MainActivity",     Log.getStackTraceString(e));
                }

                Toast.makeText(MainActivity.this, "Recording started", Toast.LENGTH_LONG).show();
            }
        });
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.abstractx1.testrecord.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:text="Record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/recordButton" />
</RelativeLayout>

AndroidMainfest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abstractx1.testrecord">

    <uses-feature
        android:name="android.hardware.microphone"
        android:required="false" />

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Thank you, and I apologize in advance if this has been answered elsewhere. I have searched for few days without success. Also, if there is ambiguity/lack of information then I will expand and answer questions.

This is such a basic thing that I believe it must be an issue with the emulator.

Can someone please help me with this?

like image 358
abstractx1 Avatar asked Jan 13 '17 17:01

abstractx1


People also ask

Why is my Android emulator not working?

If the emulator fails to start for you, check to see that you have adequate free disk space. Because many security and antivirus software packages work by monitoring every read and write operation, use of such software can decrease performance of tools like the Android Emulator.

What does “failed to start the emulator” mean?

When players try to open the emulator, it shows a message that “Failed to start the emulator, the emulator was not properly closed. Please restart the emulator or computer and try again later”.

How to fix “failed to start the emulator” error in GTA 5?

To solve this error completely, you have to re-login into the game again each time you open a new window. If this failed to fix your issue, you may proceed for the next potential fix. Moreover, that will surely solve the “Failed to Start the Emulator” error.

Why is my Android emulator running slow on Windows 10?

If you are running Android Emulator on Windows, check to see if you have installed Windows Updates KB4013429 and KB4015217 . Some users reported improved Android Emulator performance after uninstalling these updates. Users also reported improved Emulator performance after installing Windows Update KB4015438.


2 Answers

I have encounter similar issue due to not using Environment so media player can not find the path to use to store the audio hence it works on some devices and not in others: https://github.com/alkathirikhalid/android-uni-foundation

I have also reviewed your code I have detected possible errors or at least recommendations

  • File location / path, you should use Environment.getExternalStorageDirectory().getAbsolutePath()... Let the OS give you the location as different devices have different naming conventions
  • Uses feature required is false, then you should have checking to see if devices have mic or not before calling Media Player
  • Uses Permission Write External Storage You are intending to store the recorded audio to store and perhaps use it / play later
  • Finally The emulator is not supported as described in Android documentation "Note: Currently, Media Recorder does not work on the emulator." https://developer.android.com/reference/android/media/MediaRecorder.html

It is highly recommended to use actual devices when dealing with multimedia, locations and other device peripherals such as sensors.

Cheers.

like image 123
Al-Kathiri Khalid Avatar answered Oct 01 '22 17:10

Al-Kathiri Khalid


MediaRecorder is not currently supported on the emulator. This is explicitly stated at the end of the introductory paragraph of the documentation. See the Android MediaRecorder documentation.

like image 26
Hod Avatar answered Oct 01 '22 17:10

Hod