12

So I have been searching for a solution now when MPMoviePlayerController is getting deprecated. my current code works fine:

moviePlayer = MPMoviePlayerController(contentURL: receivedURL)
    moviePlayer!.movieSourceType = MPMovieSourceType.Unknown
    moviePlayer!.view.frame = view.bounds
    moviePlayer!.scalingMode = MPMovieScalingMode.AspectFill
    moviePlayer!.controlStyle = MPMovieControlStyle.None
    moviePlayer!.shouldAutoplay = true

    view.addSubview((moviePlayer?.view)!)
    moviePlayer?.setFullscreen(true, animated: true)
    moviePlayer!.play()

I record a video, and once I'm done recording, it will be played back with the code above in a new view. But as I understand MPMoviePlayerController is deprecated in iOS9 and I can't find any solution working like the code above. I understand I need to user AVPlayer and AVPlayerViewController.

I have tried this and many other solutions that looks like them. But it just won't work like I want it in the code above.

I just want to record a video, and have it played back in a new view, exactly like snapchat.

Please help me

ADDED ADDITIONAL INFO:

So when I try to fix it with this (the accepted answer) or this solution (AVPlayer), the view is blank. nothing is shown.

var player:AVPlayer!
    let avPlayerLayer:AVPlayerLayer = AVPlayerLayer(player: player)
    avPlayerLayer.frame = CGRectMake(50,50,100,100)
    self.view.layer.addSublayer(avPlayerLayer)
    player = AVPlayer(URL: receivedURL)
    player.play()

And If i try with the AVPlayerViewController which I had to modify:

let player = AVPlayer(URL: receivedURL)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    dispatch_async(dispatch_get_main_queue(), {
        self.presentViewController(playerViewController, animated: true) {
            playerViewController.player?.play()
        }
    })

this happens, where nothing is shown. enter image description here

3
  • 1
    Your linked answer seems like the solution. Expand on "But it just won't work like I want it in the code above." What exactly is the problem? Commented Apr 26, 2016 at 16:34
  • What issues are you running into when you're trying to use AVPlayerViewController? Your question doesn't make sense. What's "not working?"
    – JAL
    Commented Apr 26, 2016 at 16:35
  • Ok I will edit the question and add a picture :) but with the answered I linked, there is two problems the first one is that nothing is played, the view is totally blank the other problem is that a video player is shown but is grey. I will take print screens and add them with the code
    – user4545564
    Commented Apr 26, 2016 at 16:58

1 Answer 1

7
var player:AVPlayer!
let avPlayerLayer:AVPlayerLayer = AVPlayerLayer(player: player)
avPlayerLayer.frame = CGRect(x:50,y:50,width:100,height:100)
self.view.layer.addSublayer(avPlayerLayer)
player = AVPlayer(url: receivedURL)
player.play()

here you initialize your player AFTER adding it to AVPlayerLayer. try like this:

let player = AVPlayer(url: receivedURL)
let avPlayerLayer:AVPlayerLayer = AVPlayerLayer(player: player)
avPlayerLayer.frame = CGRect(x:50,y:50,width:100,height:100)
self.view.layer.addSublayer(avPlayerLayer)
player.play()
9
  • still just an empty view :/
    – user4545564
    Commented Apr 26, 2016 at 18:39
  • something may be wrong with your video url. I tried this one - clips.vorwaerts-gmbh.de/VfE_html5.mp4 - and everything works as excpected. remember that if your video url is not https://, you need to configure App Transport Security Policy. Commented Apr 26, 2016 at 18:52
  • a dumb question, i should do this in my viewdidload? like: override func viewDidLoad() { super.viewDidLoad() let urlPath: String = "clips.vorwaerts-gmbh.de/VfE_html5.mp4" let url: NSURL = NSURL(string: urlPath)! let player = AVPlayer(URL: url) let avPlayerLayer:AVPlayerLayer = AVPlayerLayer(player: player) avPlayerLayer.frame = CGRectMake(50,50,100,100) self.view.layer.addSublayer(avPlayerLayer) player.play() }
    – user4545564
    Commented Apr 26, 2016 at 18:59
  • yes, it's ok to do this in viewDidLoad. btw i'm sorry, i have forgotten http:// in my link Commented Apr 26, 2016 at 19:05
  • no its there, i think its just Stackoverflow removes it when pasted in here.. I did it like the code above in my viewdidload, but still blank.
    – user4545564
    Commented Apr 26, 2016 at 19:06