gpt4 book ai didi

ios - AVAudioSession 在系统声音之上播放

转载 作者:搜寻专家 更新时间:2023-11-01 06:43:38 24 4
gpt4 key购买 nike

嘿,下面是这段代码。我只是想得到它,以便如果他们是任何音乐、播客等正在从操作系统内的另一个应用程序播放,所有声音都会被静音。但是我已经尝试设置它的类别,但它不起作用!我没有使用正确的类别吗?也许不在正确的地方?

目标是用户可以在使用该应用程序时收听自己的音乐。

sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("GameMusic9", ofType: "mp3")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("GameMusicRP", ofType: "mp3")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Death", ofType: "mp3")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Tap", ofType: "wav")!)!)

//Monsters
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Blue", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Brown", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Green", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Orange", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Purple", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Red", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Tan", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Yellow", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Correct", ofType: "wav")!)!)//12
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Cowbell", ofType: "aiff")!)!)//13
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("12", ofType: "wav")!)!)//14
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Hybrid", ofType: "mp3")!)!)//15
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Correct", ofType: "wav")!)!)//16

AVAudioSession.sharedInstance().setActive(true, error: nil)

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)

//Close or punch, or bycycle

var error:NSError?
for var i:Int = 0; i < sounds.count; i++
{
audioPlayers.append(AVAudioPlayer(contentsOfURL: sounds[i], error: &error))

audioPlayers.last?.prepareToPlay()
}

最佳答案

不,您没有使用正确的类别。来自docs :

AVAudioSessionCategoryAmbient

The category for an app in which sound playback is nonprimary—that is, your app can be used successfully with the sound turned off.

This category is also appropriate for “play along” style apps, such as a virtual piano that a user plays while the Music app is playing. When you use this category, audio from other apps mixes with your audio. Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).

尝试AVAudioSessionCategoryPlayback:

AVAudioSessionCategoryPlayback

The category for playing recorded music or other sounds that are central to the successful use of your app.

When using this category, your app audio continues with the Silent switch set to silent or when the screen locks. (The switch is called the Ring/Silent switch on iPhone.) To continue playing audio when your app transitions to the background (for example, when the screen locks), add the audio value to the UIBackgroundModes key in your information property list file.

By default, using this category implies that your app’s audio is nonmixable—activating your session will interrupt any other audio sessions which are also nonmixable. To allow mixing for this category, use the AVAudioSessionCategoryOptionMixWithOthers option.

或者尝试 AVAudioSessionCategorySoloAmbient,它类似于 AVAudioSessionCategoryAmbient,但默认情况下不可混合。

编辑:我误解了这个问题。目标是仅在其他应用程序未播放任何内容时才播放音乐。这可以通过检查共享的 AVAudioSession 来实现:

[[AVAudioSession sharedInstance] isOtherAudioPlaying];

只有在返回 NO 时才播放您的音乐。请注意,从 iOS 8.0 开始,最好使用另一个属性(它们的语义略有不同):

[[AVAudioSession sharedInstance] secondaryAudioShouldBeSilencedHint];

The value is YES when another application with a non-mixable audio session is playing audio.

Applications should use this property as a hint to silence audio that is secondary to the functionality of the application. For example, a game using AVAudioSessionCategoryAmbient can use this property to decide to mute its soundtrack while leaving its sound effects unmuted.

您还应该订阅 AVAudioSessionSilenceSecondaryAudioHintNotification 通知并停止/开始您的音乐以反射(reflect) secondaryAudioShouldBeSilencedHint 中的变化。

NSNotificationCenter.defaultCenter().addObserverForName(AVAudioSessionSilenceSecondaryAudioHintNotification, object: nil, queue: nil) { notification in
// stat/stop music based on AVAudioSession.sharedInstance().secondaryAudioShouldBeSilencedHint
}

关于ios - AVAudioSession 在系统声音之上播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32874204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com