Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
36
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@black_iron

【Android】ロリポップからのSoundPool

今までSoundPoolのインスタンス化は下記のような方法でした。

final int SOUND_POOL_MAX = 6;

SoundPool pool = new SoundPool(SOUND_POOL_MAX, AudioManager.STREAM_MUSIC, 0);

Android 5.0 ロリポップ (API Level 21) からこの方法は非推奨になり、次の様な書き方が推奨されます。

final int SOUND_POOL_MAX = 6;

AudioAttributes attr = new AudioAttributes.Builder()
    .setUsage(AudioAttributes.USAGE_MEDIA)
    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
    .build();

SoundPool pool = new SoundPool.Builder()
    .setAudioAttributes(attr)
    .setMaxStreams(SOUND_POOL_MAX)
    .build();

互換性を保つ場合は下記の様な感じになると思います。

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private SoundPool buildSoundPool(int poolMax)
{
    SoundPool pool = null;

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        pool = new SoundPool(poolMax, AudioManager.STREAM_MUSIC, 0);
    }
    else {
        AudioAttributes attr = new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_MEDIA)
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .build();

        pool = new SoundPool.Builder()
            .setAudioAttributes(attr)
            .setMaxStreams(poolMax)
            .build();
    }

    return pool;
}

final int SOUND_POOL_MAX = 6;
SoundPool pool = buildSoundPool(SOUND_POOL_MAX);
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
36
Help us understand the problem. What are the problem?