android - 如何只录制1分钟的音频?

标签 android audio timer media-player record

在我的android应用中,我只需要录制音频1分钟?有谁能帮助我录制一定时间的音频?我已经编写了用于录制音频的代码,只有在按下停止按钮时它才会停止,但是我需要在1分钟后自动停止录制...

这是我的代码..但是没有用

       try {
             recorder.prepare();

           recorder.start();
         //Declare the timer

           Timer t = new Timer();
           //Set the schedule function and rate
           t.scheduleAtFixedRate(new TimerTask() {

               @Override
               public void run() {

                   runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Toast.makeText(AudioRecordingActivity.this, "record successfullly ",
                                    Toast.LENGTH_SHORT).show();
                    i1=i1+1;
                        }

                    });


                   //Called each time when 1000 milliseconds (1 second) (the period parameter)
               }

           },
           //Set how long before to start calling the TimerTask (in milliseconds)
           0,
           //Set the amount of time between each execution (in milliseconds)
           10*1000);

           if (null != recorder) {
            recorder.stop();
            recorder.reset();
            recorder.release();

            recorder = null;
            Toast.makeText(AudioRecordingActivity.this, "record successfullly",
                    Toast.LENGTH_SHORT).show();

           }

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

最佳答案

试试下面的代码

recorder.start();
   new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
        recorder.stop();
         }
                }, 60000);

关于android - 如何只录制1分钟的音频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21522574/

相关文章:

java - 无法在 android Studio 中解析符号 'R'

android - 在 iOS 中创建 "dashboard"主屏幕

android - Qt安卓编译报错

javascript - 如何在不刷新网页的情况下播放歌曲?

c# - 如何使用定时器等待?

java - 如何暂停我的 Java 程序 2 秒

android - libgdx 中的 Button 是否有 "Hover"选项?

javascript - 如何使用 php 和 html 中的按钮播放数据库中的音频?

python - 特定的 .wav 文件被缩短和扭曲

jquery - 一个简单的 JavaScript 倒数计时器的代码?