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

Community
  • 1
  • 1
  • 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? – Curmudgeonlybumbly Apr 26 '16 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 Apr 26 '16 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 –  Apr 26 '16 at 16:58

1 Answers1

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()
Lena Bru
  • 13,521
  • 11
  • 61
  • 126
Olga Nesterenko
  • 1,324
  • 11
  • 15
  • still just an empty view :/ –  Apr 26 '16 at 18:39
  • something may be wrong with your video url. I tried this one - http://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. – Olga Nesterenko Apr 26 '16 at 18:52
  • a dumb question, i should do this in my viewdidload? like: override func viewDidLoad() { super.viewDidLoad() let urlPath: String = "http://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() } –  Apr 26 '16 at 18:59
  • yes, it's ok to do this in viewDidLoad. btw i'm sorry, i have forgotten http:// in my link – Olga Nesterenko Apr 26 '16 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. –  Apr 26 '16 at 19:06
  • do you have any other views on your view? do you have NSAppTransportSecurityNSAllowsArbitraryLoads in your Info.plist? – Olga Nesterenko Apr 26 '16 at 19:17
  • I tried in a new project, just file -> new-> project.. and there i tried to paste into viewdidload to see if it works. but not even there –  Apr 26 '16 at 19:22
  • ok, so if I create a new project, and play the previous recorded video that works with the MPMoviePlayerController in the viewdidload like this: let urlPath: String = "file:///private/var/mobile/Containers/Data/Application/B98B09BE-8B4C-447E-B60B-E1FDD0179485/tmp/tempMovie.mp4" let url: NSURL = NSURL(string: urlPath)! let player = AVPlayer(URL: url) let avPlayerLayer:AVPlayerLayer = AVPlayerLayer(player: player) avPlayerLayer.frame = view.frame self.view.layer.addSublayer(avPlayerLayer) player.play() it works, but not in my previous proj –  Apr 26 '16 at 19:33
  • it means that smth is wrong with your previous project. look at subviews of your root view carefully, maybe something is convering your player? – Olga Nesterenko Apr 26 '16 at 19:54