Android SoundPool不播放声音

问题描述 投票:1回答:1

我想使用SoundPool单击按钮时播放短声音。但是,当单击该按钮时,尽管Log告诉我它已加载并正在播放,但没有声音出现。如果相关,声音文件是.ogg。这是我的代码,在buttonSound()中调用了onCreate()

void buttonSound(){
    AudioAttributes attributes = new AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
        .build();

    soundPool = new SoundPool.Builder().setAudioAttributes(attributes).build();

    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId,
            int status) {
            Log.d("SOUNDPOOL", "COMPLETE "+button);
        }
    });
    button = soundPool.load(this, R.raw.button, 1);

}


public void onClick(View view) {
    switch (view.getId()) {
        case R.id.button_back:
            // Sound loaded
            if(button != 0) {
                soundPool.play(button, 1f, 1f, 1, 0, 1f);
                Log.d("PLAYING", "PLAYING");
            }
            break;
android android-studio soundpool
1个回答
0
投票

因此显然AudioAttributes.USAGE_ASSISTANCE_SONIFICATION是问题,它与AudioAttributes.USAGE_GAMEAudioAttributes.USAGE_MEDIA正常工作。不过,我不知道为什么。

© www.soinside.com 2019 - 2024. All rights reserved.