gpt4 book ai didi

Android:为什么 MediaPlayer 播放的声音比 SoundPool 大?

转载 作者:太空狗 更新时间:2023-10-29 15:26:37 26 4
gpt4 key购买 nike

您可以在 Android 上使用 MediaPlayer 或 SoundPool 播放声音。当使用 MediaPlayer 时,我们的两个 Android 设备播放相同的声音时声音会大得多。在第三台设备上,音量听起来是一样的。

你知道为什么吗?我可以让 SoundPool 播放与 MediaPlayer 一样响亮的声音吗?

有问题的设备:

代码:用SoundPool播放mp3声音

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);
}

代码:用MediaPlayer播放mp3声音

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

欢迎您download test project .

最佳答案

可能的解决方案:

您需要创建 AudioManager 来获取、更改用户在手机中设置的全局媒体音量,以及通过更改 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);

注意:

如果我在soundpool 中为音量设置0.5,实际音量将始终是全局音量 的一半。非常容易重现:

  • 在手机设置中将全局媒体音量设置为最大
  • 使用 soundpool.play 将 Activity 音量设置为 0.5 - 播放声音
  • 将 soundpool.play 中的音量设置为 1 - 播放声音,这将是两次大声

所以传递给 SoundPool.play 方法的音量确实是全局音量的倍增器!**因此,如果您想在当前音量设置下播放声音,只需将“1”作为音量传递或根据要求进行更改**

可能是使用全局媒体音量播放音频文件的媒体播放器类。您正在使用硬编码的 soundPool.play(soundId, 1, 1, priority, loop, rate);值(value)观!

我们需要尝试

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

更多文本:

SoundPool:

SoundPool is designed for short clips which can be kept in memory decompressed for quick access, this is best suited for sound effects in apps or games. Using this method with soundboards is a bad idea as you will be loading lots of “medium” sized sounds into the memory and you may exceed your limit (16Mb) and get an OutOfMemoryException. SoundPool load music files using a separate thread, the operation of the main thread does not block the UI. it can be used to play short sound clips say like gun shots during a game (something like less than 3 seconds long). A nice feature that sound pool comes with is that you can play many sounds simultaneously which happens a lot when you think of a game. So you first build a Sound Pool object to load all media files. MediaPlayer:"

MediaPlayer is designed for longer sound files or streams, this is best suited for music files or larger files. The files will be loaded from disk each time create is called, this will save on memory space but introduce a small delay (not really noticeable).

引用: SO

关于Android:为什么 MediaPlayer 播放的声音比 SoundPool 大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26190070/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com