java - Android:为什么不推荐使用 SoundPool 的构造函数?

标签 java android audio constructor deprecated

这是否意味着我们不能再使用它了? 如果 min API 设置在 21 以下,我们应该使用什么? 另外,是否可以忽略警告,因为使用它构建的旧应用程序可以在新操作系统上运行?

最佳答案

为什么不推荐使用 SoundPool 构造函数

SoundPool constructor已弃用,取而代之的是使用 SoundPool.Builder构建 SoundPool 对象。 old constructor有三个参数:maxStreamsstreamTypesrcQuality

  • 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/

相关文章:

c - 不使用<windows.h>在C程序中播放轨道

c - 使用ffmpeg c连接视频和音频时如何计算pts和dts

java - 透明扫描仪(中)

java - 在 Jackson 中序列化/反序列化别名基元,无需太多样板

java - 我创建了一个默认拨号器应用程序,但 ACTION_CALL Intent 在某些设备上不起作用

java - 解密位图的像素

java - 如何从扬声器获取TargetDataLine?

java - 如何计算字符串出现的次数

java - Android:findViewById 返回 null?

android - 即使我完成了该应用程序,它也会继续返回到之前的 Activity