加载AVPlayer

时间:2017-09-04 18:01:38

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

http://xample.com/play.m3u?id=12345
(lldb)

然后它崩溃并在AppDelegate中显示Thread 1: EXC_BAD_ACCESS (code=EXC_I386_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)
}

我将init中的PlayerView方法更改为:

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仍为" "

2 个答案:

答案 0 :(得分:5)

我已经制作了一份指南,希望能够展示您误解的内容。

我向其他读者道歉没有使用MARKDOWN文本格式,但请将此视为我对原始海报的最大尝试。我们之前的帖子已经进行了充分的讨论。

enter image description here

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

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

答案 1 :(得分:0)

看来

class PlayerLauncher: NSObject

无法参考

let playerView = PlayerView()

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