gpt4 book ai didi

ios - 防止 AVPlayer 取消背景音频

转载 作者:搜寻专家 更新时间:2023-10-31 08:10:56 25 4
gpt4 key购买 nike

在我的应用程序中,我使用 AVPlayer 播放简单的视频。这些视频没有任何音轨。问题是当我播放它们时,所有背景音乐(不是来 self 的应用程序)都停止了。我该如何防止这种情况?

有没有办法用AVPlayer播放视频不取消背景音乐?

我的代码是:

let urlAsset = AVURLAsset(URL: urlLocal, options: nil)
let item = AVPlayerItem(asset: urlAsset)
self.videoPlayer = AVPlayer(playerItem: item)

if let player = self.videoPlayer {
self.videoLayer = AVPlayerLayer(player: player)
if let layer = self.videoLayer {

layer.frame = self.view.bounds
self.view.layer.addSublayer(layer)

NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoPlayerDidReachEnd:", name: AVPlayerItemDidPlayToEndTimeNotification , object: nil)

player.actionAtItemEnd = AVPlayerActionAtItemEnd.None
player.play()
}
}

谢谢!

最佳答案

带有一些错误处理的 Swift 3:

do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
} catch let error as NSError {
print(error)
}

do {
try AVAudioSession.sharedInstance().setActive(true)
} catch let error as NSError {
print(error)
}

关于ios - 防止 AVPlayer 取消背景音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31671029/

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