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?