Android Media Player没有停止,当我单击播放按钮多次播放时

问题描述 投票:0回答:1

当我一次又一次单击播放按钮时,它会同时播放多次。我想停止多个播放。这是代码:

媒体播放器和按钮的对象


MediaPlayer mPlayer;
Button playbtn;
Button stopbtn;

按钮单击事件监听器以播放音频


playbtn = (Button) findViewById(R.id.play);
playbtn.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

                Uri myUri1 = Uri.parse("file:///sdcard/Birds/parrot.mp3");
                mPlayer = new MediaPlayer();
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

                try {

                    mPlayer.setDataSource(getApplicationContext(), myUri1);
                } catch (IllegalArgumentException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (SecurityException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IllegalStateException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    mPlayer.prepare();
                } catch (IllegalStateException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                }
                mPlayer.start();




按钮单击事件监听器以停止音频


stopbtn = (Button) findViewById(R.id.stop);
stopbtn.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(mPlayer!=null && mPlayer.isPlaying()){
            mPlayer.stop();
        }
    }
});

任何帮助。谢谢!

android android-mediaplayer
1个回答
0
投票

[每次点击都会创建一个新玩家,但仅保留对最后一个玩家的引用,因此解决方案是将玩家保留在列表中并停止所有玩家。

private List<MediaPlayer> players = new ArrayList<>()



stopbtn = (Button) findViewById(R.id.stop);
stopbtn.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

        for(player in players){
            if(player!=null && player.isPlaying()){
                mPlayer.stop();
            }
        }
        players.clear();
    }
});

另一种解决方案是,仅使用播放器的一个实例。将播放器初始化移到外部,单击为:

playbtn = (Button) findViewById(R.id.play);

Uri myUri1 = Uri.parse("file:///sdcard/Birds/parrot.mp3");
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

try {

    mPlayer.setDataSource(getApplicationContext(), myUri1);
} catch (IllegalArgumentException e) {
    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
    e.printStackTrace();
}


try {
    mPlayer.prepare();
} catch (IllegalStateException e) {
    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
playbtn.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
            if(!mPlayer.isPlaying())
                mPlayer.start();
© www.soinside.com 2019 - 2024. All rights reserved.