AudioTrack, SoundPool or MediaPlayer Which Should I use?

余生长醉 提交于 2019-11-26 09:34:37

问题


I need to play multiple audio files, with different duration, like 5 to 30 seconds. And i want to set volume independently for right/left channel and also to apply effects, like Reverb or Distortion. So, Which API should i use?

Also, i can\'t find too much doc on AudioTrack API. Do you know where can i find examples? Thx.


回答1:


Sound pool is actually audio mixer. It can play short clips only regardless of whether they are encoded as ogg or mp3 or they are uncompressed. Sound pool always store them in memory uncompressed, and you must know that limit is 1 MB. If your clip is too big in memory, sound pool will fall silent, and you'll find following error: "AudioFlinger could not create track. status: -12" Media player plays stream and decode it in real time. So it can play much longer clips but needs processor power for it.

So Media player is better for background music, while sound pool is better fort short audio effects (clicks, explosions, sound loops). In addition, sound pool can play more clips simultaneously, and has volume and speed control. Also it can play loops.

One note: you can't play music from sound pool if clip isn't fully loaded and decoded. So you must use OnLoadCompleteListener (Android 10 or above) to check it. If you try to play sound before it is decoded, sound pool will be mute.

Media player doesn't suffer from these problems.




回答2:


In addition to what the other answers suggested, one difference between MediaPlayer and SoundPool is that in MediaPlayer, you cannot play a sound clip when it is already playing (that feature could be helpful especially in games). MediaPlayer needs the clip to finish completely before it could be played again. I think that information is provided in the official website.

However, SoundPool does not have such an issue and also I noticed that SoundPool plays audio just when it is called (I sometimes noticed a slight delay when I used MediaPlayer ).

I think that is (part of) the reason why people say that SoundPool is better for short sounds such as in games. Hope this helps:)




回答3:


I would recommend MediaPlayer for such duration.

You can find more info here: How bad is Android SoundPool? What alternative to use?




回答4:


MediaPlayer does not support setting the volume for multible channels.

I think SoundPool is the only thing that suits your needs here.

EDIT: Yup, you need a SoundPool, read this: http://www.wiseandroid.com/post/2010/07/13/Intro-to-the-three-Android-Audio-APIs.aspx



来源:https://stackoverflow.com/questions/13527134/audiotrack-soundpool-or-mediaplayer-which-should-i-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!