从 Swift 3 中的 API 获取 AVPlayer 中的视频
Posted
技术标签:
【中文标题】从 Swift 3 中的 API 获取 AVPlayer 中的视频【英文标题】:Fetch video in AVPlayer from API in Swift 3 【发布时间】:2017-08-09 18:01:01 【问题描述】:我可以在 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】:如果您使用的是 JSON,您实际上不会发送视频。 JSON 本质上是 Key:Value 数据。您可以让 JSON 发送视频数据或下载视频的链接。但是目前看起来你有:
"course_video":"course.mkv"
换句话说,你有类型:
String:String
我建议通过 json 传递一个链接,然后下载相应的视频。看看Alamofire,它非常适合 HTTP 请求和处理您的下载。获得视频数据后,您可以使用 AVPlayer 进行播放。
【讨论】:
现在,我也包含了下一个视图控制器的内容。在这里,我使用 Alamofire 获取 API。我在上面写的关于视频的 JSON 是虚拟的。那是出于隐私考虑。现在,我的问题是如何在 Swift 3 中使用 AVPlayer 播放该视频?以上是关于从 Swift 3 中的 API 获取 AVPlayer 中的视频的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Swift 3.1 中的 API 创建解析数据,API 以括号开头和结尾
UISearchBar 从 Json (SWIFT 3) 获取数据