java 在 API 21 的 SoundPool.Builder 类中设置音频属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28210921/
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
set Audio Attributes in SoundPool.Builder class for API 21
提问by Pranit Bankar
I am following an Android Programming video lecture series which was designed in the pre-API 21 times. Hence it tells me to create a SoundPool variable in the following manner.
我正在关注一个在 API 之前设计的 Android 编程视频讲座系列 21 次。因此,它告诉我以下列方式创建 SoundPool 变量。
SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
//SoundPool(int maxStreams, int streamType, int srcQuality)
However, I want to use this SoundPool for API 21 as well. So, I am doing this:
但是,我也想将此 SoundPool 用于 API 21。所以,我这样做:
if((android.os.Build.VERSION.SDK_INT) == 21){
sp21 = new SoundPool.Builder();
sp21.setMaxStreams(5);
sp = sp21.build();
}
else{
sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
}
sp21 is a variable of Builder
type for API 21 and sp is of SoundPool
type.
sp21 是Builder
API 21 类型的变量,sp 是SoundPool
类型变量。
This works very well with my AVD having API 21 and real device having API 19. (Haven't tried with a real device with API 21 but I think it will work well). Now, I want to set the streamType
to USAGE_MEDIA
in the if-block before sp = sp21.build();
. So I type:
这对于我的具有 API 21 的 AVD 和具有 API 19 的真实设备非常有效。(尚未尝试使用具有 API 21 的真实设备,但我认为它会很好地工作)。现在,我想在 if-block before 中设置streamType
to 。所以我输入:USAGE_MEDIA
sp = sp21.build();
sp21.setAudioAttributes(AudioAttributes.USAGE_MEDIA);
But the Lint marks it in red and says:
但是 Lint 将其标记为红色并说:
The method setAudioAttributes(AudioAttributes) in the type SoundPool.Builder is not applicable for the arguments (int)
SoundPool.Builder 类型中的 setAudioAttributes(AudioAttributes) 方法不适用于参数 (int)
I know that even if I do not set it to USAGE_MEDIA it will be set to the same by default. But I am asking for future reference if I have to set it to something else like : USAGE_ALARM.
我知道即使我没有将它设置为 USAGE_MEDIA,它也会默认设置为相同的。但是如果我必须将它设置为其他类似的东西,我会要求将来参考:USAGE_ALARM。
How should I proceed ?
我应该如何进行?
Please Help!
请帮忙!
I have referred to Audio Attributes, SoundPool, SoundPool.builderand AudioManager.
我已经提到了Audio Attributes、SoundPool、SoundPool.builder和AudioManager。
回答by rmdroid
An AudioAttributes instance is built through its builder, AudioAttributes.Builder.
AudioAttributes 实例是通过其构建器 AudioAttributes.Builder 构建的。
You can use it in the following way.
您可以通过以下方式使用它。
sp21.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build());
Ref:https://developer.android.com/reference/android/media/AudioAttributes.html
参考:https: //developer.android.com/reference/android/media/AudioAttributes.html
回答by Rohit
I have something to add here. I was using SoundPool in my game app to play small and simple ogg audio files. It was working fine even on emulators with API 21. Today I decided to modify it to use SoundPool.Builder().
我有一些东西要在这里补充。我在我的游戏应用程序中使用 SoundPool 来播放小而简单的 ogg 音频文件。即使在带有 API 21 的模拟器上它也能正常工作。今天我决定修改它以使用 SoundPool.Builder()。
I looked at Android's SoundPool.Builder document. It says there
我查看了 Android 的 SoundPool.Builder 文档。它说在那里
public static class
SoundPool.Builder
extends Object
java.lang.Object
? android.media.SoundPool.Builder
Class Overview
Builder class for SoundPool objects.
Note the line "Builder class for SoundPool objects." So SoundPool.Builder() creates SoundPool object. SoundPool() also creates SoundPool object. So this is what I did.
请注意“SoundPool 对象的构建器类”这一行。所以 SoundPool.Builder() 创建 SoundPool 对象。SoundPool() 还创建 SoundPool 对象。所以这就是我所做的。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes audioAttrib = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
mSound = new SoundPool.Builder().setAudioAttributes(audioAttrib).setMaxStreams(6).build();
}
else {
mSound = new SoundPool(6, AudioManager.STREAM_MUSIC, 0);
}
mSound is declared as
mSound 被声明为
private SoundPool mSound;
Rest of the code (where I load, play, stop, release sound) remains exactly as it was earlier. And it is working in API 21 and earlier versions
其余代码(我加载、播放、停止、释放声音的地方)与之前完全一样。它适用于 API 21 及更早版本
Hope this helps you all
希望这对大家有帮助