0

我有一个简单的类来录制声音并在录制播放文件后播放它:

public class MainActivity extends Activity
{
    private static final String LOG_TAG = "AudioRecordTest";
    private static String mFileName = null;

    private MediaRecorder mRecorder = null;
    private Button startRecord, startPlaying, stopPlaying;
    private MediaPlayer   mPlayer = null;

    private void onRecord(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }
    }

    private void onPlay(boolean start) {
        if (start) {
            startPlaying();
        } else {
            //stopPlaying();
            pausePlaying();
        }
    }

    private void startPlaying() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

    private void pausePlaying(){
        mPlayer.stop();
    }

    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }

    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }



    public MainActivity() {
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/audiorecordtest.3gp";
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.activity_main);

        startPlaying = (Button) findViewById(R.id.buttonStartPlay);
        stopPlaying = (Button) findViewById(R.id.buttonStopPlaying);
        startRecord = (Button) findViewById(R.id.buttonStartRecord);

        startRecord.setOnClickListener(new OnClickListener() {
            boolean mStartRecording = true;
            @Override
            public void onClick(View v) {
                onRecord(mStartRecording);
                if (mStartRecording) {
                    startRecord.setText("Stop recording");
                } else {
                    startRecord.setText("Start recording");
                }
                mStartRecording = !mStartRecording;

            }
        });       

        startPlaying.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onPlay(true);

            }
        });

        stopPlaying.setOnClickListener(new OnClickListener() {
            boolean mStartPlaying = true;
            @Override
            public void onClick(View v) {
                onPlay(false);

            }
        });

    }

    @Override
    public void onPause() {
        super.onPause();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }
}

我知道如何暂停,但我不知道如何再次运行文件,但从暂停文件的那一刻开始。有任何想法吗?

4

2 回答 2

2

改变这个:

private void pausePlaying(){
            mPlayer.stop();
        }

至:

    private void pausePlaying(){
           if(mPlayer.isPlaying()){
                 mPlayer.pause();
             } else {
               mPlayer.start();
             }
     }
于 2013-06-18T11:07:34.860 回答
1

不要使用 .stop() 来暂停。在 MediaPlayer API 中,您可以看到 .pause() 将是最佳选择。如果你重组

    mPlayer.setDataSource(mFileName);
    mPlayer.prepare();

要在您的 onCreate 方法中发生,您可以避免每次都重置它。查看他们的图表http://i.stack.imgur.com/svHrq.gif

于 2013-06-18T11:00:49.967 回答