我无法在swift中从本地服务器加载视频文件

时间:2016-05-13 02:17:42

标签: ios swift avplayer nsurl avplayerviewcontroller

我正在使用Swift使用iOS。 我想在Swift中使用AVPlayer加载视频文件 我能够从本地服务器加载图像和文本文件。但我无法加载视频文件。这是我正在使用的代码。

let movieUrl = NSURL(string: "http://akjfsjf/ajdfl/ajkf/car.mp4")
let player = AVPlayer(URL: movieUrl!)
let playerController = AVPlayerViewController()

playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame

player.play()

2 个答案:

答案 0 :(得分:0)

我尝试过关注URLfunction

func playvideo (){

        let movieUrl = NSURL(string: "http://www.w3schools.com/html/movie.mp4")
        let player = AVPlayer(URL: movieUrl!)
        let playerController = AVPlayerViewController()
        playerController.player = player
        player.play()
        self.addChildViewController(playerController)
        self.view.addSubview(playerController.view)
        playerController.view.frame = self.view.frame
    }

同样来自code我发现您使用的是http而不是https,因此您需要在property文件中添加以下plist

enter image description here

答案 1 :(得分:0)

试试这个,

import UIKit
import AVKit
import AVFoundation


class ViewController: UIViewController {

var playerVC : AVPlayerViewController!
var playerItem : AVPlayerItem!
var player : AVPlayer!
var playerLayer: AVPlayerLayer!

override func viewDidAppear(animated: Bool) {

            let url = NSURL.init(fileURLWithPath: "your file path")

            self.playerItem = AVPlayerItem.init(URL: url!)
            self.player = AVPlayer.init(playerItem: self.playerItem)
            self.playerVC = AVPlayerViewController.init();
            self.playerVC.player = self.player;
            self.player.currentItem!.playbackLikelyToKeepUp

            self.presentViewController(self.playerVC, animated: true) { () -> Void in
                self.playerVC.player?.play()
       }

    }
 }

希望这有帮助!