使用Swift 3中的AVPlayer通过API从服务器获取视频

时间:2017-08-11 11:27:56

标签: swift3 alamofire avplayer

我正在尝试从AVPlayer中的服务器获取视频。数据采用JSON格式。我也给出了JSON格式的数据。该视频采用.mp4格式。

但视频无法播放。但在我尝试从.mp4的本地存储播放视频之前,它在AVPlayer中成功播放。我的代码有什么问题?我正在使用Swift 3。

我的代码是:

//的ViewController

import UIKit
import AVFoundation
import AVKit
import Alamofire


class ViewController: UIViewController {

var playerController = AVPlayerViewController()
var player:AVPlayer?
var playerController1 = AVPlayerViewController()
var player1:AVPlayer?
var dictDataImage:NSArray = NSArray()
var dictDataVideo: NSArray = NSArray()
var appDictionary:NSDictionary!



   @IBOutlet var videoPreviewLayer2: YTPlayerView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.videos()

    videoPreviewLayer2.delegate = self
}




 func videos(){

    let collectionviewone: String = "http://rillmark.academy/api/rillmark-academy/home-page-video"
    Alamofire.request(collectionviewone, method: .get, parameters: nil, encoding: JSONEncoding.default)
        .responseJSON { response in
            debugPrint(response.result)
            if let JSON = response.result.value{

                self.appDictionary = (JSON as AnyObject) as! NSDictionary

                print("self.app =",self.appDictionary)


                let url1: URL = URL(string: (self.appDictionary.value(forKey: "home_video")) as! String)!


                let url4 = url1.absoluteString

                print("url11=", url1)
                print("url22=", url4)

                let url3 = NSURL(fileURLWithPath: url4)
                let item = AVPlayerItem(url: url3 as URL)
                print("item=", item)
                self.player = AVPlayer(playerItem: item)
                self.playerController = AVPlayerViewController()
                self.playerController.player = self.player
                self.playerController.view.frame = self.videoPreviewLayer.frame
                self.videoPreviewLayer.addSubview(self.playerController.view)
                self.playerController.showsPlaybackControls = false
                self.playerController.player?.play()

            }
    }

}
}

// JSON

{
"status": "Success",
"home_video": "http://video/homepage/e27U0B_1502445369-homepage-2.mp4",
}

0 个答案:

没有答案