gpt4 book ai didi

ios - AVAudioSession overrideOutputAudioPort & localNotifications 静音

转载 作者:行者123 更新时间:2023-11-29 02:14:27 28 4
gpt4 key购买 nike

您好,我尝试了类似问题的各种解决方案,但无法让我的代码正常工作。我在我的应用程序中调用了以下函数:

func PlaySound (WhenToPlaySound:String) {

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

if WhenToPlaySound == "BeginningOfRound" {

if UIApplication.sharedApplication().applicationState == UIApplicationState.Background {

soundnotification.soundName = "BoxingBellStart.wav"

UIApplication.sharedApplication().scheduleLocalNotification(soundnotification)

println("timer is done in background mode")

} else {

// Load Sound
soundlocation = NSBundle.mainBundle().URLForResource("BoxingBellStart", withExtension: "wav")!
player = AVAudioPlayer(contentsOfURL: soundlocation, error: &Error)
player.volume = 1.0

// Play Sound
player.play()

println("timer is done in active mode")

}

} else {

if UIApplication.sharedApplication().applicationState == UIApplicationState.Background {

soundnotification.soundName = "Boxing.wav"

UIApplication.sharedApplication().scheduleLocalNotification(soundnotification)

println("timer is done in background mode")

} else {

// Load Sound
soundlocation = NSBundle.mainBundle().URLForResource("Boxing", withExtension: "wav")!
player = AVAudioPlayer(contentsOfURL: soundlocation, error: &Error)
player.volume = 1.0

// Play Sound
player.play()

println("timer is done in active mode")

}

}
}

除了两件事,它大部分都有效:

<罢工>1。我似乎无法覆盖扬声器音量。我希望在播放声音之前将系统音量调到最大。

  1. 设置为在应用处于后台模式时激活的 LocalNotifications 仅在设备未静音时支付。

为了解决第一个问题,我想做以下但不知道如何使用它:

AVAudioSession.sharedInstance().overrideOutputAudioPort(<#portOverride: AVAudioSessionPortOverride#>, error: <#NSErrorPointer#>) 

提前致谢,艾斯

最佳答案

overrideOutputAudioPort 影响音频路由,而不是音量。

当您说您想要“覆盖扬声器音量”时,我假设您的意思是您想要通过应用程序代码控制系统输出音量。这是不可能的,因为 Apple 认为输出音量应始终由用户控制。

AVAudioPlayer 的 volume 属性将音量相对设置为系统输出级别。默认为 1.0(播放器音量 == 系统音量)。你不能把它调高,像脊椎穿刺一样,到 1.1...

另请参阅我的回答 here ...如果你想控制系统音量,你将需要 MPVolumeView 提供的用户界面。

与您的通知类似 - 如果用户将设备静音,您的应用将无法忽略它。

更新
关于通知,它并不像我建议的那么简单。如果您将 AVAudioSession 类别设置为 AVAudioSessionCategoryPlayback (和 read the apple docs on this setting ),它可能会起作用。

When using this category, your app audio continues with the Silent switch set to silent or when the screen locks

您可能还需要将“音频”添加到 info.plist 中的 UIBackgroundModes

关于ios - AVAudioSession overrideOutputAudioPort & localNotifications 静音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28901374/

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