Android:带定时器的录音

标签 android avaudiorecorder

我正在创建媒体记录器演示应用程序。为此,我需要两个按钮启动和停止。
我想在单击开始按钮时显示计时器并想在停止按钮上停止。

有什么想法吗?

谢谢

最佳答案

简单的解决方案可以基于这些类:

public static class TimeCounter{
    private long startTime;

    public TimeCounter(){
        startTime = new Date().getTime();
    }

    public long countTime(){
        return new Date().getTime() - startTime;
    }
}

public class TimeCounterTask extends AsyncTask<TimeCounter, Long, Void>{

    @Override
    protected Void doInBackground(TimeCounter... params) {
        TimeCounter tc = params[0];
        while(true){
            publishProgress(new Long(tc.countTime()));
            try{
                Thread.sleep(500);
            } catch(InterruptedException e){
                break;
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Long... values) {
        super.onProgressUpdate(values);
        long millis = values[0].longValue();
        int minutes = (int) ((millis / 1000) / 60);
        int seconds = (int) ((millis / 1000) % 60);
        String s = String.format("%d min, %d sec", 
                minutes, seconds);
        timerText.setText(s);
    }

}
  • 在启动按钮处理程序中创建新的 TimerCounterTask 并执行这个新的 TimerCounter() 参数
  • 在停止按钮处理程序中停止当前任务
  • timerText是显示定时器的TextView

关于Android:带定时器的录音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4977110/

相关文章:

swift - 通过 AVMetaDataItem 写入 ID3 标签

iphone - iPhone 的录音格式?

android - BaseGameActivity.runOnUpdateThread() 与 Entity.registerUpdateHandler()

android - 如何获取android中运行的app的包名

android - 如何检测从您的 Android 设备调用的电话是已接听还是已拒绝?

avaudioplayer - AVAudiorecorder 和 AVAudioPlayer 并不总是播放声音

swift - 调用 AVAudioRecorder 的初始化程序 - Swift 2.0

android - 在 RecyclerView 的 ItemAnimator 上禁用 onChange 动画

android - 远程配置备选方案 : Store UI specific parameters

ios - AVAudioRecorder swift 2