java.lang.RuntimeException: setAudioSource falhou
Eu sou novo no desenvolvimento android. Estou apenas tentando gravar um áudio com o teste do android studio (2.1.1) com o dispositivo 6.0.1 Marshmallow.
public class MainActivity extends AppCompatActivity {
Button start, stop;
public MediaRecorder recorder = null;
public String fileextn = ".mp4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start_button);
stop = (Button) findViewById(R.id.stop_button);
start.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_button:
startRecord();
case R.id.stop_button:
stoprecord();
}
}
}
);
}
public void startRecord() {
recorder = new MediaRecorder();
recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setOutputFile(getFilePath());
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void stoprecord() {
if (recorder != null) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
}
private String getFilePath() {
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, "MediaRecorderSample");
if (!file.exists())
file.mkdirs();
return (file.getAbsolutePath() + "/" + fileextn);
}
}
A propósito, incluí a permissão de uso no arquivo manifestext.xml.
É isso que recebo no Android Monitor:
05-18 11:08:36.576 10414-10414/com.example.gk.audiocapture E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gk.audiocapture, PID: 10414
java.lang.RuntimeException: stop failed.
at android.media.MediaRecorder.stop(Native Method)
at com.example.gk.audiocapture.MainActivity.stoprecord(MainActivity.java:65)
at com.example.gk.audiocapture.MainActivity$1.onClick(MainActivity.java:35)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Tentei algumas respostas, não encontrei nenhuma.
MY AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gk.audiocapture">
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<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>