【问题标题】:Progress while recording audio录制音频时的进度
【发布时间】:2015-10-12 20:30:20
【问题描述】:

我正在尝试制作一个进度条,它会在我录制声音时做出反应。我的意思是,当记录打开并且有声音时,我的进度条必须移动。我不知道我应该怎么称呼它,但它经常在音频播放器中使用以显示正在播放音乐。我希望你明白我的意思。

我正在用这段代码录制声音:

                recorder = new MediaRecorder();
                recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);
                recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);             
                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                recorder.setOutputFile(outputfile);

【问题讨论】:

  • 我猜你需要的是SeekBar
  • 或进度条我不知道哪个会更好。
  • SeekBar 有一个限制,如果你正在录制音频,那么你的 seekbar 的限制是多少以及它会录制多长时间

标签: android audio


【解决方案1】:

使用getMaxAmplitude()

多次调用该方法并延迟,你会得到你需要的。

这是一个使用RxJava的实现:

mTimerSubscription = Observable.interval(100, TimeUnit.MILLISECONDS)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(aLong -> {
            // evey 100 milliseconds this method is called
            int amplitude = mRecorder.getMaxAmplitude();
            LogWrapper.d(TAG, "amplitude: " + amplitude + " rms " + (int) Math.sqrt(amplitude));
            mSoundProgressBar.setProgress((int) Math.sqrt(amplitude));
        });

您可以使用Handler.postDelayed 很好地做到这一点。

【讨论】:

    【解决方案2】:
    class Recording extends Activity
    {
    
    Timer timer = new Timer();
    ProgressBar pb;
    Button startRecording;
    Button stop;
    MediaRecorder recorder;
    int MAX_DURATION=60000;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
                setContentView(R.layout.recording)
                pb          =(ProgressBar)findViewById(R.id.progressBar1);
                startRecording  =(Button)findViewById(R.id.startRecording);
                stop        =(Button)findViewById(R.id.stop);
    
                recorder = new MediaRecorder();
                recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);
                recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);             
                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                recorder.setOutputFile(outputfile);
    
    
                recorder.setMaxDuration(MAX_DURATION); //supposing u want to give maximum length of 60 seconds
                pb.setMax(MAX_DURATION); //supposing u want to give maximum length of 60 seconds
                pb.setProgress(0);
                startProgress();
    
                startRecording.setOnClickListener(new View.OnClickListener() {
    
                        @Override
                        public void onClick(View arg0) {
                            stopped=false;
                            recorder.start();
    
                        }
                    });
    
                 stop.setOnClickListener(new View.OnClickListener() {
    
                        @Override
                        public void onClick(View arg0) {
                                stopped=true;
                                pb.setProgress(0); // if u want to reset
                                recorder.stop();
    
    
                        }
                    });
        }
    
    
    void startProgress()
    {
        timer.schedule(new TimerTask() {
    
                        @Override
                        public void run() {
    
                            if(!stopped)  // call ui only when  the progress is not stopped
                            {
                                runOnUiThread(new Runnable() {
    
                                    @Override
                                    public void run() {
                                        try 
                                        {
    
                                                pb.setProgress(pb.getProgress()+1000);
    
                                        } catch (Exception e) {}
    
    
    
                                    }
                                });
                            }
                        }
    
    
    
                }, 1, 1000);
    
    
    }
    
    }
    

    希望对你有所帮助并给你一个想法:)

    【讨论】:

      【解决方案3】:

      希望此链接对您有所帮助。

      AudioRecord recorder; // our recorder, must be initialized first
      short[] buffer; // buffer where we will put captured samples
      DataOutputStream output; // output stream to target file
      boolean isRecording; // indicates if sound is currently being captured
      ProgressBar pb; // our progress bar recieved from layout
      while (isRecording) {
          double sum = 0;
          int readSize = recorder.read(buffer, 0, buffer.length);
          for (int i = 0; i < readSize; i++) {
              output.writeShort(buffer [i]);
              sum += buffer [i] * buffer [i];
          }
          if (readSize > 0) {
              final double amplitude = sum / readSize;
              pb.setProgress((int) Math.sqrt(amplitude));
          }
      }
      

      Displaying Sound Volume in Real-Time While Recording

      谢谢

      【讨论】:

      • 嗨 ravi,虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。请看这里:Why and how are some answers deleted?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      相关资源
      最近更新 更多