mute just the background music of my app

 ̄綄美尐妖づ 提交于 2020-01-03 04:54:47

问题


i build an app, and in main menu i want to create a button to control whether the user wants to use music (unmute) or not (mute) only for this application (not the device) and the background music is played on another layout..

This is my method to call media player (in class Question.java):

public void playSound(int arg)
{
    try
    {
        if(player != null)
        {
            if (player.isPlaying()) 
            {
                player.stop();
                player.reset();
                player.release();
            }
        }
    }
    catch(Exception e)
    {

    }

    if (arg == 2)
    {
        player = MediaPlayer.create(this, R.raw.b);
    }

    if(player != null)
    {
        player.setLooping(true);
        player.start();
    }
}   

And this is the code for the button (in my main menu, MainActivity.java):

public class MainActivity extends Activity
{
  //another code.....
  public String klik;

  protected void onCreate(Bundle savedInstanceState)
  {
    //another code...

    DataAdapter myDbHelper = new DataAdapter(this);
    myDbHelper.createDatabase();       
    myDbHelper.open();      
    Cursor get = myDbHelper.getSound(1);
    klik = Utility.GetColumnValue(get, "klik");
    //to get value of klik on my database

    if(klik.equals("1"))
    {
        setGbrSound(1);
        //set button's background to mute
    }
    else if(klik.equals("2"))
    {
        setGbrSound(2);
        //set button's background to unmute
    }

    myDbHelper.close();

    //another code...

    btnsuara.setOnClickListener(new View.OnClickListener()
    {   
        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub
            if(klik.equals("1"))
            {
                AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE);
                aManager.setStreamMute(AudioManager.STREAM_MUSIC, true);        
                setSound(2); //update klik value in database to klik=2
                setGbrSound(2); //set button's background to unmute
                setSound(3); //change value of String klik in this class from the value of klik in database
            }
            else if (klik.equals("2"))
            {
                AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE);
                aManager.setStreamMute(AudioManager.STREAM_MUSIC, false);

                setSound(1); //update klik value in database to klik=1
                setGbrSound(1); //set button's background to mute
                setSound(3); //change value of String klik in this class from the value of klik in database
            }
        }   
    });
}

if i use this code in MainActivity.java, the function is working properly (i think it is because no media player to be played in this class). When the class Question.java is running, the method playSound is called and the media player is played. When i go back to the MainActivity.java and i choose to mute the music, and i go to the Question.java again, the background music is not played and then the Media volume setting for the device is disabled (not only for my application). Anyone know how to solve this? Thx..

EDIT: i tried to use this code but it is still cannot be unmuted..

    btnsuara.setOnClickListener(new View.OnClickListener()
    {   
        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub
            if(isMuted(mContext)==false)
            {
                AudioManager aManager =(AudioManager)getSystemService(AUDIO_SERVICE);
                aManager.setStreamMute(AudioManager.STREAM_MUSIC, true);        

                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
                Editor editor = prefs.edit(); // get an Editor object
                editor.putBoolean("isMuted", true); // set the mute boolean to true (mute)
                editor.commit();

                setGbrSound(2); //set button's background to unmute/sound on
            }
            else if (isMuted(mContext)==true)
            {   
                AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE);
                aManager.setStreamMute(AudioManager.STREAM_MUSIC, false);

                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
                Editor editor = prefs.edit(); // get an Editor object
                editor.putBoolean("isMuted", false); // set the mute boolean to false (unmute)
                editor.commit();

                setGbrSound(1); //set button's background to mute/sound off
            }
        }   
    });

public static boolean isMuted(Context c)
{
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
     return prefs.getBoolean("isMuted", false); // false is the default value 
}

Any comments?


回答1:


The fastest way:

1). Save the value as boolean or int or whatever in SharedPreferences

2). Check this value from anywhere in your app where you play music

You are done. Ask if you need some sample code

EDIT example:

In your menu Activity, define a Context as a class field (where you have all the other fields declared):

     private Context mContext = this;

1). Then (in your onClick() method), set a boolean like:

   SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
   Editor editor = prefs.edit(); // get an Editor object
   editor.putBoolean("isMuted", true); // set the mute boolean to true
   editor.commit(); // save the changes

2). Perform the check from anywhere in your app by using this method (pass a valid Context). You can define and call this method in the Activity from which you want to check if the "mute" option was activated:

    public static boolean isMuted(Context c){
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
     return prefs.getBoolean("isMuted", false); // false is the default value 
      }

Note that you can overwrite this value by using Editor.commit() method again



来源:https://stackoverflow.com/questions/20947890/mute-just-the-background-music-of-my-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!