MPMoviePlayerController deprecated, what now?

纵然是瞬间 提交于 2019-12-04 02:56:33

问题


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.


回答1:


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()


来源:https://stackoverflow.com/questions/36870189/mpmovieplayercontroller-deprecated-what-now

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!