在比赛结束时做点什么

时间:2014-05-05 18:15:23

标签: android audio android-mediaplayer android-audiorecord

我有一个按钮,按下后会开始播放录制的音频。它也是图标的变化。我想在播放结束时再次更改图标。有人可以举个例子吗?谢谢:))

这是我的音频课程:

public class Audio {
    private MediaPlayer mPlayer;
    private MediaRecorder mRecorder;

    public String path;

    public void setPath(String path) {
        this.path = path;
    }

    public void stopPlayRecorded() {
        if (mPlayer != null) {
            mPlayer.stop();
        }
    }

    public void playRecorded() throws IllegalArgumentException,
            SecurityException, IllegalStateException, IOException {

        ditchMediaPlayer();

        File f = new File(path);

        if(!recordingExists(f)) {
            return;
        }

        mPlayer = new MediaPlayer();
        mPlayer.setDataSource(path);
        mPlayer.prepare();
        mPlayer.start();
    }

    private void ditchMediaPlayer() {
        if (mPlayer != null) {
            try {
                mPlayer.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public void stopRecording() {
        if (mRecorder != null) {
            mRecorder.stop();
            ditchMediaRecorder();
        }

    }

    public void beginRecording() throws IllegalStateException, IOException {
        File f = new File(path.substring(0, path.lastIndexOf("/")));

        if (!recordingExists(f)) {
            f.mkdirs();
        }

        ditchMediaRecorder();
        File outFile = new File(path);

        Log.d("R_PATH", path);

        if (outFile.exists()) {
            outFile.delete();
        }

        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile(path);

        mRecorder.prepare();
        mRecorder.start();
    }

    public boolean recordingExists(File f) {
        if (f.exists())
            return true;
        else
            return false;
    }

    private void ditchMediaRecorder() {
        if (mRecorder != null) {
            mRecorder.release();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

是的,你可以用OnCompletionListener

来做到这一点
    mPlayer.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            // do something when the end of a media source 
            // has been reached during playback.

        }
    });