views:

92

answers:

1

Hi, here is my situation:

I have a media player playing music in an Android application. I've found that with certain headphones, the volume is much too loud even when the volume is set to it's lowest setting. As a result, I want to change the volume of the music for all volume levels to be 10% of what it normally is (actually, this value is user-defined of course).

The following works perfectly:

mediaPlayer.setVolume(0.1f, 0.1f);

The volume of the music is now at a good level for listening. However, if the user now changes the volume using the volume rocker (thus changing the music stream volume), the media player changes the volume as expected, but it also seems to reset the 'setVolume' parameters to 1.0, causing a massive volume change. Setting the volume back to 0.1 sets the volume to how it should be (which is 10% of the current music stream volume).

To quote the Android docs for the MediaPlayer.setVolume method:

This API is recommended for balancing the output of audio streams within an application

How can you do this if it gets reset to 1.0 each time the system volume changes?

Any help muchly appreciated. Thanks.

A: 

My suggestion would be to try overriding the onKeyDown(...) and manually adjust the volume by getting the AudioManager object using:

getSystemService(AUDIO_SERVICE).adjustVolume(ADJUST_RAISE);

or whatever method you are using to change the volume. By overriding the volume rocker button behaviors, you'll remove any default behaviors that are causing problems.

mtmurdock
Thanks for your comment. Overriding onKeyUp/Down (and then re-setting the MediaPlayer object to the correct volume) is a slight improvement, but there are two problems:1: The activity must be in focus (normally not the case for a music player playing in a service)2: If the correct view does have focus, for a split second the volume goes very loud before being corrected.Thanks again though. Anyway, I think I need a suite of Android phones to test this sort of thing...
Daniel Flower
There are some other adjustVolume methods in the api that effect specific streams in specific ways, so you may want to check those out (im not sure if they'll be any more help to you).
mtmurdock