18

I currently have my game correctly handling disabling its own BGM when music is playing in the built-in iPod app, but it does not detect when an app such as Pandora is playing music.

Currently, in my applicationDidBecomeActive method, I check [[MPMusicPlayerController iPodMusicPlayer] playbackState] to determine whether music is playing. What is the equivalent of this to check if an app like Pandora is playing audio in the background?

Tim R.
  • 1,570
  • 2
  • 15
  • 33

4 Answers4

27

AudioSessionGetProperty (as mentioned in jake_hetfield's answer) is deprecated as of iOS 7.

Instead, try this one-liner that uses isOtherAudioPlaying:

BOOL isOtherAudioPlaying = [[AVAudioSession sharedInstance] isOtherAudioPlaying];

Works on iOS 6+.

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
14

Check out this question

Seems you can see if another audio is playing by checking the property kAudioSessionProperty_OtherAudioIsPlaying like this:

UInt32 propertySize, audioIsAlreadyPlaying=0;
propertySize = sizeof(UInt32);
AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &audioIsAlreadyPlaying);

A complement to this could be to ask the user if he/she wants to have the game music or the already playing sound/music.

Community
  • 1
  • 1
jake_hetfield
  • 3,388
  • 1
  • 23
  • 30
  • Thank you! I did a ton of searching but somehow never found that question. I don't plan to ask the user, I think it's enough to infer that the user wants the keep listening to their own music if they have it playing already. – Tim R. Sep 18 '12 at 12:15
  • I'm getting a false positive from `kAudioSessionProperty_OtherAudioIsPlaying` at the moment. Other audio is definitely not playing (i stopped the iPod on my iPhone 5 iOS 6.x). This sometimes happens. It's like something gets stuck in there, making `kAudioSessionProperty_OtherAudioIsPlaying` always return YES/1/true/positive. It normally only fixes itself after rebooting the device. – Jonny Apr 23 '13 at 10:56
  • 1
    FYI, that might not be a false positive. Some apps play "silent audio" (i.e., a short audio file with no sound) to bypass the 15 minute idle timer. It could be an app doing that that's triggering a true return for the check. Just a thought. – stuckj Jun 03 '13 at 20:35
4

As of iOS 8, the secondaryAudioShouldBeSilencedHint property should be used:

/* Will be true when another application with a non-mixable audio session is playing audio.  Applications may use
this property as a hint to silence audio that is secondary to the functionality of the application. For example, a game app
using AVAudioSessionCategoryAmbient may use this property to decide to mute its soundtrack while leaving its sound effects unmuted.
Note: This property is closely related to AVAudioSessionSilenceSecondaryAudioHintNotification.
*/
@property(readonly) BOOL secondaryAudioShouldBeSilencedHint  NS_AVAILABLE_IOS(8_0);
kevinl
  • 4,194
  • 6
  • 37
  • 55
-1

You may want to do something like this.....

  1. Create A class to handle your audio settings say... "AudioManager"

  2. Poll the Boolean "isOtherAudioPlaying"... maybe assign it to your own Boolean value.

import Foundation
import AVFoundation

class AudioManager {
    static let successBingSoundID: SystemSoundID = <Your System Sound ID in Int>
    static func playSystemSoundIfBackgroundSoundIsOff() {
        guard !AVAudioSession.sharedInstance().isOtherAudioPlaying else {return}
        AudioServicesPlaySystemSoundWithCompletion(successBingSoundID, nil)
    }
}

usage:

AudioManager.playSystemSoundIfBackgroundSoundIsOff()
Hitit
  • 420
  • 6
  • 21
hoboBob
  • 832
  • 1
  • 17
  • 37
  • This answer doesn’t add anything that can’t be learned from the other answers, except it adds the extraneous step of adding an audio manager class. The example you provide can’t even poll that value more than once without another audio manager being constructed. – Tim R. Mar 15 '18 at 15:40
  • @TimR. I agree with you. But having to make a new class in order to separate logic can sometimes be useful. Also, @hoboBob actually provided swift version of `.isOtherAudioPlaying,` it provided some good info. I changed the original code so that people can use them easily. – Hitit Jul 29 '20 at 03:08