How to Manage Audio Volumes Sanely in My Android App

How can I manage audio volumes sanely in my Android app?

Got another suggestion via Google Groups that is the platform integrated solution I was looking for and works fine:

Please don't handle the volume keys
yourself - it is almost impossible to
guarantee that you won't break the
behavior of the volume keys.

Call this API in your onCreate():

setVolumeControlStream(AudioManager.STREAM_MUSIC);

This tells the AudioManager that when
your application has focus, the volume
keys should adjust music volume.

Volume Control in android application

There was another question from a long time ago that asked the same thing. Essentially the answer is: don't override the onKeyDown and onKeyUp buttons. It's much better to simply use this one line setVolumeControlStream(AudioManager.STREAM_MUSIC); in your onCreate() method. That tells the OS that the volume buttons should affect the "media" volume when your application is visible, and that's the volume it uses for your application.

As for controlling the Media volume no matter what app is visible, I'm not sure that can be done - or if it could, whether that would be a good thing.

Suppressing system audio volume to play notification audio from inside app

I got the answer to this question in the following link:

http://developer.android.com/training/managing-audio/audio-focus.html

We need to request for audio focus (transient/permanent) and then can play the app audio:

AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Request audio focus for playback
int result = am.requestAudioFocus(afChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// Start playback.
Log.i(TAG, "...audiofocus granted....");
MediaPlayer mPlay = MediaPlayer.create(this, R.raw.sound);
mPlay.start();
}

OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
// Pause playback
Log.i(TAG, "....audiofocus loss transient in listener......");
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// Resume playback
Log.i(TAG, "....audiofocus gain in listener......");
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
Log.i(TAG, "....audiofocus loss in listener......");
//am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
am.abandonAudioFocus(afChangeListener);
// Stop playback
}
}
};

Hope it helps someone.

controlling volume in MediaPlayer

Check to make sure you're not returning true every time onKeyDown is called. If that's the case then you're telling android that you'll handle all onKeyDown events, so android might be ignoring the volume key.

How to set type to my audio so it will be treated like a call?

In the end, I was missing:

  1. Permission - MODIFY_AUDIO_SETTINGS
  2. Setting AudioManager mode to MODE_IN_COMMUNICATION

I'm surprised it wasn't in tutorials, maybe it was changed in later SDK.

Issue with volume buttons in android app

Call this within your activities onCreate().

    setVolumeControlStream (AudioManager.STREAM_MUSIC);

I'm looking to make a custom volume monitor

I assume you wanted more information than just how to make a Toast. So here are few other pointers.

To access the volume level use AudioManager:

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);

Catching the volume buttons is simple enough:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyUp(keyCode, event);
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
// Do something if currentVolume is low enough
// return true;
}
// Check if Up is pushed too
return false;
}

And a Toast is pretty easy too:

Toast.makeText(context, "Message", Toast.LENGTH_LONG).show();

If you ever want your app / service to do more with the current volume, you may realize that this isn't as straight forward as it sounds...

This method tells the system which audio stream to adjust with the volume buttons while your app is active:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

Read the documentation on this and you get a glimpse of how unpredictable tracking the volume yourself can be. Here's a question on the same subject: How can I manage audio volumes sanely in my Android app?



Related Topics



Leave a reply



Submit