kAudioSessionProperty_OtherMixableAudioShouldDuck 用于允许将 iPod 音频与应用程序音频混合的 Audio Session 类别,指定当应用程序产生声音时是否应降低 iPod 音频的级别。默认情况下,此属性的值为 FALSE (0)。将其设置为非零值以打开闪避。 我们怎样才能解决这个问题呢?有属性(property)吗?
提前致谢, 钱德拉。
最佳答案
查看@zaphodtx 发布的示例代码,一种可能的解决方案是激活和停用当前 Audio Session 。
例如,在 Swift 中:
import UIKit
import AVFoundation
class MyController: UIViewController, AVAudioPlayerDelegate{
var audioPlayer: AVAudioPlayer?
func viewDidLoad(){
super.viewDidLoad()
configureAudioSession()
}
func configureAudioSession(){
let session = AVAudioSession.sharedInstance()
do{
try session.setCategory(AVAudioSessionCategoryPlayback, withOptions: [.DuckOthers])
} catch {
print(
"Unable to configure audio session, Attempting " +
"to activate or deactivate the audio session will "
"likely not meet your expectations."
)
return
}
print("Audio Session Configured")
}
func activateAudioSession(value: Bool){
let session = AVAudioSession.sharedInstance()
try? session.setActive(value)
}
func playAudioAtPath(path:String){
audioPlayer?.stop()
let url = NSURL(fileURLWithPath: path)
if let player = try? AVAudioPlayer(contentsOfURL: url){
activateAudioSession(true)
print("Playing AVAudioPlayer Sound from path: '\(path)'")
player.volume = volume
player.delegate = self
player.play()
audioPlayer = player
} else {
print("Failed to play AVAudioPlayer Sound from path: '\(path)'")
audioPlayer = nil
}
}
func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
activateAudioSession(false)
}
}
关于ios4 - 如何取消 AVAudioSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6489630/