不推荐使用MPMoviePlayerController,现在是什么?

时间:2016-04-26 16:04:26

标签: ios swift mpmovieplayercontroller avplayer

所以当MPMoviePlayerController被弃用时,我一直在寻找解决方案。我目前的代码工作正常:

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

我录制了一个视频,一旦我完成录制,它将在新视图中使用上面的代码播放。但据我所知,在iOS9中不推荐使用MPMoviePlayerController,我找不到像上面的代码一样的任何解决方案。我知道我需要使用AVPlayer和AVPlayerViewController。

我尝试了this以及许多其他类似的解决方案。 但它不会像我在上面的代码中那样工作。

我只是想录制一段视频,并以新的视角播放,就像是一样。

请帮帮我

增加附加信息:

因此,当我尝试使用this(已接受的答案)或this solution(AVPlayer)进行修复时,视图为空。什么都没有显示出来。

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

如果我尝试使用我必须修改的AVPlayerViewController

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

发生这种情况,没有显示任何内容。 enter image description here

1 个答案:

答案 0 :(得分: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()

这里您将播放器添加到AVPlayerLayer后初始化。试试这样:

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