2013-01-09 219 views
4

我想用麦克风录制音频并保存音频文件。开始录制工作正常,但是当我尝试停止录制时,模拟器会发出强制关闭错误。堆栈跟踪:停止录制音频时出错

01-09 18:16:59.075: E/AndroidRuntime(831): FATAL EXCEPTION: main 
01-09 18:16:59.075: E/AndroidRuntime(831): java.lang.IllegalStateException 
01-09 18:16:59.075: E/AndroidRuntime(831): at android.media.MediaRecorder.stop(Native Method) 
01-09 18:16:59.075: E/AndroidRuntime(831): at com.example.voice.recorder.MainActivity.StopRecording(MainActivity.java:45) 
01-09 18:16:59.075: E/AndroidRuntime(831): at com.example.voice.recorder.MainActivity$1.onClick(MainActivity.java:76) 
01-09 18:16:59.075: E/AndroidRuntime(831): at android.view.View.performClick(View.java:3511) 
01-09 18:16:59.075: E/AndroidRuntime(831): at android.view.View$PerformClick.run(View.java:14105) 
01-09 18:16:59.075: E/AndroidRuntime(831): at android.os.Handler.handleCallback(Handler.java:605) 
01-09 18:16:59.075: E/AndroidRuntime(831): at android.os.Handler.dispatchMessage(Handler.java:92) 
01-09 18:16:59.075: E/AndroidRuntime(831): at android.os.Looper.loop(Looper.java:137) 
01-09 18:16:59.075: E/AndroidRuntime(831): at android.app.ActivityThread.main(ActivityThread.java:4424) 
01-09 18:16:59.075: E/AndroidRuntime(831): at java.lang.reflect.Method.invokeNative(Native Method) 
01-09 18:16:59.075: E/AndroidRuntime(831): at java.lang.reflect.Method.invoke(Method.java:511) 
01-09 18:16:59.075: E/AndroidRuntime(831): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
01-09 18:16:59.075: E/AndroidRuntime(831): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
01-09 18:16:59.075: E/AndroidRuntime(831): at dalvik.system.NativeStart.main(Native Method) 

它在MediaRecorder.stop()上给出错误; 这是我试图阻止拍摄:

public void StopRecording() throws IOException{ 
    recorder.stop(); 
    recorder.reset(); 
    recorder.release(); 
    recorder = null; 
} 

我如何开始录音:

public class MainActivity extends Activity { 
    MediaRecorder recorder; 
public void StartRecording(){ 
    recorder = new MediaRecorder(); 
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    recorder.setOutputFile("/sdcard/sample.3gp"); 
    try { 
     recorder.prepare(); 
     recorder.start(); 
    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

我如何调用该方法:

  if (!tv.getText().equals("Recording...")){ 
       tv.setText("Recording..."); 
       tv.setTextColor(Color.RED); 
       record.setImageResource(R.drawable.microphone_icon_pressed); 
       StartRecording(); 

      }else{ 
       tv.setText("Click the button to start recording"); 
       record.setImageResource(R.drawable.microphone_icon); 
       tv.setTextColor(Color.BLACK); 
       try { 
        StopRecording(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

我在这2个权限我清单:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.RECORD_AUDIO" /> 

因此,开始录制工作正常,但停止录制不会。有人知道代码有什么问题吗?

由于提前, 西蒙

+0

请不要重复标签的问题。我编辑了它。 – Simon

+0

你能为你的代码添加更多的上下文吗?你如何/在哪里宣布'录音机'? (例如,如果它是'BroadcastReceiver'的非静态成员,则可能会遇到这类问题)。 – Michael

+0

您是否完成了[音频捕捉指南](http://developer.android.com/guide/topics/media/audio-capture.html)中的步骤3,4,5,6? – Stan

回答

0

您的刻录机显然不是处于记录状态。你应该确保它是否成功启动。因为在start()之前调用stop()时发生IllegalStateException。如果它被抛出,则在stop()块中添加RuntimeException,然后删除输出文件。

MediaRecorder.java

/** 
    * Stops recording. Call this after start(). Once recording is stopped, 
    * you will have to configure it again as if it has just been constructed. 
    * Note that a RuntimeException is intentionally thrown to the 
    * application, if no valid audio/video data has been received when stop() 
    * is called. This happens if stop() is called immediately after 
    * start(). The failure lets the application take action accordingly to 
    * clean up the output file (delete the output file, for instance), since 
    * the output file is not properly constructed when this happens. 
    * 
    * @throws IllegalStateException if it is called before start() 
    */ 
    public native void stop() throws IllegalStateException; 

我还建议不要放录音的对象,直到你需要启动和停止连续您的应用程序被关闭。根据以下流程,创建记录器onCreate()/ onResume()并释放onPause/onDestroy()。

enter image description here