I my game application am using musics. I need mute and unmute button to put mob i silent mode.
Music runs in every activity like playing game and checking score.But mute button is added in menu activity.
I googled didn't get any exact result.
Use audio manager and set volume
AudioManager audioManager = (AudioManager) getSystemService(ctx.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
I'd choose to set the volume to 0 (mute) or 1 (unmute) with MediaPlayer.setVolume(float volumeLeft, float volumeRight). Also preserves the users volume settings. Trigger by Button is easily achieved with onClickListener().
Related
I am using isMusicActive function to detect the media player is running (note that this is default media app, such as com.sec.android.app.music). Now, I want to determine the pause state. Is it possible to get that state? Thank all
AudioManager mAudioManager = (AudioManager) getSystemService(getApplicationContext().AUDIO_SERVICE);
if (mAudioManager.isMusicActive())
I am using Android 5.0 and 6.0. I want to check my default media player (calls from intent) is running/playing in background. Currently, I used the function
AudioManager mAudioManager = (AudioManager) getSystemService(getApplicationContext().AUDIO_SERVICE);
if(mAudioManager.isMusicActive()){
Log.d("TAG","Running");
}
else{
Log.d("TAG","Not running");
}
However, it cannot know if the media player is in background but it is in stop/pause mode (looks like when you scroll down the notification bar and click the pause button in the media player). Do we have any function which can determine the media player is opened in background, but it is in stop/pause mode?
I'm wondering, what is the best way to handle the AudioManager in Android, for media.
As you know, AudiManager manage different type of audio, like music or ringtone.
If I have an applicaiton playing sound effects and vibrating, with 3 activity, how do I have to handle this class ?
-Do I have to set the volume on the onCreate method in each activity ?
Then use it
audioManager.setVolumeControlStream(AudioManager.STREAM_MUSIC);
-Or do I have to make a static reference, and use it each time ?
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
-Or is these wrong, and I didn't understand how it works ?
To tell the truth, I'm having problem handling this im my 3 activities : in the first 2 activities, I have sound effect, and in the 3rd one, only vibration. In the first activity, the sound is in "Ringtone mode", and the other two "Media mode". So the sound volume is different which is bad.
In order to use the AudioManager you have to have an instance of it (in every Activity or Service you want to use it) so:
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
then you can use it:
audioManager.setVolumeControlStream(AudioManager.STREAM_MUSIC);
This doesn't need to be called inside the OnCreate() method but need to be called before you play the sound.
When someone calls, my app will turn off the ringing tone in order not to distract the user. So could you please tell me how to turn off the ringing sound in Android?
EDIT:
It seems I will need to use this one :http://developer.android.com/reference/android/media/Ringtone.html
I think I've found the solution. In case someone else needs it, I copy my solution here:
AudioManager manager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
manager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
There is a manager class called AudioManager which controls the audios and such. And it has a instance method setRingerMode(int mode) which accepts a integer flag.
Once you're done with the silence, you can change the status of the ring tone by passing in this flag RINGER_MODE_NORMAL
I have certain data which i got, when user fill a form in an activity. There is an option Mode where user can select RING or VIBRATE.
So my question is how could i actually implement it in my activity, I see various examples on Telephony manager and phonestatelistener
http://marakana.com/forums/android/examples/62.html.
http://www.tutorialforandroid.com/2009/01/get-phone-state-when-someone-is-calling_22.html
but its hard to implement it in my application,i have data in text form only and dont know how to use this data to switch it from one mode to another. Please tell me in terms of coding example.
phone state listener doesnt determine the ring or vibrate mode - it is for the call status (idle, ring, offhook)
to get the ring/vibrate/silent stau use Audiomanager
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.getRingerMode();// returns AudioManager.RINGER_MODE_VIBRATE or AudioManager.RINGER_MODE_SILENT or AudioManager.RINGER_MODE_NORMAL
// to set ringer mode
am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);