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?