java Android:为什么不推荐使用 SoundPool 的构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39184157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Why is the constructor for SoundPool deprecated?
提问by Harsha
Does it mean that we cannot use it anymore? What should we use if the min API is set below 21? Also, is it okay to ignore the warning as older applications built using it work on the new OSes?
这是否意味着我们不能再使用它了?如果将 min API 设置为低于 21,我们应该使用什么?此外,可以忽略警告,因为使用它构建的旧应用程序可以在新操作系统上运行吗?
回答by Suragch
Why the SoundPool constructor was deprecated
为什么不推荐使用 SoundPool 构造函数
The old SoundPool
constructorwas deprecated in favor of using SoundPool.Builder
to build the SoundPool
object. The old constructorhad three parameters: maxStreams
, streamType
, and srcQuality
.
旧的SoundPool
构造函数已被弃用,转而SoundPool.Builder
用于构建SoundPool
对象。在老的构造函数有三个参数:maxStreams
,streamType
和srcQuality
。
- The
maxStreams
parameter can still be set with the Builder. (And if you don't set it, it defaults to 1.) - The
streamType
parameter is replaced byAudioAttributes
, which is more descriptive thanstreamType
. (See the different stream type constants starting here.) WithAudioAttributes
you can specify the usage(why you are playing the sound), the content type(what you are playing), and flags(how to play it). - The
srcQuality
parameter was supposedly there to set the sample-rate converter quality. However, it was never implemented and setting it had no effect.
- 该
maxStreams
参数仍然可以通过 Builder 设置。(如果你不设置它,它默认为 1。) - 该
streamType
参数被替换AudioAttributes
,这是比更具描述streamType
。(请参阅此处开始的不同流类型常量。)AudioAttributes
您可以指定用法(为什么播放声音)、内容类型(您正在播放的内容)和标志(如何播放)。 - 该
srcQuality
参数应该是用来设置采样率转换器质量的。但是,它从未实施并且设置它没有任何效果。
Thus, SoundPool.Builder
is better than the old constructor because maxStreams
does not need to be explicitly set, AudioAttributes
contains more information than streamType
, and the useless srcQuality
parameter was eliminated. That is why the old constructor was deprecated.
因此,SoundPool.Builder
比旧的构造函数更好,因为maxStreams
不需要显式设置,AudioAttributes
包含比 多的信息streamType
,并且srcQuality
消除了无用的参数。这就是不推荐使用旧构造函数的原因。
Using the deprecated constructor to support versions before API 21
使用已弃用的构造函数来支持 API 21 之前的版本
You may still use the old constructor and ignore the warnings if you like. "Deprecated" means that it still works but is no longer the recommended way of doing things.
如果您愿意,您仍然可以使用旧的构造函数并忽略警告。“已弃用”意味着它仍然有效,但不再是推荐的做事方式。
If you wish to make use of the new constructor while still supporting old versions you can use an if
statement to select the API version.
如果您希望在仍支持旧版本的同时使用新构造函数,您可以使用if
语句来选择 API 版本。
SoundPool mSoundPool;
int mSoundId;
//...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mSoundPool = new SoundPool.Builder()
.setMaxStreams(10)
.build();
} else {
mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
}
mSoundId = mSoundPool.load(this, R.raw.somesound, 1);
// ...
mSoundPool.play(mSoundId, 1, 1, 1, 0, 1);
Watch this videofor more details.
观看此视频了解更多详情。
回答by Augusto Carmo
Use SoundPool.Builder
instead. The way a SoundPool is created has been changed. You are encouraged to use the new way.
使用SoundPool.Builder
来代替。SoundPool 的创建方式已经改变。鼓励您使用新方法。