【问题标题】:Android Media Player does not stop. When I click on a play button, it plays multiple timesAndroid 媒体播放器不会停止。当我单击播放按钮时,它会播放多次
【发布时间】:2020-04-29 10:11:04
【问题描述】:

当我一次又一次地点击播放按钮时,它会同时播放多次。我想停止多次播放。代码如下:

媒体播放器和按钮的对象


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】:

    它会在每次播放点击时创建一个新播放器,但只保留对最后一个播放器的引用,因此解决方案是将播放器保留在列表中并停止所有播放器。

    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();
    

    【讨论】:

    • 谢谢@Pavneet_Singh。我已经按照你的第二个建议做了,但这是同样的问题。
    • @Tahmid 也将 prepare 移到外面并尝试更新的代码
    • 再次感谢@Pavneet_Singh。现在它工作正常。
    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多