加载AVPlayer时出现错误线程1:EXC_BAD_ACCESS(code = EXC_I386_GPFLT) [英] Getting error Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) when loading AVPlayer
问题描述
当我选择collectionViewCell时,我正在尝试加载AVPlayer,这是我在didSelectItem
中的代码:
I am trying to load an AVPlayer when I select a collectionViewCell, Here is my code in didSelectItem
:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let item = items?[indexPath.item] {
showPlayer(item: item)
}
}
func showPlayer(item: Item) {
let playerLauncher = PlayerLauncher()
playerLauncher.showVideoPlayer()
let playerView = PlayerView()
playerView.item = item
playerView.setupPlayerView()
}
这是我的PlayerLauncher文件:
This is my PlayerLauncher file:
class PlayerView: UIView {
var item: Item? {
didSet {
if let id = item?.itemId {
itemId = id
}
}
}
var itemId = String()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor(white: 0.3, alpha: 1)
}
var player: AVPlayer?
func setupPlayerView() {
let baseurl = "http://xample.com/play.m3u?id="
let urlString = "\(baseurl)\(itemId)"
print(urlString)
if let url = URL(string: urlString) {
player = AVPlayer(url: url)
let playerLayer = AVPlayerLayer(player: player)
self.layer.addSublayer(playerLayer)
playerLayer.frame = self.frame
let audioSession = AVAudioSession.sharedInstance()
do{
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
} catch let err {
print(err)
return
}
player?.play()
player?.addObserver(self, forKeyPath: "currentItem.status", options: .new, context: nil)
}
}
}
class PlayerLauncher: NSObject {
func showVideoPlayer() {
print("Showing the player...")
if let keyWindow = UIApplication.shared.keyWindow {
let view = UIView(frame: keyWindow.frame)
view.backgroundColor = UIColor.white
view.frame = CGRect(x: keyWindow.frame.width - 10, y: keyWindow.frame.height - 10, width: 10, height: 10)
let playerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.height)
let playerView = PlayerView(frame: playerFrame)
view.addSubview(playerView)
keyWindow.addSubview(view)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
view.frame = keyWindow.frame
}, completion: { (completedAnimation) in
//later...
})
}
}
}
因此,每当我选择该项目时,播放器便会开始加载,控制台会打印URL(因为我已经在其中添加了print语句),这就是它的打印内容:
So whenever I select the item, the player starts loading, console prints the URL (because I've added print statement there), This is what it prints:
http://xample.com/play.m3u?id=12345
(lldb)
,然后崩溃,并在AppDelegate中显示Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
错误.我该如何解决?
and then It crashes and shows Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
error in AppDelegate. How can I fix it?
谢谢.
更新:
我稍微更改了didSelectItem
函数.这是代码:
I changed the didSelectItem
func a bit. Here is the code:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let item = items?[indexPath.item] {
showPlayer(item: item)
}
}
func showPlayer(item: Item) {
let playerLauncher = PlayerLauncher()
playerLauncher.showPlayer(item: item)
}
我将PlayerView
中的init
方法更改为:
var item: Item? {
didSet {
if let id = item?.itemId {
itemId = id
}
}
}
init(frame: CGRect, item: Item) {
super.init(frame: frame)
self.item = item
setupPlayerView()
setupContainerView()
backgroundColor = UIColor(white: 0.3, alpha: 1)
}
以及PlayerLauncher
中所需的更改:
let playerView = PlayerView(frame: playerFrame, item: item)
view.addSubview(playerView)
我在let playerView
行上休息了一下,我看到了传递给它的项目ID.但是它并没有传递给PlayerView
类中的urlString.因此,ID仍为" "
.
I put a break on let playerView
line and I can see the item Id passed there. But it is not getting passed to the urlString in PlayerView
class. Hence, ID is still " "
.
推荐答案
我编写了一份指南,希望可以显示您的误会.
I've made a guide that hopefully shows what you've been misunderstanding.
对于其他读者未使用MARKDOWN文本格式,我深表歉意,但请确保这是我对原始海报的最大尝试.在上一篇文章中,我们已经进行了充分的讨论.
I apologize to other readers for not using MARKDOWN text format, but please do consider this as my upmost attempt for the original poster. We've already went through sufficient discussions in the previous post.
那么,您如何解决此问题? 我想直接给您提供代码没有什么好处,请尝试根据此信息修复代码.
So, how do you fix this? I guess there's nothing good to give you codes directly, please try to fix the code based on this information.
如果您还有其他问题,我会尽力为您提供帮助:)
And if you have more questions, I'll try my best to help you :)
这篇关于加载AVPlayer时出现错误线程1:EXC_BAD_ACCESS(code = EXC_I386_GPFLT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!