5

I am trying to develop a simple android audio recorder.Everything builds fine and it also runs fine on the android device. It seems like i can start the recording but when i want to stop it throws a IllegalStateException. I can't find the mistake. Here is the code:

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;
 }
}

I want to trigger it with:

VoiceRecorder vr = new VoiceRecorder();

vr.startRecord(cont);

vr.stopRecording(cont);

when calling start Logcat says: (what should be ok)

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

But when i call stop:

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)
4

2 回答 2

5

问题不在于停止,而在于开始。它无法正确启动,这就是为什么您以后无法停止它的原因。

检查几件事:a)您添加了权限 android.permission.RECORD_AUDIO

b)您写入 SD 卡(我不确定它的要求,但我相信我在写入内部存储器时遇到了问题)。您需要获得写入 SD 卡的权限。

c) 另外,尝试设置 onErrorListener http://developer.android.com/reference/android/media/MediaPlayer.html#setOnErrorListener(android.media.MediaPlayer.OnErrorListener )

d) 在任何其他调用之前尝试 reset() MediaPlayer 我建议阅读这篇文章 http://developer.android.com/reference/android/media/MediaPlayer.html#Valid_and_Invalid_States Android 的 MediaRecorder 状态是噩梦。

于 2012-09-06T21:23:26.807 回答
2

当您从无效状态调用 stop() 时,似乎会引发该错误。

记录器的活动周期似乎也表明您需要在开始记录之前准备()它 - 您在这样做吗?除非至少准备好记录器,否则 stop() 不是有效命令。

如果是这样,那么记录器可能需要一段时间才能真正稳定在有效状态,然后才能稍后调用 stop()。您在 start() 之后立即调用它,所以那里可能正在发生一些事情。

于 2012-09-06T21:15:18.583 回答