如何使用Swift 2中的AVPLayer从远程URL播放视频

时间:2016-06-09 20:23:50

标签: swift swift2 avplayer avplayerviewcontroller

我试图将AVPlayer用于视频文件,我在googleDrive,dropbox,riouxsvn和github的公共文件夹中上传了一些视频......没有一个可以播放。

否则我发现有效的样本,我发现有效的视频是可以在浏览器中自动播放的视频。

播放器不是真正的问题......这个问题与视频更相关。如果我从可以播放的那些网址中删除任何网址,那么视频会立即在浏览器中显示...如果我在无法播放的网址上显示您将看到视频无法启动,我必须将其播放( dropbox和google drive)或者在riouxsvn视频开始下载。

所以我不知道,我在互联网上找不到如何解决这个问题。您是否知道我应该如何以及在何处上传视频以便在设备上播放?

@IBAction func playVideoTest(sender: AnyObject) {

    //Video URLs that can be played
    //let url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
    //let url:NSURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!

    //Video URLs that can NOT be played
    let url:NSURL = NSURL(string: "https://svn.riouxsvn.com/salsa4allmedia/Videos/SampleVideo.mp4")!
    //let url:NSURL = NSURL(string: "https://www.dropbox.com/s/kkjwajuo7r1mqox/SampleVideo.mp4?dl=0")!
    //let url:NSURL = NSURL(string: "https://drive.google.com/open?id=0B1tAk1RMaHu0T1dVNWFYS1ExS0k")!
    //let url:NSURL = NSURL(string: "https://github.com/dervatii/SalsaMedia/blob/master/SampleVideo.MOV")!

    var player = AVPlayer()

    player = AVPlayer(URL: url)
    let playerController = AVPlayerViewController()
    playerController.player = player

    self.presentViewController(playerController, animated: true) {
        player.play()
    }
}

Check the result showed in the simulator here

1 个答案:

答案 0 :(得分:-1)

在swift 3中

   @IBAction func playVideo(_ sender: AnyObject) {

                let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
                let player = AVPlayer(url: videoURL! as URL)
                let playerLayer = AVPlayerLayer(player: player)
                playerLayer.frame = vedioPlayer.bounds
                vedioPlayer.layer.addSublayer(playerLayer)
                player.play()
}