在Android应用程式中使用地图时暂停MediaPlayer [英] Pause MediaPlayer when using a Map in android app

查看:178
本文介绍了在Android应用程式中使用地图时暂停MediaPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用地图构建一个约40个声音的音板。
(上一个主题)



有没有人知道一个好的方法来暂停重新启动Mediaplayer如果应用程序移动到后台(来电或类似的东西)?我还是很新的,所以这可能是一个超级简单的东西。感谢任何可以帮助的人。


地图map = new



HashMap() ;



map.put(R.id.button1,R.raw.sound1);



地图。 put(R.id.button2,R.raw.sound2);



...


然后迭代:

$ (Map.Entry条目:map.entrySet()){

  
$ b final MediaPlayer sound = MediaPlayer.create(entry.getValue());

按钮按钮=(ImageButton)findViewById(entry.getKey());

button.setOnClickListener(new View.OnClickListener(){

@Override

public void onClick(View v){

sound.start();

}

});

}



解决方案

我假设你不想一起播放所有的声音,所以你可以在循环之外声明mediaplayer对象,然后你不需要知道正在播放哪个文件。您可以使用 sound.pause(); sound.stop(); 声音。 reset(); (取决于你想要执行的操作)我建议你看看 android docs ,以了解如何正确实现。



如果要为每个文件创建媒体播放器我怀疑你想要,将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(); or sound.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屋!

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