0

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?

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
Bobby Hartanto
  • 71
  • 2
  • 13

1 Answers1

0

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

Droidman
  • 11,485
  • 17
  • 93
  • 141
  • sorry, but i don't really understand this code.. :| how and where should i put this code? the first one when i click mute button? then the second one? how can the second code check if it is muted or not? Thanks before for your explanation :D – Bobby Hartanto Jan 06 '14 at 12:37
  • I made further corrections to my example. The second code can check this since `SharedPreferences` are global for your app. Consult this document to understand how it works: http://developer.android.com/reference/android/content/SharedPreferences.html and also this one http://developer.android.com/reference/android/preference/PreferenceManager.html – Droidman Jan 06 '14 at 15:11
  • i think this code is not so useful for my app, because i have build the indicator in the database to know whether if it is mute (klik=1) and it is unmute (klik=2).. The problem is whenever i mute the app (the option is only in MainActivity.java as the main menu), and then i go to the layout (Question.java) with MediaPlayer which is played, then i go back to the main menu and choose unmute, and i go to the Question.java, the Media Player is not played anymore because it is still disabled.. i think there is no error in my code, but it still can't be unmuted.. – Bobby Hartanto Jan 06 '14 at 17:53
  • i have found this [http://stackoverflow.com/questions/7908962/setstreammute-never-unmutes] and it is said that it is a bug.. but i think there should be a way out to solve this problem.. i can't used setVolume(0,0) because the media player is played in another layout, so if i use setVolume(0,0) it will be forced close.. any suggestion to solve this problem, @Droidman ? :D – Bobby Hartanto Jan 06 '14 at 17:58
  • there's absolutely no point in using database just to check if the mute option was activated. Try to do it using SharedPreferences (and don't forget to update the isMuted boolean each time the settings are changed). I also have a mute button in one of my apps (in the settings menu) and I do it exactly the same way I stated above. Works fine – Droidman Jan 06 '14 at 18:09
  • I have used your code, but it still cannot be unmuted.. i have no problem to mute the app, but the problem is when i want to unmute the app.. @Droidman – Bobby Hartanto Jan 07 '14 at 01:19
  • I'm using database just to know if the button has been clicked or not, as i have explained above.. – Bobby Hartanto Jan 07 '14 at 01:23
  • if you have unmuted the app, use my 1st code snippet BUT set the boolean to false. Yes, and I tell you that it's ridiculous to use a database for this purpose. – Droidman Jan 07 '14 at 01:42
  • it still cannot be unmuted when i have muted the app.. still don't know why :| what is the function of the second code? To check whether it is mute or not? Then is it the same with the code isMuted(mContext)==true ? – Bobby Hartanto Jan 07 '14 at 03:48
  • yes, the 2nd code checks whether this boolean indicator is set to true. You should start the MediaPlayer if that method returns false. If it always returns true, you did not implement the code to set isMuted to false while clicking the "unmute" button – Droidman Jan 07 '14 at 14:48
  • i have edited the code in my question, and it still didn't work although i have used isMuted code.. any correction? – Bobby Hartanto Jan 07 '14 at 15:27
  • 1). You do not need the "==" operator to check for a boolean value 2). You call the isMuted() method BEFORE setting this value in SharedPreferences, so far I can see. You need to only call this method in you Activity which plays the sound – Droidman Jan 07 '14 at 17:04
  • okay.. i have tried this code with media player played in my MainActivity which contains the button to control mute and unmute. So when i click mute, it mutes, and when i click unmute, it unmutes. So far no problem with my codes above. But the problem when i mute the sound, and i go to the next layout (with button "start" to start game), and when i go back to the MainActivity, and i want to try to unmute, it still unmutes.. Then what does it mean that i call isMuted() method before setting the value in SharedPreferences? Is this method just used to check whether the boolean is true or false? – Bobby Hartanto Jan 07 '14 at 17:37
  • I made a sample project with 2 Activities demonstrating how it works. The 2nd Activity plays a sound only when you have unmuted the sound in the 1st Activity. Take a look at this and I guess you will better understand how to achieve what you want (.zip format, the link is active for 5 days): http://files.mail.ru/13B13ED8A74C4FFF86A60FB4353283D1 – Droidman Jan 07 '14 at 20:35
  • i got confused about this logic, if(prefs.getBoolean("isMuted", false)), then it mean that the Media Player is off.. Why? And then why did we use editor.putBoolean("isMuted", false); if the "isMuted" is false? :D – Bobby Hartanto Jan 08 '14 at 12:27
  • I guess you ain't getting how the `prefs.get*Datatype()*` method works. The second parameter is the default value which will be used in case the `Preference` does NOT exist. So `prefs.getBoolean(key, false)` doesn't mean that it will return false. False will only be returned if you have set it to false OR if the key is invalid (preference does not exist). The logic of player is (generally expressed): IF NOT isMuted THEN PLAY ELSE DO NOTHING. The same as: IF isMuted THEN DO NOTHING ELSE PLAY – Droidman Jan 08 '14 at 12:39
  • 2nd note: you don't need to overwrite the variable with the same value of course, might be my mistake. Though it won't change anything – Droidman Jan 08 '14 at 12:43
  • hmm.. if(prefs.getBoolean("isMuted", false)), and false means if there is no prefs or isMuted is false.. is it true? then i want to know, i have implemented your code in my app, but my app didn't run the if(prefs.getBoolean("isMuted", false)), but the else condition, i mean my default sound setting is on, but in your app the default setting of the sound is off, although i used your code.. – Bobby Hartanto Jan 08 '14 at 13:36
  • when i change the function, if(prefs.getBoolean("isMuted", false)) then "isMuted",true, it cannot be mute.. but when i used your code, if(prefs.getBoolean("isMuted", false)) then isMuted,false, it can be mute and unmute, nothing went wrong.. but i really haven't understand the logic of this code.. when i run my app in the first time, the sound is on although i have used the same code as yours.. :D – Bobby Hartanto Jan 08 '14 at 14:01
  • then i want to know, when i run your app in the first time, the button is red and the sound is off, when i click the next, your textview displays that isMuted is true.. in Setting layout, the red button is displayed, so it means that prefs is false or there is no prefs, but in the next activity, why it can be changed to true value (because the textview displays "true"? – Bobby Hartanto Jan 08 '14 at 14:11
  • in my sample code, the button is red when the sound is muted. I made this just to visualize. Sure you can change tha value of the preference in the next activity, but do it BEFORE making any calls to the player – Droidman Jan 08 '14 at 17:51
  • why it is red, because in the first time i ran this app, the prefs doesn't exist yet, so it executed the if(prefs.getBoolean("isMuted", false)) one.. then when i click next, i think you haven't changed the prefs to true, but why does it can be true in Boolean.toString(isMuted())? – Bobby Hartanto Jan 09 '14 at 02:54