这是否意味着我们不能再使用它了? 如果 min API 设置在 21 以下,我们应该使用什么? 另外,是否可以忽略警告,因为使用它构建的旧应用程序可以在新操作系统上运行?
最佳答案
为什么不推荐使用 SoundPool 构造函数
老SoundPool
constructor已弃用,取而代之的是使用 SoundPool.Builder
构建 SoundPool
对象。 old constructor有三个参数:maxStreams
、streamType
和 srcQuality
。
maxStreams
参数仍然可以是set with the Builder . (如果不设置,则默认为 1。)streamType
参数替换为AudioAttributes
,比streamType
更具描述性。 (查看从 here 开始的不同流类型常量。)使用AudioAttributes
您可以指定 usage(播放声音的原因)、内容类型(您正在播放的内容)和 flags(如何播放)。srcQuality
参数应该是用来设置采样率转换器质量的。但是,它从未实现过,设置也没有任何效果。
因此,SoundPool.Builder
比旧的构造函数更好,因为 maxStreams
不需要显式设置,AudioAttributes
包含比旧的构造函数更多的信息streamType
,去掉了无用的srcQuality
参数。这就是旧的构造函数被弃用的原因。
使用已弃用的构造函数来支持 API 21 之前的版本
如果您愿意,您仍然可以使用旧的构造函数并忽略警告。 “已弃用”意味着它仍然有效,但不再是推荐的做事方式。
如果您希望在仍支持旧版本的同时使用新的构造函数,您可以使用 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);
观看this video了解更多详情。
关于java - Android:为什么不推荐使用 SoundPool 的构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39184157/