iOS: ¿Cómo detecto si se está reproduciendo música en cualquier aplicación de música de fondo?

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.

Actualmente, en mi 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?

preguntado el 18 de septiembre de 12 a las 12:09

4 Respuestas

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

En su lugar, pruebe esta frase de una sola línea que usa esOtroAudioReproduciendo:

BOOL isOtherAudioPlaying = [[AVAudioSession sharedInstance] isOtherAudioPlaying];

Funciona en iOS 6+.

Respondido el 29 de Septiembre de 14 a las 04:09

Versión Swift 2.3: var otherAudioPlaying = AVAudioSession.sharedInstance().otherAudioPlaying - Callam

Rápido 3: var otherAudioPlaying = AVAudioSession.sharedInstance().isOtherAudioPlaying - Emil Korngold

Comprar esta pregunta

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.

contestado el 23 de mayo de 17 a las 13:05

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.

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

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. - atascado

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);

respondido 10 mar '16, 23:03

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)



Respondido 30 Jul 20, 13:07

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.

@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. - hitita

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.