如何使用MediaPlayer播放

如何使用MediaPlayer播放

本文介绍了如何使用MediaPlayer播放/停止/再次播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击时我有一个按钮,它会播放音频,并且在播放时,我可以将其暂停然后再次播放,依此类推.我有一个摇动事件,我想在摇动时播放音频并在再次摇动设备时重播它(首先停止音频,然后调用stopAudio?)

I have a button when clicked it plays an audio, and while it is playing I can pause it and replay it again and so forth. I have a shake event, where I want to play the audio on shake and replay it when device is shaken again (by first stopping the audio, calling stopAudio?)

我有以下代码:

llBtn = (LinearLayout) findViewById(R.id.button);
llBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Toast.makeText(MainActivity.this, "LL WAS CLICKED", Toast.LENGTH_SHORT).show();
        //IF AUDIO IS NOT PLAYING... PLAY AUDIO
        if (tvPS.getText() == "Stop Phrase!") {
            StopAudio();
        }
        else {
            PlayAudio();
        }
    }
});
//the same button is clicked to stop, it is currently setting to Pause.
public void StopAudio() {
        mediaPlayer.pause();
        Toast.makeText(MainActivity.this, "STOPPED", Toast.LENGTH_SHORT).show();
        tvPS.setText("Play Phrase!");
    }

public void PlayAudio() {
    //stopPlaying(mediaPlayer);
    tvPS.setText("Stop Phrase!");

    inResId = getResources().getIdentifier("play" , "raw", getPackageName());

    mediaPlayer = MediaPlayer.create(getBaseContext(), inResId);
    mediaPlayer.seekTo(0);
    mediaPlayer.start();
    mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            //Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_SHORT).show();
            tvPS.setText("Play Phrase!");
            mediaPlayer.pause();
        }
    });
}
...
public void onShake() {
    // Do stuff!
    Toast.makeText(getBaseContext(), "Motion detected",
            Toast.LENGTH_SHORT).show();

    if (mediaPlayer == null) {
        PlayAudio();
    }
}

如何修改上面的代码以处理播放,如果停在中间,则停止播放,然后再次播放.

How can I modify the code above to handle Play, and then stop if stopped in the middle and then able to replay it again.

按钮单击有效,但是我每次都创建一个新实例,而不是回收.摇动设备后,它会播放两次音频,而不是一次.

The button click works but I am creating a new instance each time and not recycling. When the device is shaken, it plays the audio twice instead of just once.

推荐答案

首先在上下文的onCreate中而不是在某些侦听器中创建MediaPlayer,并且不要忘记在完成后将其释放,因为它会消耗很多资源,

First of all create the MediaPlayer in onCreate of the context and not in some listener, and don't forget to release it when done, since it consumes a lot of resources,

mediaPlayer.release();
mediaPlayer = null;

接下来,在完成歌曲后,使用stop()功能而不是pause()停止音频.

Next thing, stop the audio using the stop() function instead of the pause() on completion of the song.

如果您正在使用服务/活动来播放视频,请在开始使用之前创建MediaPlayer实例(可能在onCreate中).

If you are using a service/activity to play a video create a MediaPlayer instance before it's usage begins, likely in onCreate.

以后使用该实例播放/暂停/停止播放媒体.

Later use that instance to play/pause/stop the media.

在服务/活动的onStop中释放MediaPlayer实例.

In the onStop of the Service/Activity release the MediaPlayer instance.

更新playMusic()函数以使用刚刚创建的MediaPlayer实例,并且在侦听器中也使用stop()而不是pause()

Update your playMusic() function to use the MediaPlayer instance you just created, also in the listener use stop() instead of pause()

您可以使用此:

String path = getExternalFilesDir(null).toString()+"/";

mMediaPlayer.setDataSource(path + mediafile);

链接:更改使用现有MediaPlayer进行音频播放的数据源?

这篇关于如何使用MediaPlayer播放/停止/再次播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 23:25