使用 AudioRecord 录制音频时的停止按钮

发布于 2024-10-12 00:58:40 字数 1039 浏览 3 评论 0原文

目前我有这个网站的代码(链接文本)工作得很好。在该网站的示例中,它开始录制 10 秒,然后立即反向播放音频。我已经修改了代码,以便在按下按钮时开始录制,但只能让它录制 10 秒,然后保存该文件。我希望能够通过按一个按钮开始录制,然后按不同的按钮停止录制。我有一个想法,它可能是线程对象的 wait() 方法的中断,但不知道如何实现它。我的代码如下:

public void onClickRecord(View v){
        text.setText("Recording");
        Thread thread = new Thread(new Runnable() {
            public void run() {
                record();
            }
        });

        thread.start();

        synchronized(this) {
        try {
            wait(10000); //This is the 10 second waiting period while recording
        } 
        catch (InterruptedException e) {
        }
        }

        isRecording = false;

        try {
            thread.join();
        } catch (InterruptedException e) {
        }

    }
    public void onClickStop(View v){
        //Here is what needs to be implemented to stop the recording.
    }

有相当多的代码,所以我只发布了我认为相关的部分。如果需要更多,请询问。

谢谢。

At the moment I have the code from this site (link text) working great. In the example on the site it starts the recording for 10 seconds and then immediately plays the audio back in reverse. I have modified the code to start recording when a button is pressed, but can only get it to record for the 10 seconds and then save that file. I want to be able to start the recording by pressing a button and then stop the recording on a different button press. I have an idea that it could be an interrupt to the wait() method of the thread object but have no idea how to implement this. My code is as follows:

public void onClickRecord(View v){
        text.setText("Recording");
        Thread thread = new Thread(new Runnable() {
            public void run() {
                record();
            }
        });

        thread.start();

        synchronized(this) {
        try {
            wait(10000); //This is the 10 second waiting period while recording
        } 
        catch (InterruptedException e) {
        }
        }

        isRecording = false;

        try {
            thread.join();
        } catch (InterruptedException e) {
        }

    }
    public void onClickStop(View v){
        //Here is what needs to be implemented to stop the recording.
    }

There is quite a bit of code so I have only posted the bits I think are relevant. If any more is needed just ask.

Thanks.

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

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

发布评论

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

评论(1

瀞厅☆埖开 2024-10-19 00:58:40

尝试 AudioRecord 类!
我认为这应该有帮助。

另请参阅此处的 AudioCapture
http://developer.android.com/guide/topics/media/index.html

try the AudioRecord Class !
I think that should be helpful.

Also look at AudioCapture here
http://developer.android.com/guide/topics/media/index.html

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