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

发布于 2024-09-13 17:14:03 字数 334 浏览 4 评论 0原文

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

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

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

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

我想知道的是“MediaRecorder处于哪种状态?” ->录音/发布/准备/初始/等等..

I wrote a code to record audio of call conversation using MediaRecorder.

how can i know whether a MediaRecorder is in running state or not, to stop the recording.
like

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

Above code is just an example.. If you do not understand my question, please tell me.. i will explain clearly with actual code..

What all i want to know is "In which state the MediaRecorder is?" -> recording/released/prepared/initial/etc..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偷得浮生 2024-09-20 17:14:03

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

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

还有这个讨论:
http://www.mail-archive.com/[电子邮件受保护]/msg35320.html

You cannot get the state directly, see the open enhancement request at http://code.google.com/p/android/issues/detail?id=800

You need to set a variable manually in the listeners when the mediaplayer reaches a certain state in order to remember the current state.

Also this this discussion:
http://www.mail-archive.com/[email protected]/msg35320.html

长伴 2024-09-20 17:14:03
   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();
            }
        }
   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();
            }
        }
2024-09-20 17:14:03
const mediaRecorder = new MediaRecorder(...)
mediaRecorder.start();
console.log(mediaRecorder.state);
// Will return "recording"
// Other possible states are "inactive" and "paused"
if (mediaRecorder.state != "inactive") {
  mediaRecorder.stop();
}

参考:
https://developer.mozilla.org/en-US/文档/Web/API/MediaRecorder/状态

const mediaRecorder = new MediaRecorder(...)
mediaRecorder.start();
console.log(mediaRecorder.state);
// Will return "recording"
// Other possible states are "inactive" and "paused"
if (mediaRecorder.state != "inactive") {
  mediaRecorder.stop();
}

Reference:
https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/state

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文