I play audio (narration) in an audiobook. The audio files are in .ogg format, between 10 and 15 seconds each.
Edit to add: I am using Android 2.2, API 8, and I have this in my manifest:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
I have setVolumeControlStream (AudioManager.STREAM_MUSIC); in my onCreate () method.
My sounds are reproduced by code similar to this:
mp = MediaPlayer.create(mContext, resource); mp.setOnCompletionListener(this); mp.seekTo(0); mp.setLooping(looping); if(isSoundEnabled()) { mp.setVolume(1, 1); } else { // I still need sounds to call their onComplete listeners to drive action mp.setVolume(0,0); } nowPlaying = true; mp.start();
But, despite more assurances (33 and counting!), You just need to setVolumeControlStream (AudioManager.STREAM_MUSIC); in onCreate () my sounds do not change volume when I press the volume keys on my device (Motorola Xoom).
To verify that my volume keys did something, I overloaded onKeyDown :
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { boolean result = true; if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { DebugLog.d(TAG, "volume up"); } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { DebugLog.d(TAG, "volume down"); } }
and confirmed through LogCat that I press the correct volume keys:
07-19 12:16:31.440: DEBUG/BookReader(17830): volume down; thread 1 07-19 12:16:31.710: DEBUG/BookReader(17830): volume down; thread 1 07-19 12:16:31.980: DEBUG/BookReader(17830): volume down; thread 1 07-19 12:16:32.440: DEBUG/BookReader(17830): volume up; thread 1 07-19 12:16:32.820: DEBUG/BookReader(17830): volume up; thread 1
Although the following link is for SoundPool , should I calculate the volume of the stream instead of using mp.setVolume(1, 1); in my first code example above?