在Swift 3中从API获取AVPlayer中的视频

时间:2017-08-09 18:01:02

标签: ios json swift3 avplayer

我可以在AVPlayer中运行本地视频。但是我无法在swift 3中从JSON中获取视频。我搜索了它但在Swift 3中找不到合适的解决方案。如何从Swift 3中的JSON获取视频?我是Swift的初学者。

我有JSON:

 {
 "status": "Success",
 "course_summary": "<p>Laravel is one of the most popular PHP frameworks </p>",
 "course_video": "course.mkv"
}

代码:

import UIKit
import AVFoundation
import AVKit
import Alamofire

class CourseSchedule: UIViewController {

var playerController = AVPlayerViewController()
var player:AVPlayer?
var dictDataContent:NSDictionary = NSDictionary()
var strd : NSString?
var dictData:NSArray = NSArray()
var appDictionary: String?

@IBOutlet weak var courseSummary: UITextView!
@IBOutlet var button: UIButton!
@IBOutlet var videoPreviewLayer: UIView!

  override func viewDidLoad() {
    super.viewDidLoad()

    let def = self.dictDataContent.value(forKey: "course_summary") as! String!
    let htmlData = NSString(string: def!).data(using: String.Encoding.unicode.rawValue)
    let attributedString = try! NSAttributedString(data: htmlData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)

    courseSummary.attributedText = attributedString
    courseSummary.textColor = UIColor.white
    courseSummary.font = UIFont(name: "Palatino", size: 14)


    //For local video to play in AVPlayer.

    let videoString = Bundle.main.path(forResource: "Working-Space", ofType: ".mp4")
    if let path = videoString {
        let url = NSURL(fileURLWithPath: path)
        let item = AVPlayerItem(url: url as URL)
        self.player = AVPlayer(playerItem: item)
        self.playerController = AVPlayerViewController()
        self.playerController.player = self.player
        playerController.view.frame = videoPreviewLayer.frame
        self.addChildViewController(playerController)
        self.videoPreviewLayer.addSubview(playerController.view)
        playerController.showsPlaybackControls = false
        self.playerController.player?.play()
    }
    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem, queue: nil, using: { (_) in
        DispatchQueue.main.async {
            self.player?.seek(to: kCMTimeZero)
            self.player?.play()
        }
    })
    button.layer.cornerRadius = 5
}  
}




//HOMEVC

import UIKit
import Alamofire


class HomeVC: UIViewController {


@IBOutlet var test: UIView!
var strid : NSString?
var strd : NSString?
var dictData:NSDictionary = NSDictionary()
var strd2 : NSString?

override func viewDidLoad() {
    super.viewDidLoad()        
    self.setupPages()
  }


func setupPages() {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    var controllerArray: [UIViewController] = []
    let strinDuration:NSString = NSString(format:"http://rillmark.academy/api/rillmark-academy/course/course-content-detail?id=%@",strid!) as String as String as NSString
    print("strid",strid!)
    let collectionviewone: String = strinDuration as String
    Alamofire.request(collectionviewone, method: .get, parameters: nil, encoding: JSONEncoding.default)
        .responseJSON { response in
            debugPrint(response.result)
            if let JSON = response.result.value{
               self.dictData = (JSON as AnyObject) as! NSDictionary
                print("self.dictData",self.dictData)
                let firstVC = storyboard.instantiateViewController(withIdentifier: "CourseContentTable") as! CourseContentTable
                firstVC.title = "Course Content"
                firstVC.dictDataContent = self.dictData
    }  

1 个答案:

答案 0 :(得分:0)

如果你正在使用JSON,那么你实际上并没有发送视频。 JSON本质上是Key:Value数据。您可以让JSON发送视频数据或从中下载视频的链接。然而,目前你看起来有:

{"course_video":"course.mkv"}

换句话说,你有类型:

{String:String}

我建议通过json传递一个链接然后下载相应的视频。看看Alamofire它对HTTP请求和处理下载非常有用。获得视频数据后,您可以使用AVPlayer进行播放。