我应该使用AudioTrack,SoundPool还是MediaPlayer?

时间:2022-09-10 23:10:26

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?

我需要播放多个音频文件,持续时间不同,比如5到30秒。我想为右/左声道独立设置音量,也可以应用效果,如Reverb或Distortion。那么,我应该使用哪种API?

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

另外,我在AudioTrack API上找不到太多的文档。你知道我在哪里可以找到例子吗?谢谢。

4 个解决方案

#1


47  

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.

声音池实际上是音频混音器。它可以播放短片段,无论它们是编码为ogg还是mp3,还是未压缩。声音池始终将它们存储在未压缩的内存中,您必须知道限制为1 MB。如果你的剪辑内存太大,声音池会沉默,你会发现以下错误:“AudioFlinger无法创建曲目。状态:-12”媒体播放器播放流并实时解码。所以它可以播放更长的剪辑但需要处理器能力。

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.

一个注意事项:如果剪辑未完全加载和解码,则无法播放声音池中的音乐。因此,您必须使用OnLoadCompleteListener(Android 10或更高版本)进行检查。如果您在解码之前尝试播放声音,则声音池将静音。

Media player doesn't suffer from these problems.

媒体播放器不会遇到这些问题。

#2


5  

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.

除了其他答案所提出的建议之外,MediaPlayer和SoundPool之间的一个区别是,在MediaPlayer中,当它已经播放时你不能播放声音片段(该功能在游戏中尤其有用)。 MediaPlayer需要剪辑完全完成才能再次播放。我认为这些信息是在官方网站上提供的。

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 ).

但是,SoundPool没有这样的问题,而且我注意到SoundPool在调用时播放音频(我有时注意到使用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:)

我认为这是(部分原因)人们说SoundPool更适合游戏等短音。希望这可以帮助:)

#3


4  

I would recommend MediaPlayer for such duration.

我会推荐MediaPlayer这么长时间。

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

你可以在这里找到更多信息:Android SoundPool有多糟糕?还有什么替代品?

#4


2  

MediaPlayer does not support setting the volume for multible channels.

MediaPlayer不支持为多个频道设置音量。

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

我认为SoundPool是唯一适合您需求的东西。

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

编辑:是的,你需要一个SoundPool,请阅读:http://www.wiseandroid.com/post/2010/07/13/Intro-to-the-three-Android-Audio-APIs.aspx

#1


47  

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.

声音池实际上是音频混音器。它可以播放短片段,无论它们是编码为ogg还是mp3,还是未压缩。声音池始终将它们存储在未压缩的内存中,您必须知道限制为1 MB。如果你的剪辑内存太大,声音池会沉默,你会发现以下错误:“AudioFlinger无法创建曲目。状态:-12”媒体播放器播放流并实时解码。所以它可以播放更长的剪辑但需要处理器能力。

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.

一个注意事项:如果剪辑未完全加载和解码,则无法播放声音池中的音乐。因此,您必须使用OnLoadCompleteListener(Android 10或更高版本)进行检查。如果您在解码之前尝试播放声音,则声音池将静音。

Media player doesn't suffer from these problems.

媒体播放器不会遇到这些问题。

#2


5  

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.

除了其他答案所提出的建议之外,MediaPlayer和SoundPool之间的一个区别是,在MediaPlayer中,当它已经播放时你不能播放声音片段(该功能在游戏中尤其有用)。 MediaPlayer需要剪辑完全完成才能再次播放。我认为这些信息是在官方网站上提供的。

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 ).

但是,SoundPool没有这样的问题,而且我注意到SoundPool在调用时播放音频(我有时注意到使用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:)

我认为这是(部分原因)人们说SoundPool更适合游戏等短音。希望这可以帮助:)

#3


4  

I would recommend MediaPlayer for such duration.

我会推荐MediaPlayer这么长时间。

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

你可以在这里找到更多信息:Android SoundPool有多糟糕?还有什么替代品?

#4


2  

MediaPlayer does not support setting the volume for multible channels.

MediaPlayer不支持为多个频道设置音量。

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

我认为SoundPool是唯一适合您需求的东西。

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

编辑:是的,你需要一个SoundPool,请阅读:http://www.wiseandroid.com/post/2010/07/13/Intro-to-the-three-Android-Audio-APIs.aspx