无法使用带有 Swift 的 AVPlayer 播放视频
Unable to play video using AVPlayer with Swift
当我点击播放视频时,显示如下。想知道为什么。没有显示错误。我确实导入了 AVKit 和 AVFoundation。另外,我在我的设备和模拟器中进行了测试。仍然不起作用。有什么建议么?非常感谢。
**Print log**
//<PFFile: 0x79d472f0>
//https://files.parsetfss.com/d9cdffff-4a1f-4bd9-b6ee-e60c07d49236/tfss-7e1636b9-5701-4737-9fa6-10609aec89e8-adsVido
//<AVPlayer: 0x786f39b0>
// Click cell to play video.
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if adsVideoArray[indexPath.row] != nil {
let theVideo:PFFile = adsVideoArray[indexPath.row]!
print(theVideo)
let url:NSURL = NSURL(string: theVideo.url!)!
theVideo.getDataInBackgroundWithBlock({ (data:NSData?, error: NSError?) -> Void in
if error == nil {
let player = AVPlayer(URL: url)
print(url)
print(player)
let playerController = AVPlayerViewController()
playerController.player = player
self.presentViewController(playerController, animated: true) {
player.play()
}
}
})
}
}
// load data from parse
func loadAdsFromParse() {
adsVideoArray.removeAll(keepCapacity: false)
let query = PFQuery(className: "user_ads")
query.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in
for object in objects! {
self.adsVideoArray.append(object.objectForKey("video") as? PFFile)
}
dispatch_async(dispatch_get_main_queue()) {
self.adsCollectionView.reloadData()
}
})
}
// the way to save video
self.tempVideo = info[UIImagePickerControllerMediaURL] as! NSURL!
let videoData = NSData(contentsOfFile:self.tempVideo.relativePath!)
let videoFile:PFFile = PFFile(name:"adsVido", data:videoData!)!
detailsObj["video"] = videoFile
detailsObj.saveInBackgroundWithBlock{ ... }
// I tried to play the video before upload it to Parse. It works.
func playImageTapped(pressed: UIGestureRecognizer) {
let player = AVPlayer(URL: tempVideo)
let playerController = AVPlayerViewController()
playerController.player = player
self.presentViewController(playerController, animated: true) {
player.play()
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
self.tempVideo = videoAdded
您是否查看并阅读了有关此内容的文档...
https://parse.com/docs/osx/api/Classes/PFFile.html
您确定将数据保存为电影而不是图像吗?一个疯狂的猜测?
我好像记得一篇新文章说 parse.com 正在关闭,所以无论如何使用它可能不是一个好主意...
当我点击播放视频时,显示如下。想知道为什么。没有显示错误。我确实导入了 AVKit 和 AVFoundation。另外,我在我的设备和模拟器中进行了测试。仍然不起作用。有什么建议么?非常感谢。
**Print log**
//<PFFile: 0x79d472f0>
//https://files.parsetfss.com/d9cdffff-4a1f-4bd9-b6ee-e60c07d49236/tfss-7e1636b9-5701-4737-9fa6-10609aec89e8-adsVido
//<AVPlayer: 0x786f39b0>
// Click cell to play video.
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if adsVideoArray[indexPath.row] != nil {
let theVideo:PFFile = adsVideoArray[indexPath.row]!
print(theVideo)
let url:NSURL = NSURL(string: theVideo.url!)!
theVideo.getDataInBackgroundWithBlock({ (data:NSData?, error: NSError?) -> Void in
if error == nil {
let player = AVPlayer(URL: url)
print(url)
print(player)
let playerController = AVPlayerViewController()
playerController.player = player
self.presentViewController(playerController, animated: true) {
player.play()
}
}
})
}
}
// load data from parse
func loadAdsFromParse() {
adsVideoArray.removeAll(keepCapacity: false)
let query = PFQuery(className: "user_ads")
query.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in
for object in objects! {
self.adsVideoArray.append(object.objectForKey("video") as? PFFile)
}
dispatch_async(dispatch_get_main_queue()) {
self.adsCollectionView.reloadData()
}
})
}
// the way to save video
self.tempVideo = info[UIImagePickerControllerMediaURL] as! NSURL!
let videoData = NSData(contentsOfFile:self.tempVideo.relativePath!)
let videoFile:PFFile = PFFile(name:"adsVido", data:videoData!)!
detailsObj["video"] = videoFile
detailsObj.saveInBackgroundWithBlock{ ... }
// I tried to play the video before upload it to Parse. It works.
func playImageTapped(pressed: UIGestureRecognizer) {
let player = AVPlayer(URL: tempVideo)
let playerController = AVPlayerViewController()
playerController.player = player
self.presentViewController(playerController, animated: true) {
player.play()
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
self.tempVideo = videoAdded
您是否查看并阅读了有关此内容的文档...
https://parse.com/docs/osx/api/Classes/PFFile.html
您确定将数据保存为电影而不是图像吗?一个疯狂的猜测?
我好像记得一篇新文章说 parse.com 正在关闭,所以无论如何使用它可能不是一个好主意...