【问题标题】:AVPlayerViewController and AVPlayer in swift doesn't show anythingswift中的 AVPlayerViewController 和 AVPlayer 不显示任何内容
【发布时间】:2015-12-18 11:11:21
【问题描述】:

我正在尝试使用 AVPlayerAVPlayerViewController 中的文档路径播放 mp4 视频,但屏幕上没有显示任何内容。我做错了什么?

这里是模拟器的截图:http://postimg.org/image/xma60nfrf/

这是我的代码:

func startPlayer(filePath:String)
{
    let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask, true)
    let docsDir = dirPaths[0]


    let filePath = (docsDir as NSString).stringByAppendingPathComponent(videos[videoNo]._Name)

    playerVC=AVPlayerViewController()
    playerVC!.view.frame=self.view.bounds
    playerVC!.view.layer.zPosition = 1;


    var videoURL = NSURL(string: filePath)
    var player = AVPlayer(URL: videoURL!)
    var playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = playerVC!.view.bounds
    playerVC!.view.layer.addSublayer(playerLayer)

    playerVC!.player=player
    self.view.addSubview(playerVC!.view)

    playerVC!.player!.play()
}

【问题讨论】:

    标签: ios swift mp4 avplayer avplayerviewcontroller


    【解决方案1】:

    使用AVAsset 作为播放器的来源。

    let currentVideoAsset = AVAsset(URL: outputFileURL)
    
    
    self.player = AVPlayer(playerItem: AVPlayerItem(asset:currentVideoAsset))
    

    【讨论】:

      【解决方案2】:

      找到了,换了

      var videoURL = NSURL(string: filePath)
      

      有了这个

      var videoURL = NSURL(fileURLWithPath: filePath)
      

      并且正在工作..

      【讨论】:

        猜你喜欢
        • 2014-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-29
        • 2020-11-10
        相关资源
        最近更新 更多