18

目前,我的游戏可以正确处理在内置 iPod 应用程序中播放音乐时禁用其自己的 BGM,但它没有检测到诸如 Pandora 之类的应用程序何时播放音乐。

目前,在我的applicationDidBecomeActive方法中,我检查[[MPMusicPlayerController iPodMusicPlayer] playbackState]以确定是否正在播放音乐。什么相当于检查像 Pandora 这样的应用程序是否在后台播放音频?

4

4 回答 4

25

AudioSessionGetProperty(如 jake_hetfield 的回答中所述)自 iOS 7 起已弃用。

相反,试试这个使用isOtherAudioPlaying的单线:

BOOL isOtherAudioPlaying = [[AVAudioSession sharedInstance] isOtherAudioPlaying];

适用于 iOS 6+。

于 2014-09-29T03:35:30.803 回答
14

看看这个问题

似乎您可以通过检查属性 kAudioSessionProperty_OtherAudioIsPlaying 来查看是否正在播放另一个音频,如下所示:

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

对此的补充可以是询问用户他/她是否想要游戏音乐或已经播放的声音/音乐。

于 2012-09-18T11:22:36.080 回答
4

从 iOS 8 开始,应该使用 secondaryAudioShouldBeSilencedHint 属性:

/* 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);
于 2016-03-10T23:25:54.873 回答
-1

你可能想做这样的事情......

  1. 创建一个类来处理您的音频设置说...“AudioManager”

  2. 轮询布尔“isOtherAudioPlaying”......也许将其分配给您自己的布尔值。

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

用法:

AudioManager.playSystemSoundIfBackgroundSoundIsOff()
于 2018-03-13T11:55:37.180 回答