Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

今まで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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした