安卓:媒体播放器停止/开始播放原始资源 [英] Android: Mediaplayer stop / start playing raw resource

查看:175
本文介绍了安卓:媒体播放器停止/开始播放原始资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在起第一次preSS,停在第二preSS或其他声音按钮preSS。如果我让播放声音通过我可以重新启动或玩另一种声音在一个单一preSS。

我的目标是为同一个按钮的第二preSS停止它,而是一个新的按钮的preSS刚刚开始新的声音,而不是新的按钮的第一preSS停止旧声,第二个preSS启动新的声音。

我明白为什么它做什么,现在这样,但我不知道如何使它工作就是我想要的。

 公共无效playSound(INT输入){
    如果(熔点=空&放大器;!&放大器; mp.isPlaying()){
         mp.stop();
         mp.reset();
    }其他{
         如果(熔点!= NULL){
             mp.reset();
             mp.release();
         }
         熔点= MediaPlayer.create(Soundboard.this,输入);
         mp.start();
    }
}


解决方案

code的此块:

 如果(熔点!= NULL){
    mp.reset();
    mp.release();
}

将永远不会被执行。 MP 只能是在这一点上,因为它是在其他块,如果(熔点!= NULL)测试。这表明,在你的思想关于使用这种方法的一个缺陷。

如果声音已通过发挥你preSS按钮,那么这个code将执行:

  mp.release();
熔点=无效;

由于 MP 不是其他块不执行,没有新的声音播放。当按钮为pressed第二次, MP 现在其他块被执行,创建一个新的的MediaPlayer 和播放声音。

It now plays on first press, stops on second press or press of another sound button. If I let a sound play through I can restart it or play another sound on a single press.

My goal is for the second press of the same button to stop it, but the press of a new button to just start the new sound instead of first press of new button stopping the old sound, second press starting the new sound.

I can see why it does what it does now but am not sure how to make it work the way I want.

public void playSound(int input){
    if (mp!=null && mp.isPlaying()) {
         mp.stop();
         mp.reset();
    } else{
         if (mp!=null){
             mp.reset();
             mp.release();
         }
         mp = MediaPlayer.create(Soundboard.this, input);
         mp.start();
    }
}

解决方案

This block of code:

if (mp!=null){
    mp.reset();
    mp.release();
}

will never be executed. mp can only be null at this point, as it is in the else block of an if (mp != null) test. This suggests that there is a flaw in your thinking regarding the use of this method.

If a sound has played through and you press the button, then this code will execute:

mp.release();
mp = null;

Since mp was not null, the else block doesn't execute and no new sound is played. When the button is pressed a second time, mp is now null and the else block gets executed, creating a new MediaPlayer and playing the sound.

这篇关于安卓:媒体播放器停止/开始播放原始资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆