简体   繁体   中英

Android SoundPool plays no sound

I want to play a short sound when a button is clicked using SoundPool. But when the button is clicked no sound appears despite the Log telling me it's loaded and "playing". The soundfile is an .ogg in case that's relevant. Here's my code, buttonSound() is called in 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;

So apparently AudioAttributes.USAGE_ASSISTANCE_SONIFICATION was the issue, it works fine with AudioAttributes.USAGE_GAME and AudioAttributes.USAGE_MEDIA . I have no idea why, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM