In short, I cannot get MediaRecorder # to work on Android 7.1.1 emulator and I don’t understand why. Here are the details:
Environment: Operating system: Mac OS X El Capitan version 10.11.6
IDE: AndroidStudio 2.2.3 Version
Problem: I wrote an example application to demonstrate the problem. This application consists of a button with OnClickListener, which will initialize and configure the MediaRecorder instance, prepare and start recording. After enabling the RECORD_AUDIO permission in the settings, the code below works correctly on Android 7.0 API 24 Emulator (x86_64 Graphics: Hardware), downloaded and used by the AVD manager, but gives the following error in the Android 7.1.1 API 25 interface (with the Google API API): ( x86_64 Graphics: Hardware):
Error only on Android 7.1.1 API 25
java.lang.RuntimeException: startup failed. on android.media.MediaRecorder.start (native method)
Question
Does anyone know what the problem is? Are there any problems with my setup that does not support Android 7.1.1? So far, this has been replicated on 2 Mac computers.
The 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 was answered elsewhere. I searched for several days without success. In addition, if there is ambiguity / lack of information, I will expand and answer questions.
This is such a basic thing, which, I believe, should be a problem with the emulator.
Can someone please help me with this?