LoginSignup
1
3

More than 5 years have passed since last update.

[Android]音量変更のアウトプットの設定

Last updated at Posted at 2017-04-18

経緯

音が出るアプリでメディアの音量を調整したいのに着信音が変化してしまう。
そんなことってありますよね。

対応

音の出る画面のActivityで

setVolumeControlStream(AudioManager.STREAM_MUSIC);

これで大丈夫。ちゃんとメディア音量が変化するようになります。

あれ?効果音だけ大きい?
それはきっとSoundPoolのAudioAttributesがUSAGE_MEDIAになってないんじゃないかな?

AudioAttributes audioAttributes = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
  audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
soundPool = new SoundPool.Builder().setAudioAttributes(audioAttributes).setMaxStreams(1).build();
} else {
//ロリポップより前のバージョンに対応するコード
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
}

参考
Handling Changes in Audio Output
AudioManager

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3