我正在尝试开发一个简单的Android音频录音机。一切都很好,它也可以在Android设备上正常运行。好像我可以开始录制,但是当我想要停止它时会抛出IllegalStateException。我找不到错误。这是代码:
public class VoiceRecorder {
MediaRecorder recorder= new MediaRecorder();
static Context cont;
public void startRecord(Context context) throws IllegalStateException, IOException{
cont = context;
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(cont.getFilesDir()+"/recordings.3gp");
recorder.prepare();
recorder.start();
}
public void stopRecording(Context context) {
cont = context;
recorder.stop();
recorder.release();
File file = new File (cont.getFilesDir()+"/recordings.3gp");
UploadFile.uploadFile("recordings.3gp", file);
recorder = null;
}
}
我想用以下方式触发:
VoiceRecorder vr = new VoiceRecorder();
vr.startRecord(续);
vr.stopRecording(续);
调用start时Logcat说:(应该没问题)
09-06 22:56:42.830: D/AudioHardwareMSM72XX(123): audpre_index = 0, tx_iir_index = 0
09-06 22:56:42.840: D/HTC Acoustic(123): msm72xx_enable_audpre: 0x0000
09-06 22:56:42.850: I/AudioHardwareMSM72XX(123): Routing audio to Speakerphone
09-06 22:56:42.850: D/HTC Acoustic(123): msm72xx_enable_audpp: 0x0001
09-06 22:56:42.850: I/AudioHardwareMSM72XX(123): Routing audio to Speakerphone
09-06 22:56:42.860: D/HTC Acoustic(123): msm72xx_enable_audpp: 0x0001
09-06 22:56:42.870: D/AudioFlinger(123): setParameters(): io 3, keyvalue routing=262144;vr_mode=0, tid 156, calling tid 123
09-06 22:56:42.870: I/AudioHardwareMSM72XX(123): Routing audio to Speakerphone
09-06 22:56:42.880: D/AudioHardwareMSM72XX(123): audpre_index = 0, tx_iir_index = 0
09-06 22:56:42.880: D/HTC Acoustic(123): msm72xx_enable_audpre: 0x0000
09-06 22:56:42.880: I/AudioHardwareMSM72XX(123): do input routing device 40000
09-06 22:56:42.880: I/AudioHardwareMSM72XX(123): Routing audio to Speakerphone
09-06 22:56:42.890: D/HTC Acoustic(123): msm72xx_enable_audpp: 0x0001
但是当我打电话给停止时:
09-06 22:59:52.440: E/MediaRecorder(1069): stop called in an invalid state: 1
09-06 22:59:52.440: W/System.err(1069): java.lang.IllegalStateException
09-06 22:59:52.460: W/System.err(1069): at android.media.MediaRecorder.stop(Native Method)
09-06 22:59:52.460: W/System.err(1069): at de.spyapp.VoiceRecorder.stopRecording(VoiceRecorder.java:33)
09-06 22:59:52.460: W/System.err(1069): at de.spyapp.CheckCMD.checkCMD(CheckCMD.java:30)
09-06 22:59:52.460: W/System.err(1069): at de.spyapp.AppActivity$2.run(AppActivity.java:44)
09-06 22:59:52.460: W/System.err(1069): at java.lang.Thread.run(Thread.java:1096)
答案 0 :(得分:4)
问题不在于停止,而在于开始。它没有正确启动,这就是为什么你以后不能停止它。
检查几件事情: a)你添加了persmission android.permission.RECORD_AUDIO
b)你写的SD卡(我不确定它的要求,但我相信我写入内部存储器有问题)。您需要获得写入SD卡的权限。c)另外,尝试设置onErrorListener http://developer.android.com/reference/android/media/MediaPlayer.html#setOnErrorListener(android.media.MediaPlayer.OnErrorListener)
d)尝试在任何其他呼叫之前重置()MediaPlayer 我建议阅读这篇文章 http://developer.android.com/reference/android/media/MediaPlayer.html#Valid_and_Invalid_States Android的MediaRecorder状态是噩梦。
答案 1 :(得分:2)
当你从无效状态调用stop()时,看起来会抛出错误。
录音机的活动周期似乎也表明你需要准备()它才能开始录音 - 你这样做吗?除非记录器至少准备好,否则stop()不是有效的命令。
如果你是,可能需要一段时间让录音机实际上处于有效状态,然后再调用stop()。你在start()之后立刻称之为 ,所以也许会发生一些事情。