如何停止MediaPlayer的其他方法打 [英] How to stop the mediaplayer playing from other methods
问题描述
我用这setOnClickListener()我使用了一个媒体播放器,这是局部声明在我的Android App.Here方法的1个内。
这样我也有两个方法,所有使用媒体播放器。此外,我宣布一个全球性的媒体播放器和放大器;在我的onCreate()。
I am using this setOnClickListener() inside an one of the method in my Android App.Here I have used A mediaPlayer, which is declared locally. Like this I also have two more methods which all uses mediaplayer. Also I have declared a global Mediaplayer & used it in various places of my onCreate().
public void setOnClickListenerWithMedia(ImageView iv,final int drawable,final int sound) {
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopAllSoundsAndClearMemory();
switchCases();
iv_gone();
fullscreenImage.setVisibility(View.VISIBLE);
fullscreenImage.setImageResource(drawable);
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), sound);
mediaPlayer.start();
}
});
}
我的问题是,如果我点击任何其他的方法,我必须停止MediaPlayer的。对于全局声明MediaPlayer对象(MP)。我可以直接使用,
My problem is if I click on any other method, I have to stop the MediaPlayer. For Globally declared MediaPlayer Object(mp.). I can directly use,
if(mp!=null&&mp.isPlaying()){
mp.stop();
}
和我可以阻止它。但我也想停止所有方法的声音。如何可能?
and I can stop it. But I also want to stop the sound from all the methods. How is it possible?
P.S: - >如果我在所有的方法都使用MP,它不播放声音和放大器;他说,以创建静态的MediaPlayer。
P.S: -> If I use mp in all the methods , it is not playing the sound & saying to create static mediaPlayer.
感谢您。
推荐答案
在创建新的播放器将其分配给全球每一次的MediaPlayer
实例。
Every time when you are creating new player assign it to Global MediaPlayer
instance.
即
申报的MediaPlayer类似这样
declare mediaPlayer like this
MediaPlayer mp;
然后在你的的onClick
或其它其他方法使用这样
And then in your onClick
or in other other methods use like this
和检查是否的MediaPlayer
已经存在与否
And check whether MediaPlayer
already exist or not
f(mp!=null&&mp.isPlaying()){
mp.stop();
mp.release();
}
mp=MediaPlayer.create(getApplicationContext(), sound);
mp.start();
这篇关于如何停止MediaPlayer的其他方法打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!