使用计时器计数器播放录制的音频

时间:2018-11-27 15:40:35

标签: android android-studio jave

我正在创建一个带有计时器计数器的记录器应用程序,该计时器可以正常运行,包括开始记录,停止记录和停止播放。

我的问题仅在于开始播放,当按下开始播放按钮时,计时器从0开始,但是当音频文件停止时我无法停止计时器。

这是开始播放代码,请帮助。我非常感激的任何帮助

playAudio.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) throws IllegalArgumentException,
                SecurityException, IllegalStateException {

            stopRecord.setEnabled(false);
            startRecord.setEnabled(false);
            stopAudio.setEnabled(true);
            mediaPlayer = new MediaPlayer();
            try {
                mediaPlayer.setDataSource(AudioSavePathInDevice);
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (!running) {
                myChronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
                 myChronometer.start();
                running = true;

                // myChronometer.stop();
            }
            mediaPlayer.start();



            Toast.makeText(MainActivity.this, "Recording Playing",
                    Toast.LENGTH_LONG).show();
        }
    });

先生,这是停止音频的代码

stopAudio.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (running) {
                myChronometer.stop();
                myChronometer.setBase(SystemClock.elapsedRealtime());
                pauseOffset = 0;
                pauseOffset = SystemClock.elapsedRealtime() - myChronometer.getBase();
                running = false;
            }

            stopRecord.setEnabled(false);
            startRecord.setEnabled(true);
            stopAudio.setEnabled(false);
            playAudio.setEnabled(true);


            if(mediaPlayer != null){
                mediaPlayer.stop();
                mediaPlayer.release();
             //   MediaRecorderReady();

                //resetmymyChronometer
                myChronometer.setBase(SystemClock.elapsedRealtime());
                pauseOffset = 0;

            }
        }
    });

这是停止记录的代码

stopRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (running) {
                myChronometer.stop();
                myChronometer.setBase(SystemClock.elapsedRealtime());
                pauseOffset = 0;
                pauseOffset = SystemClock.elapsedRealtime() - myChronometer.getBase();
                running = false;
            }
            mediaRecorder.stop();
            stopRecord.setEnabled(false);
            playAudio.setEnabled(true);
            startRecord.setEnabled(true);
            stopAudio.setEnabled(false);

            Toast.makeText(MainActivity.this, "Recording Completed",
                    Toast.LENGTH_LONG).show();
        }
    });

MediaRecorderReady方法

public void MediaRecorderReady(){
        //myChronometer start
        if (!running) {

            myChronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
            myChronometer.start();
            running = true;
        //***********************

        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        mediaRecorder.setOutputFile(AudioSavePathInDevice);

    }
}

startRecord侦听器

startRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(checkPermission()) {

                AudioSavePathInDevice =
                        Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +
                                CreateRandomAudioFileName(5) + "AudioRecording.3gp";

                MediaRecorderReady();

                try {
                    startRecord.setEnabled(true);
                    mediaRecorder.prepare();
                    mediaRecorder.start();

                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                startRecord.setEnabled(false);
                stopRecord.setEnabled(true);

                Toast.makeText(MainActivity.this, "Recording started",
                        Toast.LENGTH_LONG).show();

            } else {
                requestPermission();
            }

        }
    });

2 个答案:

答案 0 :(得分:1)

您可以使用OnCompletionListener,例如:

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        // your code to stop record
    }
});

有关更多信息: https://developer.android.com/reference/android/media/MediaPlayer.OnCompletionListener

答案 1 :(得分:0)

myChronometer.stop();

从不叫。

在playAudio的onClick()方法中,您应该设置

running = true