视频无法播放NSURLSession

时间:2015-09-22 07:56:13

标签: ios iphone mpmovieplayercontroller

我正在从网址抓取视频,然后我尝试播放它,但是在NSURLSession.sharedSession().dataTaskWithURL内,我无法播放视频

var url:NSURL = NSURL(string: "http://example.com/format=xml")!
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { data, response, error in
        if error != nil {
            println(error)
            return
        }
        var error: NSError?
        if let xmlDoc = AEXMLDocument(xmlData: data, error: &error) {

            // prints the same XML structure as original
           // println(xmlDoc.xmlString)

           println(xmlDoc.root["Ad"]["InLine"]["Creatives"]["Creative"]["Linear"]["MediaFiles"]["MediaFile"].value)
           self.mediaData = xmlDoc.root["Ad"]["InLine"]["Creatives"]["Creative"]["Linear"]["MediaFiles"]["MediaFile"].value
self.videroPlay(self.mediaData)
            }
    }
    task.resume()

//Function for Video Play

func videroPlay(url:String)
{

    var nsUrl:NSURL = NSURL(string: url)!
    self.moviePlayer = MPMoviePlayerController()
    self.moviePlayer.movieSourceType = MPMovieSourceType.Streaming
    self.moviePlayer = MPMoviePlayerController(contentURL: nsUrl)

    self.moviePlayer.prepareToPlay()
    self.moviePlayer.shouldAutoplay = true

    self.moviePlayer.stop()
    self.moviePlayer.view.frame = self.view.bounds
    self.view.addSubview(self.moviePlayer.view)
    self.moviePlayer.controlStyle = MPMovieControlStyle.Embedded
    self.moviePlayer.scalingMode = MPMovieScalingMode.AspectFit
    self.moviePlayer.play()
}

它会保持黑色或视频播放结束。

1 个答案:

答案 0 :(得分:0)

我是这样做的:

dispatch_async(dispatch_get_main_queue(),{
            self.videroPlay(self.mediaData)
        })