录制音频时进度

时间:2014-04-21 09:06:58

标签: android audio

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

我用这段代码录制声音:

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

3 个答案:

答案 0 :(得分:5)

使用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完成此操作。

答案 1 :(得分: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);


}

}

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

答案 2 :(得分:0)

希望此链接可以帮助您。

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

由于