13

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

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

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

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

我只想知道“MediaRecorder 处于哪个状态?” -> 录制/发布/准备/初始/等..

4

2 回答 2

11

您无法直接获取状态,请参阅http://code.google.com/p/android/issues/detail?id=800处的开放增强请求

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

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

于 2010-08-11T14:46:52.577 回答
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();
            }
        }
于 2020-04-12T11:54:43.673 回答