Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to handle AudioManager in android

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.

like image 499
FR073N Avatar asked Oct 23 '25 18:10

FR073N


1 Answers

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.

like image 58
XorOrNor Avatar answered Oct 25 '25 08:10

XorOrNor