Android: why does MediaPlayer play the same sound louder than SoundPool?

You can play audio using MediaPlayer or SoundPool on Android. Two of our Android devices play the same audio much louder when using MediaPlayer. On the third device, the volume sounds the same.

Do you know why? Can I make SoundPool play sounds as loud as MediaPlayer?

The devices in question:

Code: play mp3 sound with SoundPool

private void playSoundPool() {
    int MAX_STREAMS = 2;
    SoundPool soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int soundId, int status) {
            int loop = 0;
            int priority = 0;
            float rate = 1.f;
            soundPool.play(soundId, 1, 1, priority, loop, rate);                    
        }
    });
    soundPool.load(this, R.raw.test, 1);
}

      

Code: Play mp3 audio with MediaPlayer

private void playMediaPlayer() {
    MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.test);
    mediaPlayer.setVolume(1, 1);
    mediaPlayer.start();
}

      

Welcome to download the test project .

+3


source to share


2 answers


Possible Solution:

You need to create an AudioManager to change the global media volume set by the user in the phone and change the volume for our own app independently by changing the volume level in the soundPool.

AudioManager mgr = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);

int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);

streamVolume = streamVolume / AudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);

      

Note:

if I set 0.5 for the volume in the soundpool , the actual volume is always half global . Very easy to reproduce:

  • set total media volume in phone settings to max
  • set volume in action with soundpool.play to 0.5 - play sound
  • set volume in soundpool.play to 1 - play sound, it will be twice as loud

So the volume passed to the SoundPool.play method is indeed a multiplier for the global volume! ** So, if you want to play sound at the current volume setting, just pass "1" as the volume or change it as per requirement **



could be a media player class using the global media volume to play the audio file. you are using hardcoded soundPool.play (soundId, 1 , 1 , priority, loop, speed); values!

we need to try with

MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.test);
mediaPlayer.setVolume(streamVolume,streamVolume);
mediaPlayer.start();

      

A few more texts:

Soundpool:

SoundPool is designed for short clips that can be stored in memory unpacked for quick access, this is best for sound effects in applications or games. Using this method with soundbars is a bad idea as you will be loading a lot of medium sized sounds in and you may exceed the limit (16MB)and get OutOfMemoryException. SoundPool downloads music files using a separate thread, the work of the main thread does not block the user interface. it can be used to play short sound clips like pistol shots during a game (something like less than 3 seconds). A nice feature that the sound pool has is that you can play many sounds at the same time, which happens very often when you think about playing. So, you first created a Sound Pool Object to load all your media files. MediaPlayer: "

MediaPlayer is for longer audio files or streams, it is best for music files or large files. Files will be loaded from disk every time create is called, this will save memory but introduce a slight delay (not very noticeable).

Link: fooobar.com/questions/283830 / ...

0


source


There is a difference in the mechanism, as you can tell that due to compression and decompression, the sound quality and volume may be lower than the actual one, and when you compare it to MediaPlayer. Therefore, it is better to use MediaPlayer for better sound quality and volume in your case.

Soundpool

The SoundPool is for short clips that can be stored in memory, unpacked for quick access, this is best for sound effects in applications or games. Using this method with soundbars is a bad idea because you are loading a large number of medium sized sounds into memory and you may exceed your limit (16MB) and get an OutOfMemoryException.



MediaPlayer

MediaPlayer is for longer audio files or streams, it is best for music files or large files. Files will be loaded from disk every time create is called, this will save memory, but introduce a small delay (not very noticeable).

-1


source







All Articles