这是否意味着我们不能再使用它了? 如果最小API设置在21以下,我们应该使用什么? 而且,忽略警告并使用旧版应用程序在新操作系统上工作是可以的吗?
这是否意味着我们不能再使用它了? 如果最小API设置在21以下,我们应该使用什么? 而且,忽略警告并使用旧版应用程序在新操作系统上工作是可以的吗?
旧的SoundPool
构造函数已被弃用,建议使用SoundPool.Builder
来构建SoundPool
对象。旧构造函数有三个参数:maxStreams
、streamType
和srcQuality
。
maxStreams
参数仍然可以通过Builder进行设置。(如果不设置,默认为1。)streamType
参数被AudioAttributes
取代,这比streamType
更具描述性。(查看从这里开始的不同流类型常量。)使用AudioAttributes
,您可以指定用途(为什么播放声音),内容类型(正在播放什么),以及标志(如何播放它)。srcQuality
参数据说是用于设置采样率转换器的质量。但是,它从未得到实现,设置它也没有任何效果。SoundPool.Builder
比旧的构造器更好,因为不需要显式设置 maxStreams
,AudioAttributes
包含比 streamType
更多的信息,并且省略了无用的 srcQuality
参数。这就是为什么旧构造器已被弃用。
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);
观看此视频以获取更多细节。
Builder
不仅仅是添加一个新的构造函数。它是一个用于构建(或构造)SoundPool
的新类。这种设计模式似乎是一种趋势。(例如StaticLayout.Builder
)。但即使他们只是添加了一个新的构造函数,通常也会弃用不再推荐使用的旧构造函数(请参见Java Date
)。 - SuragchSoundPool.Builder
代替。创建SoundPool的方式已经改变,建议使用新的方式。
minSdkVersion
为21或更高,则应该使用SoundPool.Builder
。 “如果min API设置低于21,应该使用什么?”- 您只能使用SoundPool
构造函数。 - CommonsWareSoundPool
提供与MediaPlayer
不同的功能(例如,优先级流)。我不会仅仅因为它的构造函数已被弃用而避免使用SoundPool
。至于SoundPool
是否符合您的整体需求,我无法说。 - CommonsWare