35

I have an app that plays intermittent sounds while its activity is open, and the user is always expecting it to make these noises, but unfortunately it is constrained by the music stream volume. The options I have discovered are thus:

  1. Adjust the music stream's volume, possibly deafening the user if they happen to be playing music at the time.
  2. Call MediaPlayer.setVolume() which is ineffective if the music stream's volume is 0.
  3. Handle volume button presses myself which presents two issues: volume button presses adjust the ringer volume unless my audio is playing, and I have been as of yet unable to catch the onKey event for a volume button press using my OnKeyListener. If there were some way to force the adjustment to apply to the music stream while my activity has focus, or to just catch the button presses myself this would work.
  4. Play a silent looping piece of audio in the background to keep the music stream in context for volume adjustments.

I think the 3rd option is probably best since it cedes control of the volume to the user, but if there were some way to just override the system volume in a single mediaplayer instance that would work too.

Luke
  • 18,585
  • 24
  • 87
  • 110

5 Answers5

76

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.

Luke
  • 18,585
  • 24
  • 87
  • 110
  • AGRH! Thanks man, I had exactly same problem (and like idiot spent 2 hours debugging my sound playing code...) – nikib3ro Jan 02 '10 at 19:36
  • 1
    Thanks. It was difficult to find this by googling. Hell, people on the official android development forum were saying to grab the volume keys. I too thought that was a bad idea, so thanks for finding this. – Martin W Oct 31 '10 at 22:18
  • If you are using the MediaPlayer, you can set it like so: mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); – Bart Burg Sep 16 '14 at 08:27
  • thanks its work for foreground only. I want to handle volume of stream_music when app in background. please let me know any solution. – Jatinkumar Patel Apr 14 '16 at 07:19
  • Sorry, it was almost 8 years ago, no idea.. And now that the stackoverflow db has been spammed everywhere its impossible to search for the text I copied from the group without hitting my own question. – Luke Feb 09 '17 at 10:01
2

It is usefull of this API: setVolumeControlStream(AudioManager.STREAM_MUSIC);

Unless this I will spend more time to fix that volume bug.

Jill
  • 21
  • 1
1

If you are using the 'OnKeyDown' method, you can get around this issue by doing the following:

public boolean onKeyDown(int keyCode, KeyEvent event) {    
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                setVolumeControlStream(AudioManager.STREAM_MUSIC);}}
Ben Simms
  • 41
  • 1
  • 5
0
   var audioManager:AudioManager?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        registerReceiver(mVolumeReceiver, IntentFilter("android.media.VOLUME_CHANGED_ACTION"))
        audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
    }

    private var mVolumeReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {

            if (intent.action == "android.media.VOLUME_CHANGED_ACTION") {
                var currentVolume = audioManager?.getStreamVolume(AudioManager.STREAM_MUSIC)
            }
        }
    }
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Piotr Labunski Mar 20 '20 at 10:32
0

This won't help you if you have override onKeyDown(int keycode,KeyEvent event) method of an activity.

Hitendra
  • 3,218
  • 7
  • 45
  • 74