LoginSignup
42
41

More than 5 years have passed since last update.

【iOS】他アプリで音楽が再生中か知りたい 重ねて再生させたい

Last updated at Posted at 2016-05-18

 ユーザーがMusicアプリで音楽再生やsafariで音声ストーリミング再生などをしていた場合、他アプリを開いた際にはその音楽がそのまま再生されることを期待している

 しかし、標準の設定では元々再生していた音楽は止まるようになっているので、その回避方法メモ

1. 他アプリで音楽が再生中かのチェック方法

Objective-C

if ([[AVAudioSession sharedInstance] isOtherAudioPlaying]) {
    // 再生中!!
}

Swift

if AVAudioSession.sharedInstance().otherAudioPlaying {
    // 再生中!!
}

 これであとは、再生中であれば自アプリのBGMなどを止めたり、再生させないような処理を記述すればok

2. 他アプリの音楽と重ねて再生させたい

 もし他アプリで音楽が再生中であれば、止めずに自アプリのBGMやSE、ボイスなどと重ねて再生させたいといった場合もある
その場合はAVAudioSessionのCategoryにAVAudioSessionCategoryAmbientを設定すれば良い

Objective-C

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

Swift

try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)

 Categoryにはデフォルトでは他アプリの再生を停止するAVAudioSessionCategorySoloAmbientが設定されている

42
41
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
42
41