加载视图时如何异步加载MPMoviePlayerController contentUrl? [英] How to load MPMoviePlayerController contentUrl asynchronous when loading view?

查看:92
本文介绍了加载视图时如何异步加载MPMoviePlayerController contentUrl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用MPMoviePlayerController播放视频.设置是:我推一个新的ViewController,然后在viewDidLoad中设置视图和电影播放器​​实例,然后使用NSURLSession.sharedSession().dataTaskWithURL()在其中加载电影的REST资源以提供URL.在完成模块中,我将电影播放器​​实例的contentUrl设置为此URL,并说播放.但是,电影帧保持黑色.如果我在viewDidLoad,viewWillAppear或viewDidAppear中将contentUrl硬编码设置为url,则电影将正常播放. errorLog和accessLog均为零. 因此,我认为异步URL加载和电影contentUrl的分配有问题.

I try to play a video using a MPMoviePlayerController. The setup is: I push a new ViewController, then set up the view and the movie player instance in viewDidLoad and then use a NSURLSession.sharedSession().dataTaskWithURL() where I load the REST resource for the movie to give me the URL. In the completion block I set the contentUrl of the movie player instance to this url and say play. However, the movie frame stays black. If I set the contentUrl hardcoded to the url either in viewDidLoad, viewWillAppear, or viewDidAppear, the movie shows just fine. The errorLog and accessLog are both nil. So I assume something is wrong with the asynchronous url loading and assigning of the movie contentUrl.

设置:Swift,Xcode 6 beta,iOS 8.

Setup: Swift, Xcode 6 beta, iOS 8.

下面是一些代码段:

class PresentationsViewController {

    override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        let presentationViewController = PresentationViewController(presentations[indexPath.row])
        navigationController.pushViewController(presentationViewController, animated: true)
    }

}

class PresentationViewController {

    var presentation: Presentation?
    var moviePlayer: MPMoviePlayerController?

    convenience init(_ presentation: Presentation) {
        self.init()
        self.presentation = presentation
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        moviePlayer = MPMoviePlayerController()
        moviePlayer!.view.frame = CGRect(x: X, y: Y, width: W, height: H)
        moviePlayer!.movieSourceType = MPMovieSourceType.Unknown
        moviePlayer!.controlStyle = MPMovieControlStyle.Embedded

        NSURLSession.sharedSession().dataTaskWithURL(presentation.url) { data, response, error in
            // Some JSON parsing etc.

            self.moviePlayer!.contentURL = presentation.videoUrl
            self.moviePlayer!.prepareToPlay()
            self.moviePlayer!.play()
        }.resume()

        view.addSubview(moviePlayer.view)

    }
}

推荐答案

我不确定这是否是Swift beta或iOS 8 beta中的错误,但是更改代码以使用AVPlayer是可行的.

I am not sure if this was a bug in the Swift betas or iOS 8 betas, but changing the code to use AVPlayer worked.

import AVFoundation
import AVKit

let playerViewController = AVPlayerViewController()

// In async block:

if let player = AVPlayer.playerWithURL(url) as? AVPlayer {
    playerViewController.player = player
}

这篇关于加载视图时如何异步加载MPMoviePlayerController contentUrl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆