I have a playVideo() method in here:
func playVideo() {
let url = NSURL(string: "http://mtc.cdn.vine.co/r/videos_r2/20660994A41184015287536758784_SW_WEBM_14252654515363566800065.mp4?versionId=X5uVE9shOvYpw7z7.VnePPyihEfx_uWj")
//let url = NSURL(string: videoUrls[videoNumber])
//println(videoUrls[videoNumber])
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
player.view.center = self.view.center
player.prepareToPlay()
player.scalingMode = .AspectFill
player.controlStyle = .None
self.view.addSubview(player.view)
player.play()
}
}
When I directly call it from viewDidLoad it works without any problem. But when I tried to call it in shared session like that:
var task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { data,response,error -> Void in
if(error == nil)
{
var err :NSError?
var jsonResult : NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary!
var records: NSArray = (jsonResult["data"]?["records"]) as NSArray
for(var i=0;i<records.count;i++)
{
var strVideo = (records[i]["videoLowURL"] as String);
videoUrls.append(strVideo)
}
}
else
{
println("Error")
}
})
task.resume()
It shows only black screen and there is no sound or nothing else.
I know that it calls playVideo() method but it does not work right.
What could be the problem ?
This is from the top of my head but the completion handler is probably not called on the main thread. All UI stuff must be done on the main thread.
Dispatch to the main queue using:
dispatch_async(dispatch_get_main_queue()) {
self.playVideo()
}