我正在使用AVPlayer
播放视频,它会阻止iPhone的背景音乐。请帮我解决
let item1 = AVPlayerItem.init(URL: NSURL(string:path))
player = AVPlayer(playerItem: item1)
layer?.player = player;
player?.play()
我的电影是为了动画;他们没有声音。为了让其他声音继续播放,在Swift:
// None of our movies should interrupt system music playback.
_ = try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: .default, options: .mixWithOthers)
感谢Marcus Adams的原始答案。
使用允许混合的category for the AVAudioSession,例如AVAudioSessionCategoryPlayback
和添加AVAudioSessionCategoryOptionMixWithOthers
。
来自文档:
- AVAudioSessionCategoryOptionMixWithOthers 将此会话中的音频与来自其他活动会话的音频混合。 仅在会话类别为AVAudioSessionCategoryPlayAndRecord或AVAudioSessionCategoryPlayback时有效。 (如果会话类别是AVAudioSessionCategoryAmbient,则隐含。) 如果您在使用此选项时激活会话,则应用的音频不会中断来自其他应用(例如音乐应用)的音频。如果不使用此选项(或可隐式混合的类别),则激活会话将中断其他不可混合的会话。 适用于iOS 6.0及更高版本。
在快速4.2请尝试这一个。
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, options: .mixWithOthers)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
根据Apple的说法
如果要继续播放音频,请在进入后台时断开AVPlayer实例与演示文稿的连接,并在返回前台时重新连接。
func applicationDidEnterBackground(_ application: UIApplication) {
// Disconnect the AVPlayer from the presentation when entering background
// If presenting video with AVPlayerViewController
playerViewController.player = nil
// If presenting video with AVPlayerLayer
playerLayer.player = nil
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Reconnect the AVPlayer to the presentation when returning to foreground
// If presenting video with AVPlayerViewController
playerViewController.player = player
// If presenting video with AVPlayerLayer
playerLayer.player = player
}
例如,使用AVPlayer
的ViewController
步骤1
启用背景模式用于图片中的音频,AirPlay和图片功能
第2步
AppDelegate.swift
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
print("Playback OK")
try AVAudioSession.sharedInstance().setActive(true)
print("Session is Active")
} catch {
print(error)
}
步骤:3
YourViewcontroller.swift
override func viewDidLoad() {
super.viewDidLoad()
addPlayerNotifications()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
removePlayerNotifations()
}
func addPlayerNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
}
func removePlayerNotifations() {
NotificationCenter.default.removeObserver(self, name:NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
NotificationCenter.default.removeObserver(self, name:NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
}
//App enter in forground.
@objc func applicationWillEnterForeground(_ notification: Notification) {
yourPlayerLayer.player = yourplayer
}
//App enter in forground.
@objc func applicationDidEnterBackground(_ notification: Notification) {
yourPlayerLayer.player = nil
}