使用Android应用地图时暂停的MediaPlayer [英] Pause MediaPlayer when using a Map in android app
问题描述
我建立使用地图约40声音的音板。
<一href=\"http://stackoverflow.com/questions/5543361/imagebutton-soundboard-android-app/5543557#5543557\">($p$pvious螺纹)
有谁知道暂停重新启动媒体播放器的好方法,如果应用程序移动到后台(来电或类似的东西)?我仍然在这个非常新,因此它可能是一些超级简单。由于任何人谁可以提供帮助。
地图=新的
的HashMap();
map.put(R.id.button1,R.raw.sound1);
map.put(R.id.button2,R.raw.sound2);
...
块引用>然后迭代:
有关(Map.Entry的条目:map.entrySet()){
最后MediaPlayer的声音= MediaPlayer.create(entry.getValue());Button按钮=(的ImageButton)findViewById(entry.getKey());button.setOnClickListener(新View.OnClickListener(){ @覆盖 公共无效的onClick(视图v){ sound.start(); }});
}
块引用>解决方案我假设你不想玩听起来都在一起,所以你可以声明MediaPlayer对象外循环,然后你也不需要知道哪些文件正被播放。您可以使用
sound.pause();
sound.stop();
或声音。复位();
(取决于以preform你想要的动作),我建议你看的 Android的文档,看看如何正确地执行它。如果你想为每个文件的我怀疑创建的MediaPlayer你想的,保存mediaplayers在图/表和暂停/时
的onPause环路重新设置()
。I am building a soundboard with about 40 sounds using a map. (Previous thread)
Does anyone know a good way to pause restart Mediaplayer if the app is moved to the background (incoming call or anything like that)? I'm still very new at this so it is probably something super simple. Thanks anyone who can help.
Map map = new
HashMap();
map.put(R.id.button1, R.raw.sound1);
map.put(R.id.button2, R.raw.sound2);
...
and then iterate:
for (Map.Entry entry : map.entrySet()) {
final MediaPlayer sound = MediaPlayer.create(entry.getValue()); Button button = (ImageButton) findViewById(entry.getKey()); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sound.start(); } });
}
解决方案I assume you don't want to play all sounds together, so you can declare the mediaplayer object outside the loop and then you don't need to know which file is being played. You can use
sound.pause();
sound.stop();
orsound.reset();
(depends on the action you want to preform) I suggest you look at the android docs to see how to implement it properly.If you want to create mediaplayer for each file which I doubt you want, save the mediaplayers in a map / list and pause / reset them in loop during
onPause()
.这篇关于使用Android应用地图时暂停的MediaPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!