我想创建一个全球性的MediaPlayer,将让玩无论哪个显示布局 [英] I want to create a global MediaPlayer that will keep playing no matter which layout is displayed

查看:194
本文介绍了我想创建一个全球性的MediaPlayer,将让玩无论哪个显示布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相反在我的菜单类创建一个,我想创建菜单类,我可以打电话让声音播放无论哪个活动开始了全球性的外面。我是一个菜鸟到Java,但我已经搜索并不能找到一个解决方案。这里是我的菜单活动。正如你可以看到我的MediaPlayer SND是本地所以当的onPause方法被调用它就会被暂停。

 公共类菜单扩展活动{MediaPlayer的SND;
切换按钮btnSound;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.menu);
    SND = MediaPlayer.create(getApplicationContext(),R.raw.bgmusic);
    snd.start();
    btnSound =(切换按钮)findViewById(R.id.tglSound);@覆盖
保护无效的onPause(){
    super.onPause();
    snd.pause();
}


解决方案

实现您的要求,最好的办法是使用服务。而应用程序是intialised可以启动服务。里面的服务启动媒体播放器,并开始播放。所以evenif你导航到下一个或其他活动,媒体将继续播放。你也可以给属性服务何时开始停止广告。暂停或回采媒体播放器你可以停止该服务,以使mediplayer也将停止播放的Isted。希望这链接将帮助你肯定的。

Instead of creating one in my menu class, I'd like to create a global one outside of the menu class that I can call to keep the sound playing no matter which activity is started. I'm a noob to java, but I have searched and can't find a solution. Here is my "menu" activity. As you can see my MediaPlayer snd is local so it gets paused when the onPause method gets called.

    public class Menu extends Activity{

MediaPlayer snd;
ToggleButton btnSound;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
    snd = MediaPlayer.create(getApplicationContext(), R.raw.bgmusic);
    snd.start();
    btnSound = (ToggleButton) findViewById(R.id.tglSound);

@Override
protected void onPause() {
    super.onPause();
    snd.pause();
}

解决方案

The best way to implement your requirement is using a service. You can start a service while the app is intialised. Inside the service start a mediaplayer and start playing. So evenif you are navigating to next or other activities the media will keep playing. You can also give property to the service when to start ad stop. Isted of pausing or stoping mediaplayer you can just stop the service so that the mediplayer will also stop playing. Hope this link will help you for sure.

这篇关于我想创建一个全球性的MediaPlayer,将让玩无论哪个显示布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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