Soundpool 不播放任何声音
我试图让 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);
}
});
}
}
Im trying to make soundpool work because I need to use it for a prodject. The problem is that it's not playing any sound. Im just trying to play a test sound to see if it works. Hope someone can see what im doing wrong here.
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);
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
load
调用之前设置setOnLoadCompleteListener
(应该使用sampleId
,因为game_over
可能尚未分配)afaik all
SoundPool
的load
方法是同步的 - 它们返回已加载的音频文件的 ID。加载音频后设置OnLoadCompleteListener
将导致不会调用此侦听器,因为没有更多内容需要加载set up
setOnLoadCompleteListener
BEFOREload
call (which should usesampleId
, asgame_over
may be not assigned yet)afaik all
load
methods ofSoundPool
are synchronous - they are returning ID of already loaded audio file. settingOnLoadCompleteListener
AFTER loading audio will cause no call for this listener, as there is nothing more to load这段代码现在可以工作了。为什么它在我的手机上无法播放是因为它使用的是系统音量而不是音乐音量。希望这将来能对其他人有所帮助。
This code is now working. Why it was not playing on my phone was because it was using the system volume and not the music volume. Hope this will help someone else in the future.