我应该使用 AudioTrack、SoundPool 或 MediaPlayer 哪个? [英] AudioTrack, SoundPool or MediaPlayer Which Should I use?

查看:31
本文介绍了我应该使用 AudioTrack、SoundPool 或 MediaPlayer 哪个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要播放多个不同时长的音频文件,比如 5 到 30 秒.我想为右/左声道独立设置音量,并应用效果,如混响或失真.那么,我应该使用哪个 API?

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?

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

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

推荐答案

Sound pool 实际上是混音器.它只能播放短片,无论它们是编码为 ogg 或 mp3 还是未压缩.声音池总是将它们未压缩地存储在内存中,您必须知道限制是 1 MB.如果您的剪辑在内存中太大,声音池将变得无声,并且您会发现以下错误:AudioFlinger 无法创建轨道.状态:-12"媒体播放器播放流并实时解码.所以它可以播放更长的剪辑,但需要处理器能力.

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.

注意:如果剪辑未完全加载和解码,您将无法播放声音池中的音乐.所以你必须使用 OnLoadCompleteListener(Android 10 或更高版本)来检查它.如果您在解码之前尝试播放声音,则声音池将静音.

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.

这篇关于我应该使用 AudioTrack、SoundPool 或 MediaPlayer 哪个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆