如何知道MediaRecorder是否处于运行状态?

时间:2010-08-11 13:16:06

标签: android audio-recording mediarecorder

我编写了一个代码,用于使用MediaRecorder录制通话对话的音频。

如何知道MediaRecorder是否处于运行状态,以停止录制。 喜欢

boolean running;
MediaRecorder mr;
//what should i assign to running?        
if(running){
   mr.stop()
}

上面的代码只是一个例子..如果您不理解我的问题,请告诉我..我会用实际代码清楚解释..

我想知道的是“MediaRecorder在哪个州?” - >记录/释放/制备/初始/等。

2 个答案:

答案 0 :(得分:8)

您无法直接获取州,请参阅http://code.google.com/p/android/issues/detail?id=800

上的开放增强请求

当媒体播放器达到某个状态时,您需要在侦听器中手动设置变量,以便记住当前状态。

这也是这个讨论: http://www.mail-archive.com/android-developers@googlegroups.com/msg35320.html

答案 1 :(得分:0)

   String flag = "0";


         public void audioRecordStart(){
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
               try {
                    if (flag.equals("1")){
                       // It means recorder is already on recording state.
                    }
                    else{
                       myAudioRecorder.prepare();
                       myAudioRecorder.start();
                       flag = "1";
                    }
                } catch (IllegalStateException ise) {
                    // make something ...
                } catch (IOException ioe) {
                    // make something
                }
                Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
            }
            else {
                getAudioPermission();
            }
        }

        public void audioRecordStop() {
            if (flag.equals("0")){
               // It means recorder is already stopped state.
            }
            else {
                myAudioRecorder.stop();
                myAudioRecorder.release();
                myAudioRecorder = null;
                flag = "0";
                Toast.makeText(getApplicationContext(), "Audio Recorder successfully", Toast.LENGTH_LONG).show();
            }
        }