Android停止录制失败

时间:2013-06-27 09:26:24

标签: android audio recording

我要开始服务以开始录音。当我试图停止时,它会显示以下消息

06-27 17:21:30.138: E/MediaRecorder(6122): stop called in an invalid state: 0

我不确定其他功能如上传数据到服务器是否会影响结果(未在Asyck Task中实现,只调用方法)

如果我没有捕获运行时异常,有时它可能会显示处理程序异常(完成活动,停止记录)

06-27 17:32:35.988: W/System.err(6818): java.lang.IllegalStateException
06-27 17:32:35.988: W/System.err(6818):     at android.media.MediaRecorder.stop(Native Method)
06-27 17:32:35.988: W/System.err(6818):     at tvbpv.test.EOrderSystem1.RecordService.stopRecording(RecordService.java:109)
06-27 17:32:35.988: W/System.err(6818):     at tvbpv.test.EOrderSystem1.RecordService.onDestroy(RecordService.java:46)
06-27 17:32:35.988: W/System.err(6818):     at android.app.ActivityThread.handleStopService(ActivityThread.java:2401)
06-27 17:32:35.988: W/System.err(6818):     at android.app.ActivityThread.access$2000(ActivityThread.java:127)
06-27 17:32:35.988: W/System.err(6818):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1225)
06-27 17:32:35.988: W/System.err(6818):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 17:32:35.988: W/System.err(6818):     at android.os.Looper.loop(Looper.java:137)
06-27 17:32:35.988: W/System.err(6818):     at android.app.ActivityThread.main(ActivityThread.java:4519)
06-27 17:32:35.988: W/System.err(6818):     at java.lang.reflect.Method.invokeNative(Native Method)
06-27 17:32:35.988: W/System.err(6818):     at java.lang.reflect.Method.invoke(Method.java:511)
06-27 17:32:35.988: W/System.err(6818):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
06-27 17:32:35.988: W/System.err(6818):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
06-27 17:32:35.988: W/System.err(6818):     at dalvik.system.NativeStart.main(Native Method)

开始录制

06-28 09:14:47.646: E/MediaRecorder(3089): start failed: -38
06-28 09:14:47.646: W/System.err(3089): java.lang.IllegalStateException
06-28 09:14:47.646: W/System.err(3089):     at android.media.MediaRecorder.start(Native Method)
06-28 09:14:47.646: W/System.err(3089):     at tvbpv.test.EOrderSystem1.RecordService.startRecording(RecordService.java:87)
06-28 09:14:47.646: W/System.err(3089):     at tvbpv.test.EOrderSystem1.RecordService.onStart(RecordService.java:49)
06-28 09:14:47.646: W/System.err(3089):     at android.app.Service.onStartCommand(Service.java:438)
06-28 09:14:47.646: W/System.err(3089):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2371)
06-28 09:14:47.646: W/System.err(3089):     at android.app.ActivityThread.access$1900(ActivityThread.java:127)
06-28 09:14:47.646: W/System.err(3089):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1222)
06-28 09:14:47.646: W/System.err(3089):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 09:14:47.646: W/System.err(3089):     at android.os.Looper.loop(Looper.java:137)
06-28 09:14:47.646: W/System.err(3089):     at android.app.ActivityThread.main(ActivityThread.java:4519)
06-28 09:14:47.646: W/System.err(3089):     at java.lang.reflect.Method.invokeNative(Native Method)
06-28 09:14:47.646: W/System.err(3089):     at java.lang.reflect.Method.invoke(Method.java:511)
06-28 09:14:47.646: W/System.err(3089):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
06-28 09:14:47.646: W/System.err(3089):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
06-28 09:14:47.646: W/System.err(3089):     at dalvik.system.NativeStart.main(Native Method)

以下是我的代码

RecordService.java


private MediaRecorder recorder;
private File instanceRecord;
private boolean recording= false;
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}



@Override
public void onStart(Intent intent, int startId) {
    startRecording();       
    super.onStart(intent, startId);
}



@Override
public void onDestroy() {
    stopRecording();
    super.onDestroy();
}

private void prepareRecording()
{
    try
    {
        recorder.prepare();
    }
    catch(IllegalStateException e)
    {
        e.printStackTrace();

    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
}

public  void startRecording(){

    try {

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

    Date today = Calendar.getInstance().getTime();    
    Format formatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
    String reportDate = formatter.format(today);


    File instanceRecordDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "TVB_PV_recordings");

    if(!instanceRecordDirectory.exists()){
        instanceRecordDirectory.mkdirs();
    }

    instanceRecord = new File(instanceRecordDirectory.getAbsolutePath() + File.separator + reportDate + "_sales_recording.mp4");
    if(!instanceRecord.exists()){
        instanceRecord.createNewFile();
    }

    recorder.setOutputFile(instanceRecord.getAbsolutePath());        
    prepareRecording();
    recorder.start();
    recording = true;

    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }catch (Exception e){
        e.printStackTrace();
    }
}

public  void stopRecording() {
    if (recorder != null) {       
          try {
            recorder.stop();
          } catch(RuntimeException e) {
              instanceRecord.delete();  //you must delete the outputfile when the recorder stop failed.
          } finally {
            recorder.release();
            recorder = null;
          }
    }                   
}


Other Activity: 

        stopService(new Intent(EOrderSystemSubmit.this, RecordService.class));      

2 个答案:

答案 0 :(得分:9)

http://developer.android.com/reference/android/media/MediaRecorder.html - 非常有吸引力地查看主题的开头部分的图表。我确信,当录音没有开始时你就叫停。

  

停止录制。在start()之后调用它。录制停止后,您必须再次对其进行配置,就像刚刚构建完一样。请注意,RuntimeException是   如果在调用stop()时没有收到有效的音频/视频数据,则故意抛出应用程序。如果在start()之后立即调用stop(),则会发生这种情况。失败让应用程序相应地采取行动来清理输出文件(例如删除输出文件),因为在发生这种情况时输出文件没有正确构造。

     

抛出   如果在start()

之前调用它,则为IllegalStateException

这就是文档所说的内容。

答案 1 :(得分:2)

未初始化刻录机时抛出错误。尝试在数据源初始化完成后开始记录。 例如,如果您正在尝试录制呼叫,请在OFFHOOK uccors时开始录制。