Best way to handle AudioManager in android - 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.

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.

Related

How to start a media player object in a activity, and then use that same object and another activity?

Example:
Activity 1:
main screen.
player = new media player()
player.start() //the sound began
now i have to equalize this same sound in another Activity...
Activity 2:
edition screen
the sound keeps playing and want to stop
example:
player.setVolume(0.0)
player.stop()
thank you
Declare player as public static in screen1
then you can access this media player in screen 2
like screen1
:
public static MediaPlayer player;
player=new MediaPlayer();
=================
===========
write your code
Screen 2 ::-
if you want to use media player in screen 2 use this code ::-
screen1.player.start(); screen1.player.stop();
You must create Service. Service to host the MediaPlayer and have your Activities communicate with the Service to play and stop songs. Don't forget to call release on the MediaPlayer when you are done. Bind the activity to service
For the sample Equalizer sample. The sample is not integrated with Service it just a seperate unit.
Obtain the sessionid of the MediaPlayer and pass it to equalizer.
Usually when we are using MediaPlayer, as playing music in itself doesnt really require to have a graphical interface, we usually use a service because only the sound resulting by the playback is needed.
- Create the mediaplayer in a service
- Send somes request to the Service after binding to it, or even by sending broadcasts to it so that it can play, stop, pause, set volume whatever you want.

Turn off ring tone temporarily

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

Adding mute button to application in android?

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().

How to implement Phonestatelistener in an activity

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);

How to handle Alarm notification in Android?

I'm developing an media player application for Android, for which I need to handle any Alarm notification, and based on that I'll pause my playback. When the Alarm in snoozed or dismissed, I'll then resume the playback.
I googled a lot for the Alarm handling, but what I found was the way to enable Alarm notifications through code, set the intent and then handle it. However, no where could I locate just handling the Alarm notification part. I don't need to set the Alarm on, it could've been set by the user, and I don't need to programmatically. All I need is just handle that notification.
Any ideas on this would be extremely useful?
Thanks,
Asheesh
HI Asheesh Vashishtha,
Correct me on this, but AFAIK whenever any other application even if it is the alarm clock, is activated, your activity will surely go in background. So i guess u can override the OnPause and OnResume functions to put your bit of code. As far as snooze or other things are concerned, they all will result in the Alarm Activity getting destroyed(or paused, don know much about it) and your activity will get resumed. So that wont be a matter of concern for u!
Hope this helps...
AFAIK, there is no way for you to be notified of what the Alarm Clock application does, any more than you get notified about any other third-party alarm clock.
Note that AlarmManager -- what you were probably reading about -- is not the same as the Alarm Clock application.
Sorry!
I ran into a similar situation while developing a media player. My solution was to use the AudioManager's OnAudioFocusChangeListener.
You implement the listener in the class like so
public class VideoPlayerHelper implements AudioManager.OnAudioFocusChangeListener {
Then you override onAudioFocusChange
#Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
//Just fall through by omitting break
case AudioManager.AUDIOFOCUS_LOSS:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
LogUtil.log(LogUtil.DEBUG, TAG, "AUDIOFOCUS_LOSS or AUDIOFOCUS_LOSS_TRANSIENT"); //Custom logging class
if (isPlaying()) {
pause();
mAudioManager.abandonAudioFocus(VideoPlayerHelper.this);
}
break;
case AudioManager.AUDIOFOCUS_GAIN:
LogUtil.log(LogUtil.DEBUG, TAG, "AUDIOFOCUS_GAIN"); //Custom logging class
break;
default:
break;
}
}
The key here is AudioManager.AUDIOFOCUS_LOSS_TRANSIENT. This was the code the listener kept receiving when the alarm clock would go off (on The Note 5). So I simply handled AudioManager.AUDIOFOCUS_LOSS_TRANSIENT the same as AudioManager.AUDIOFOCUS_LOSS by pausing the media player and letting go of the audio focus.
When we setup the media player, I added this line before adding the data source
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
Make sure your code for starting the media player also has this line in it (I have it in the start code and onResume code in case the alarm went off while the app was in the background).
mAudioManager.requestAudioFocus(VideoPlayerHelper.this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
That line helps you get back the audio focus when you hit the play button after dismissing the alarm clock.
You should also let go off audio focus when you're finished with the media player. I put this line of code in the onStop and onDetach methods.
mAudioManager.abandonAudioFocus(VideoPlayerHelper.this);
It's not as much setup as you may think and it allows you to adjust your media player whenever unexpected audio is introduced (such as an alarm clock or timer goes off).

Categories

Resources