我試圖將麥克風的音頻記錄保存到文件對象而不是文件。爲什麼?因爲我只需要保存記錄,而應用程序處於活動狀態。這是我嘗試使用從開發人員.android.com「音頻捕獲」教程將音頻記錄保存到文件對象
private Button b1;
private Button b2;
private ProgressBar bar;
private MediaRecorder mRecorder = null;
private MediaPlayer mPlayer = null;
private boolean recording = false;
private boolean playing = false;
public AudioRecordDialog(Context context, final MainActivity main) {
super(context);
setTitle(R.string.setupSound);
setContentView(R.layout.audio_record_dialog_layout);
bar = (ProgressBar) findViewById(R.id.progressBar1);
bar.setAlpha(0);
b1 = (Button) findViewById(R.id.button1);
b1.setText(R.string.startButton);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (recording) {
b1.setText(R.string.startButton);
recording = false;
bar.setAlpha(0);
if (mRecorder != null) {
mRecorder.stop();
mRecorder.release();
}
mRecorder = null;
} else {
b1.setText(R.string.stopButton);
recording = true;
bar.setAlpha(1);
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile("record.3gp");
try {
mRecorder.prepare();
} catch (IOException e) {
Toast toast = Toast.makeText(getContext(),
"Failed Recording!", Toast.LENGTH_SHORT);
toast.show();
}
mRecorder.start();
}
}
});
b2 = (Button) findViewById(R.id.button2);
b2.setText(R.string.playButton);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (playing) {
b2.setText(R.string.playButton);
playing = false;
if (mPlayer != null)
mPlayer.release();
mPlayer = null;
} else {
b2.setText(R.string.pauseButton);
playing = true;
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource("record.3gp");
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Toast toast = Toast.makeText(getContext(),
"Failed Playing soundFile!",
Toast.LENGTH_SHORT);
toast.show();
}
}
}
});
但它不工作,並在單擊記錄按鈕時崩潰。 這裏是logCat。
08-28 02:07:14.144: E/Trace(17588): error opening trace file: Permission denied (13)
08-28 02:07:14.324: D/SensorManager(17588): caller of registerListener:17588,sensor type:3
08-28 02:07:14.410: D/libEGL(17588): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
08-28 02:07:14.425: D/libEGL(17588): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
08-28 02:07:14.425: D/libEGL(17588): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
08-28 02:07:14.511: D/OpenGLRenderer(17588): Enabling debug mode 0
08-28 02:07:17.785: E/MediaRecorder(17588): start called in an invalid state: 4
08-28 02:07:17.785: D/AndroidRuntime(17588): Shutting down VM
08-28 02:07:17.785: W/dalvikvm(17588): threadid=1: thread exiting with uncaught exception (group=0x40ddd2a0)
08-28 02:07:17.793: E/AndroidRuntime(17588): FATAL EXCEPTION: main
08-28 02:07:17.793: E/AndroidRuntime(17588): java.lang.IllegalStateException
08-28 02:07:17.793: E/AndroidRuntime(17588): at android.media.MediaRecorder.start(Native Method)
08-28 02:07:17.793: E/AndroidRuntime(17588): at com.notfall.notruf.AudioRecordDialog$1.onClick(AudioRecordDialog.java:74)
08-28 02:07:17.793: E/AndroidRuntime(17588): at android.view.View.performClick(View.java:4106)
08-28 02:07:17.793: E/AndroidRuntime(17588): at android.view.View$PerformClick.run(View.java:17052)
08-28 02:07:17.793: E/AndroidRuntime(17588): at android.os.Handler.handleCallback(Handler.java:615)
08-28 02:07:17.793: E/AndroidRuntime(17588): at android.os.Handler.dispatchMessage(Handler.java:92)
08-28 02:07:17.793: E/AndroidRuntime(17588): at android.os.Looper.loop(Looper.java:137)
08-28 02:07:17.793: E/AndroidRuntime(17588): at android.app.ActivityThread.main(ActivityThread.java:5118)
08-28 02:07:17.793: E/AndroidRuntime(17588): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 02:07:17.793: E/AndroidRuntime(17588): at java.lang.reflect.Method.invoke(Method.java:511)
08-28 02:07:17.793: E/AndroidRuntime(17588): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
08-28 02:07:17.793: E/AndroidRuntime(17588): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
08-28 02:07:17.793: E/AndroidRuntime(17588): at dalvik.system.NativeStart.main(Native Method)
08-28 02:07:19.191: I/Process(17588): Sending signal. PID: 17588 SIG: 9
它崩潰的
mRecorder.start();
當你打電話你準備通過準備好的傾聽者,當回調被擊中,然後調用start –