如何使用 avplayer 从文档目录播放视频文件
Posted
技术标签:
【中文标题】如何使用 avplayer 从文档目录播放视频文件【英文标题】:How to play video file from documentdirectory using avplayer 【发布时间】:2017-09-12 13:55:19 【问题描述】:我正在 ios 中创建视频播放器应用程序,如果我将 mp4 文件存储在捆绑资源中,或者如果我流式传输 url,它工作正常,但如果我将文件存储在文档目录中并且我尝试使用 avplayer 播放它不播放我应该对文档目录中的脱机文件进行不同的处理吗
代码:
let fileManager = FileManager()
let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appendingFormat("/myvideo.mp4"))
self.playVideo(filePath: destinationURLForFile.path)
func playVideo(filePath:String)
var playerItem = AVPlayerItem.init(url:URL.init(string: filePath)!)
var player = AVPlayer(playerItem: playerItem)
var playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.frame
self.view.layer.addSublayer(playerLayer)
player.play()
【问题讨论】:
【参考方案1】:试试这个,用 Swift 3.0 编写,
let fm = FileManager.default
let docsurl = try! fm.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let path = docsurl.appendingPathComponent("myvideo.mp4")
playVideo(filePath: path)
func playVideo(filePath:String)
var playerItem = AVPlayerItem(url: URL(fileURLWithPath: filePath)
var player = AVPlayer(playerItem: playerItem)
var playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.frame
self.view.layer.addSublayer(playerLayer)
player.play()
【讨论】:
两者一样没有区别 您错过了检索问题中文档路径的代码行。 将playVideo(filePath: path)
替换为playVideo(filePath: path.path)
【参考方案2】:
swift 3/4 100%%%%%%% 可行
录像机打开摄像机
@IBAction func RecordAction(_ sender: Any)
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
print("CameraAvailable")
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.mediaTypes = [kUTTypeMovie as String]
imagePicker.allowsEditing = false
imagePicker.showsCameraControls = true
self.present(imagePicker,animated: true, completion: nil)
else
print("CameraNotAvailable")
保存到文档目录
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
// recover video URL
let url = info[UIImagePickerControllerMediaURL] as? URL
// check if video is compatible with album
let compatible: Bool = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum((url?.path)!)
// save
if compatible
UISaveVideoAtPathToSavedPhotosAlbum((url?.path)!, self, nil, nil)
print("saved!!!! \(String(describing: url?.path))")
videopath = url //save url to send next function play video
dismiss(animated: true, completion: nil)
// error
func video(_ videoPath: String, didFinishSavingWithError error: Error?, contextInfo: UnsafeMutableRawPointer)
从文档目录播放视频
@IBAction func playvideo(_ sender: Any)
let player = AVPlayer(url: videopath!) // video path coming from above function
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true)
playerViewController.player!.play()
【讨论】:
【参考方案3】:我尝试了包括上述在内的几种方法,但发现每次启动应用程序时文档路径都会更改,这会更改url的第一部分。
这有助于我获取文件的当前位置
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let playerItem = AVPlayerItem(url: URL(fileURLWithPath: documentsPath.appendingFormat("/\(clipId).mp4")))
player = AVPlayer(playerItem: playerItem)
希望这对某人有所帮助
【讨论】:
它工作,这实际上很聪明,你帮助了我,谢谢你我看到你有 clipId,我猜你正在制作一个视频应用程序,无论如何我试着给你帮助,你呢想要一个带有 youtube 视频 url 下载链接的 json 响应的 url,我会给你,非常感谢以上是关于如何使用 avplayer 从文档目录播放视频文件的主要内容,如果未能解决你的问题,请参考以下文章
AVPlayer 无法在 iOS8 上播放 NSDocuments 目录中的视频
AVPlayer 不在 iOS 文档目录中播放 MP3 音频文件