Blazey John
Blazey John

Reputation: 47

Playing video Swift IOS 8

I am using this code to play video in

 dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.activityIndicator.stopAnimating()
            var url:NSURL = NSURL(string: self.WinVideos[0].url)!
            println(self.WinVideos[0].url)
            var player:AVPlayer!
            var playerItem:AVPlayerItem!;
            var avPlayerLayer:AVPlayerLayer = AVPlayerLayer(player: player)
            avPlayerLayer.frame = CGRect(x: 20, y: 100, width: 480, height: 480)
            self.view.layer .addSublayer(avPlayerLayer)
            player = AVPlayer(URL: url)
            player.play()
  })

However this doesn't seem to work in my phone (IOS 8.4) what could be the issue?

Upvotes: 1

Views: 495

Answers (2)

iOS Dev
iOS Dev

Reputation: 195

I am playing video from UIImagePickerController as follow:

        urlVideo = info.objectForKey(UIImagePickerControllerMediaURL) as NSURL

        objMoviePlayerController = MPMoviePlayerController(contentURL: urlVideo)
        objMoviePlayerController.movieSourceType = MPMovieSourceType.Unknown
        objMoviePlayerController.view.frame = self.view.bounds
        objMoviePlayerController.scalingMode = MPMovieScalingMode.AspectFill
        objMoviePlayerController.controlStyle = MPMovieControlStyle.Fullscreen
        objMoviePlayerController.shouldAutoplay = true

        self.view.addSubview(objMoviePlayerController.view)

        objMoviePlayerController.prepareToPlay()
        objMoviePlayerController.play()

Upvotes: 0

Özgür Ersil
Özgür Ersil

Reputation: 7013

Because before you init the player, you are passing to avPlayerLayer, it should be like that

dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.activityIndicator.stopAnimating()
            var url:NSURL = NSURL(string: self.WinVideos[0].url)!
            println(self.WinVideos[0].url)
            var player:AVPlayer!
            var playerItem:AVPlayerItem!;
            player = AVPlayer(URL: url)
            var avPlayerLayer:AVPlayerLayer = AVPlayerLayer(player: player)
            avPlayerLayer.frame = CGRect(x: 20, y: 100, width: 480, height: 480)
            self.view.layer.addSublayer(avPlayerLayer)
            player.play()
  })

Upvotes: 2

Related Questions