Soundpool 不播放任何声音

发布于 2025-01-11 02:04:25 字数 1198 浏览 0 评论 0原文

我试图让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹎☆浅夏丿初晴 2025-01-18 02:04:25

load 调用之前设置 setOnLoadCompleteListener (应该使用 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 all SoundPoolload 方法是同步的 - 它们返回已加载的音频文件的 ID。加载音频后设置 OnLoadCompleteListener 将导致不会调用此侦听器,因为没有更多内容需要加载

set up setOnLoadCompleteListener BEFORE load call (which should use sampleId, as game_over may be not assigned yet)

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 all load methods of SoundPool are synchronous - they are returning ID of already loaded audio file. setting OnLoadCompleteListener AFTER loading audio will cause no call for this listener, as there is nothing more to load

向地狱狂奔 2025-01-18 02:04:25

这段代码现在可以工作了。为什么它在我的手机上无法播放是因为它使用的是系统音量而不是音乐音量。希望这将来能对其他人有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文