如何打开声音开/关在全球范围内的Andr​​oid游戏 [英] How to turn sound on/off globally in Android game

查看:137
本文介绍了如何打开声音开/关在全球范围内的Andr​​oid游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的安卓游戏,我提供的选项菜单中的打开或关闭游戏。我的声音和振动有3个活动,我想,如果我关闭声音或任何震动我的游戏活动它必须关闭其他所有活动,即使我移动到一个活动到另一个活动,如果任何一个活动我再次打开游戏的声音或振动它为ON的所有其他活动游戏.The基本的问题是,我没能做到这一点,我在这混乱。我也是不知道哪种方法我用做that.Please帮助我。
我有3类:
1)帮助器类(MySetting)
2)1级(主类)
3)班2

I am working on Android game and I am providing options menu to "Turn On" or "Turn Off" the sounds and vibration of the game .I have 3 Activity and i want that if i OFF the Sound or Vibrate from any Activity of My Game it has to be OFF in all other Activity even if i move to one activity to another activity and if from any one activity i again turn ON the sound or vibration of the game it has to ON in all the other activity of the game .The basic problem is that I am not able to do that and i am confused in this. i am also not sure that which method i use to do that.Please Help me . i have 3 class: 1)Helper Class(MySetting) 2)class1(main class) 3)class 2

1类(主要code我试图为)

Class 1(main code i try is)

if(MySetting.mp.isPlaying()==true){  
             ms.setSoundON(true);

         }  
         else {  
             ms.loadAudio(this); 
             MySetting.PlaySound();  
             ms.setSoundOFF(false);  
             ms.setSoundON(true);  
         }  
         Button ON=(Button)findViewById(R.id.Start); 
                  ON.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {  
                if(ms.isSoundON()==false){  
                    ms.loadAudio(Class1.this);  
                     MySetting.PlaySound();  
                     ms.setSoundOFF(false);  
                     ms.setSoundON(true);  
                     tv.setText("Sound is Start");  
                     }  
                 else {  

                     ms.setSoundON(true);  
                 } 

            }  

         });  
                  Button OFF=(Button)findViewById(R.id.stop);  
                  OFF.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {  
                MySetting.StopSound();  
                ms.setSoundOFF(true);  
                ms.setSoundON(false);  
                tv.setText("Sound is Stop");  
            }  

         });  
                  Button change=(Button)findViewById(R.id.changeActivity);  
                  change.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {  
                startActivity(new Intent(Class1.this,Class2.class));  

                    }  
                 });    

同为CLASS2 结果
辅助类code是:

SAME FOR CLASS2
HELPER CLASS CODE IS:

static boolean SoundON,SoundOFF;  
static MediaPlayer mp=new MediaPlayer();  
    public void loadAudio(Context context){  
                mp=MediaPlayer.create(context,R.raw.fullmoon);  
}  
    public static void PlaySound(){  
            mp.start();  
            }  
    public boolean setSoundON(boolean on){  
        SoundON=on;  
        return SoundON;  
    }  
    public boolean setSoundOFF(boolean off){  
        SoundOFF=off;  
        return SoundOFF;  
    }  
    public boolean isSoundON(){  
        return SoundON;  
    }  
    public boolean isSoundOFF(){  
        return SoundOFF;  
    }  
    public static void StopSound(){  
        mp.stop();  
        mp.release();         
    }  

**现在我抵达问题是,当我SHIFT从行为到另APP CRASH当时PLZ帮助我,我想在和关闭单击选项菜单的声音**结果
感谢和问候结果
RizN81

**NOW THE PROBLEM I AM GETTING IS WHEN I SHIFT FROM ON ACTIVITY TO ANOTHER APP CRASH AT THAT TIME PLZ HELP ME AND I WANT TO ON AND OFF THE SOUND ON CLICK OF OPTION MENU **
Thanks and Regards
RizN81

推荐答案

我会去一个辛格尔顿类,可以管理您的活动所有的声音。我没有previously在Android中完成的,但在C ++中,我已经在其他的例子看到(的这里和的这里)实际上支持我的想法。

I would go for a Singleton class that could manage all the sounds in your activity. I haven't previously done in Android, but on C++, and I've seen in other examples (here and here) that actually support my idea.

一个辛格尔顿是来自世界各地在应用程序中,更重要的访问,还有每个应用程序只有一个实例,这将让您全局配置任何声音。

A Singleton is accessible from everywhere in your application and more importantly, there's only one instance per application, which will let you configure any sound globally.

什么这些链接和我都在讲的是具有管理每一个声音在你的游戏中一个类可以帮助你:

What those links and me are telling is that having a single class that manages every sound in your game will help you:


  1. 简化的声音通话的(你可以调用从的HashMap (例如)通过 YourSoundManager.getInstance()。播放每一个声音( soundName); YourSoundManager.getInstance()阻止他们停止(soundName); ,或干脆复制粘贴示例上面有速度和声音多一点控制)的由 facading 中的电话和

  2. 控制任何声音,就像你有一个大的调音台,除了它是用什么,但code完成的:)

  1. simplify sound calls (you can call every sound from a HashMap (for example) by YourSoundManager.getInstance().play("soundName"); and stop them by YourSoundManager.getInstance().stop("soundName");, or simply copy-pasting the examples above to have a little more control in speed and sounds) by facading the calls and,
  2. control any sound, just like you had a big mixing console, except it is done using nothing but code :)

这取决于你想要做的静音游戏什么的例子。要么停止所有的声音,或者把自己的音量。 我会去先阻止他们,因为这是最简单的方法。

From those examples it depends on what you want to do to mute your game. Either stopping all the sounds, or putting their volume down. I'd go for stopping them at first, as it's the simplest approach.

编辑:

如果你想停止的声音从活动切换到另一个时,你仍然可以使用单的方法。

If you want to stop a sound when switching from an activity to another one, you can still use the singleton approach.

所以它播放你愿意使用的声音可以覆盖onResume()方法。还要检查这个声音不是由那个可以包含任意播放声音(或用其他更熟悉战术)一个HashMap查找播放。我给你一些code可能不工作,你(因为它依赖于你的最终方式以及如何管理音)

You can override the onResume() method so it plays the sound you are willing to use. Also check that this sound isn't playing by looking up at a HashMap that could contain any playing sound (or use other more familiar tactics). I'll write you some code that may not be working to you (as it depends on your final approach and how you manage sounds)

@Override
public void onResume()
{
    if(! SoundManager.getInstance().isPlaying("thisStageSound") ) //if it's already playing, do not stop it!
    {
        SoundManager.getInstance().stopAllSounds();
        SoundManager.getInstance().playSound("thisStageSound");
    }
    super.onResume();
}

这篇关于如何打开声音开/关在全球范围内的Andr​​oid游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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