提问人:Inomie 提问时间:3/2/2022 更新时间:3/2/2022 访问量:125
Soundpool 未播放任何声音
Soundpool is not playing any sound
问:
我试图使 soundpool 工作,因为我需要将它用于产品。问题是它没有播放任何声音。我只是想播放测试声音,看看它是否有效。希望有人能看到我在这里做错了什么。
public class MainActivity extends AppCompatActivity {
SoundPool soundPool;
int game_over;
@Override
protected void onCreate(
Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes audioAttributes = new AudioAttributes
.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
soundPool = new SoundPool
.Builder()
.setMaxStreams(4)
.setAudioAttributes(audioAttributes)
.build();
}
game_over = soundPool.load(this, R.raw.test, 1);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundPool.play(game_over, 1, 1, 1, 1, 1f);
}
});
}
}
答:
1赞
snachmsm
3/2/2022
#1
设置 BEFORE 调用(应使用 ,因为可能尚未分配)setOnLoadCompleteListener
load
sampleId
game_over
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundPool.play(sampleId, 1, 1, 1, 1, 1f);
}
});
game_over = soundPool.load(this, R.raw.test, 1);
afaik 的所有方法都是同步的 - 它们返回已加载的音频文件的 ID。设置 AFTER loading audio 将导致此侦听器不调用,因为没有更多内容要加载load
SoundPool
OnLoadCompleteListener
评论
0赞
Inomie
3/2/2022
没错,错过了那件事。但它没有发挥任何作用。
0赞
Inomie
3/2/2022
只是试过了。play 方法返回 1,因此它应该正在播放。
0赞
snachmsm
3/2/2022
确保音量级别,也许使用另一个测试文件...还可以尝试使用不同的标志。没有任何其他线索......setUsage
0赞
Inomie
3/2/2022
它在模拟器上工作,但不在我的手机上工作
0赞
Inomie
3/2/2022
#2
此代码现在正在工作。为什么它没有在我的手机上播放,是因为它使用的是系统音量而不是音乐音量。希望这将在未来帮助其他人。
评论