Android的:如何阻止其他活动的媒体播放器? [英] Android: How to stop other active media players?

查看:244
本文介绍了Android的:如何阻止其他活动的媒体播放器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个音乐播放器。我的应用程序有2名球员 -

I'm trying to create a music player. My application has 2 players -

(一)播放从内存中的歌曲

(二)起使用URL的在线音乐流

(b) plays online music streams using URLs

我的应用程序的主页/默认的活动为(一)。对于球员的(二)有一个单独的活动。

The home/default activity of my app is (a). For player (b) there is a separate activity.

问题是,当用户presses在活动上的播放按钮(b)播放的URL流,从内存中获取的轨道发挥。

Issue is when user presses the play button in activity (b) to play the URL stream, the track from the memory gets played.

好像MediaPlayer的从活动(一)仍然有效。

It seems like MediaPlayer from activity (a) is still active.

所以,我想:


  • 如果用户presses播放按钮,所有其他的媒体播放器被停止。

  • If user presses the play button, all other media player gets stopped.

有没有什么办法让其他媒体播放器的参考;检查它们是否活跃;如果是,则阻止他们。

Is there any way to get reference of other media players; Check if they are active; If yes, then stop them.

这是一个模糊的角度来说,我认为这可以通过发送广播服务活动(进行一),并进行了功能有哪些会听,并停止播放器。

From a vague perspective, I thought this can be done by sending a broadcast to the Service of activity (a), and make a function there which will listen it and stop the player.

但似乎有点蛮力给我&安培;还我应该怎么获得玩家的引用(一)。

But it seems a bit brute to me & also how should I get the reference of the player in (a).

希望我已经清除我的疑问。

Hope I've cleared my doubt.

请建议。

感谢您

推荐答案

您可以在项目中创建一个类,并调用它MediaPlayerRegistry您存储的每个媒体播放器参考当前正在使用的对象。并随时随地访问他们你的应用程序停止/释放/重放。它看起来是这样的:

You can create a class in your project and call it MediaPlayerRegistry where you store a reference to each mediaPlayer object you are currently using. and access them anywhere in your app to stop / release / replay them. it would look something like this:

public class MediaPlayerRegistry {
      public static List<MediaPlayer> mList = new ArrayList<MediaPlayer>();
}

再从你有你可以调用任何活动:

Then from any activity you have you can call:

MediaPlayerRegistry.mList.put(MediaPlayer mPlayer);

将其添加到您的注册表......当你想要释放​​它停止所有正在运行的对象。

to add it to your registry... and when you want to release it stop any running object.

for ( MediaPlayer player : MediaPlayerRegistry.mList) {
if (player != null && player.isplaying() {
      player.release();
}
}

只是把它当作自己的MediaPlayer对象池

just think of it as your own pool of MediaPlayer objects

这篇关于Android的:如何阻止其他活动的媒体播放器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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