18

現在、内蔵のiPodアプリで音楽を再生しているときに、ゲームで独自のBGMの無効化を正しく処理していますが、Pandoraなどのアプリが音楽を再生しているときは検出されません。

現在、私のapplicationDidBecomeActive方法では、[[MPMusicPlayerController iPodMusicPlayer] playbackState]音楽が再生されているかどうかを確認しています。Pandoraのようなアプリがバックグラウンドでオーディオを再生しているかどうかを確認するためのこれに相当するものは何ですか?

4

4 に答える 4

25

AudioSessionGetProperty(jake_hetfieldの回答に記載されているように)iOS7では非推奨になっています。

代わりに、isOtherAudioPlayingを使用するこのワンライナーを試してください:

BOOL isOtherAudioPlaying = [[AVAudioSession sharedInstance] isOtherAudioPlaying];

iOS6以降で動作します。

于 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 に答える