简体   繁体   中英

SoundPool media playing as ringtone not as media

public class Soundboard extends AppCompatActivity {
private SoundPool soundPool;
private int sound1, sound2, sound3, sound4, sound5, sound6, sound7, sound8, sound9, sound10, sound11;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_soundboard);

    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(11)
                .setAudioAttributes(audioAttributes)
                .build();
    } else {
        soundPool = new SoundPool(11, AudioManager.STREAM_MUSIC, 0);
    }
    sound1 = soundPool.load(this, R.raw.tournament, 1);
    sound2 = soundPool.load(this, R.raw.tournament2_fortnite, 1);
    sound3 = soundPool.load(this, R.raw.uuuaaaa, 1);
    sound4 = soundPool.load(this, R.raw.lass_heute_fortnite, 1);
    sound5 = soundPool.load(this, R.raw.kannst_du_fortnite, 1);
    sound6 = soundPool.load(this, R.raw.fortnite_mit_dir, 1);
    sound7 = soundPool.load(this, R.raw.papapapaka, 1);
    sound8 = soundPool.load(this, R.raw.haeusl, 1);
    sound9 = soundPool.load(this, R.raw.cringe_musik, 1);
    sound10 = soundPool.load(this, R.raw.lachen, 1);
    sound11 = soundPool.load(this, R.raw.furz, 1);
}

public void playSound(View v) {
    switch (v.getId()) {
        case R.id.imageButton:
            soundPool.play(sound1, 1, 1, 0, 0, 1);
            break;
        case R.id.imageButton2:
            soundPool.play(sound2, 1, 1, 0, 0, 1);
            break;
        case R.id.imageButton3:
            soundPool.play(sound3, 1, 1, 0, 0, 1);
            break;
        case R.id.imageButton4:
            soundPool.play(sound4, 1, 1, 0, 0, 1);
            break;
        case R.id.imageButton5:
            soundPool.play(sound5, 1, 1, 0, 0, 1);
            break;
        case R.id.imageButton6:
            soundPool.play(sound6, 1, 1, 0, 0, 1);
            break;
        case R.id.imageButton7:
            soundPool.play(sound7, 1, 1, 0, 0, 1);
            break;
        case R.id.imageButton8:
            soundPool.play(sound8, 1, 1, 0, 0, 1);
            break;
        case R.id.imageButton9:
            soundPool.play(sound9, 1, 1, 0, 0, 1);
            break;
        case R.id.imageButton10:
            soundPool.play(sound10, 1, 1, 0, 0, 1);
            break;
        case R.id.imageButton11:
            soundPool.play(sound11, 1, 1, 0, 0, 1);
            break;
    }
}

Hi there. I am currently designing a soundboard and I am using soundPool to play the sounds. It is working pretty well but my sounds are played as ringtone. But I want them to be played as media? How do I do that, I didn't find anything on the subject. but maybe I was just too clumsy.

I hope you can help me, Bernd

Try using AudioAttributes.USAGE_GAME.

    protected void createSoundPool() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        createNewSoundPool();
    } else {
        createOldSoundPool();
    }
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected void createNewSoundPool() {
    AudioAttributes attributes = new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_GAME)
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .build();
    soundPool = new SoundPool.Builder()
            .setAudioAttributes(attributes)
            .setMaxStreams(MAX_STREAMS)
            .build();
}

protected void createOldSoundPool() {
    soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, SRC_QUALITY);
}

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