Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 加载AVPlayer时获取错误线程1:EXC_错误访问(代码=EXC_I386_GPFLT)_Ios_Swift_Xcode_Avplayer_Appdelegate - Fatal编程技术网

Ios 加载AVPlayer时获取错误线程1:EXC_错误访问(代码=EXC_I386_GPFLT)

Ios 加载AVPlayer时获取错误线程1:EXC_错误访问(代码=EXC_I386_GPFLT),ios,swift,xcode,avplayer,appdelegate,Ios,Swift,Xcode,Avplayer,Appdelegate,当我选择collectionViewCell时,我正在尝试加载AVPlayer,以下是我在didSelectItem中的代码: func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { if let item = items?[indexPath.item] { showPlayer(item: item) } } func s

当我选择collectionViewCell时,我正在尝试加载AVPlayer,以下是我在
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文件:

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(因为我在其中添加了打印语句),这就是它打印的内容:

http://xample.com/play.m3u?id=12345
(lldb)
然后它崩溃并在AppDelegate中显示
线程1:EXC\u BAD\u访问(code=EXC\u I386\u GPFLT)
错误。我怎样才能修好它

谢谢

更新:

我稍微更改了
didSelectItem
func。代码如下:

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)
}
以及
播放机启动
中所需的更改:

let playerView = PlayerView(frame: playerFrame, item: item)
view.addSubview(playerView)
我在
let playerView
行上加了一个分隔符,我可以看到传递到那里的项目Id。但是它没有传递到
PlayerView
类中的urlString。因此,ID仍然是

,看起来是这样的

class PlayerLauncher: NSObject
无法参考

let playerView = PlayerView()

尝试定义函数范围之外的对象,以便函数可以更改它们,并在函数之外引用它们。

我制作了一个指南,希望能说明您误解的地方

我向其他读者道歉,不使用减价文本格式,但请考虑这是我最原始的海报的尝试。在上一篇文章中,我们已经进行了充分的讨论

那么,你如何解决这个问题? 我想直接给你代码没有什么好处,请尝试根据这些信息修复代码


如果您还有更多问题,我会尽力帮助您:)

我删除了
setupPlayer()
func,并在
override init
中编写了AVPlayer代码。它现在不会崩溃,但是
itemId
现在是空的,它什么也不播放。只需在initBro之后/期间将另一个变量传递给任何需要它的对象,您仍然会犯同样的错误。我不知道如何让你明白这一点。。。但我会再试一次。嘿!我终于修好了!谢谢你的帮助!:D