将swift 2视频代码转换为swift 3视频代码

时间:2016-10-25 20:42:50

标签: swift video swift2 swift3 avkit

以下代码显示视频。它在swift 2中工作得很完美,但在快速3 xcode状态下,MPMoivePlayerController代码已逐步淘汰。我只是希望我的视频能像swift 2一样在swift 3中显示。

import UIKit 
import AVFoundation
import AVKit
import MediaPlayer


class video: UIViewController {

var moviePlayer : MPMoviePlayerController!


   override func viewDidLoad() {
    super.viewDidLoad()


    let path = Bundle.main.path(forResource: "jxdo", ofType:"mp4")
    let url = URL(fileURLWithPath: path!)
    self.moviePlayer = MPMoviePlayerController(contentURL: url)
    if let player = self.moviePlayer {
        player.view.frame = CGRect(x: 67, y: 75, width: self.view.frame.size.width/2, height: self.view.frame.size.height / 5)
        player.view.sizeToFit()
        player.scalingMode = MPMovieScalingMode.aspectFit
        player.isFullscreen = false
        player.controlStyle = MPMovieControlStyle.default
        player.movieSourceType = MPMovieSourceType.file
        player.repeatMode = MPMovieRepeatMode.none

        self.view.addSubview(player.view)

1 个答案:

答案 0 :(得分:1)

不推荐使用MPMoviePlayerController,而应使用AVPlayerViewController。有关Apple文档页面https://developer.apple.com/reference/mediaplayer/mpmovieplayercontroller

的更多信息,请参阅

此线程也可能有助于开始转换为较新的api:How to load MPMoviePlayerController contentUrl asynchronous when loading view?