AVPlayer 无法播放视频
AVPlayer cannot play video
我试图在 JSQMessagesViewController 的子类中播放视频,但它没有开始播放 onClick 事件。
我正在从 firebase 下载视频,然后通过附加唯一引用 -LK7WSGtGAQ2anpDxkCQ[=19=] 将数据写入设备。请参阅下面的打印语句以获取完整路径。
//response to collection view tap events
//called every time we type on a message
override func collectionView(_ collectionView: JSQMessagesCollectionView!, didTapMessageBubbleAt indexPath: IndexPath!) {
let message = messages[indexPath.item]
print("message type is \(message.type) and message media uid is \(message.mediaMessage?.mediaUID)")
if message.type == MessageType.image {
let jsqMessage = jsqMessages[indexPath.item]
let mediaItem = jsqMessage.media as! JSQPhotoMediaItem
let photos = IDMPhoto.photos(withImages: [mediaItem.image])
let browser = IDMPhotoBrowser(photos: photos)
self.present(browser!, animated: true, completion: nil)
}
if message.type == MessageType.video {
let jsqMessage = jsqMessages[indexPath.item]
if jsqMessage.isMediaMessage {
if let mediaItem = jsqMessage.media as? JSQVideoMediaItem {
print("mediaItem.fileURL is \(mediaItem.fileURL)")
let player = AVPlayer(url: mediaItem.fileURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
}
}
}
}//end of extension
mediaItem.fileURL is
Optional(file:///Users/bogdanbarbulescu/Library/Developer/CoreSimulator/Devices/7FB42206-997D-4AC2-B0BD-CEE2E22DAFBE/data/Containers/Data/Application/8F17DF31-16B1-4B27-ACB2-015AA55D8979/Documents/-LK7WSGtGAQ2anpDxkCQ)
更新
在上面的url后面加上.MOV后现在可以播放视频了,但是播放的时候没有声音。如何解决这个问题?
问题是我在保存数据到磁盘时,没有在路径末尾添加.MOV
扩展名,因此AVPlayer无法播放视频。
之前:
mediaItem.fileURL 是文件:///Users/bogdanbarbulescu/Library/Developer/CoreSimulator/Devices/7FB42206-997D-4AC2-B0BD-CEE2E22DAFBE/data/Containers/Data/Application/8F17DF31-16B1-4B27-ACB2-015AA55D8979/Documents/-LK7WSGtGAQ2anpDxkCQ
之后:文件:///Users/bogdanbarbulescu/Library/Developer/CoreSimulator/Devices/7FB42206-997D-4AC2-B0BD-CEE2E22DAFBE/data/Containers/Data/Application/8F17DF31-16B1-4B27-ACB2-015AA55D8979/Documents/-LK7WSGtGAQ2anpDxkCQ.MOV
我试图在 JSQMessagesViewController 的子类中播放视频,但它没有开始播放 onClick 事件。 我正在从 firebase 下载视频,然后通过附加唯一引用 -LK7WSGtGAQ2anpDxkCQ[=19=] 将数据写入设备。请参阅下面的打印语句以获取完整路径。
//response to collection view tap events
//called every time we type on a message
override func collectionView(_ collectionView: JSQMessagesCollectionView!, didTapMessageBubbleAt indexPath: IndexPath!) {
let message = messages[indexPath.item]
print("message type is \(message.type) and message media uid is \(message.mediaMessage?.mediaUID)")
if message.type == MessageType.image {
let jsqMessage = jsqMessages[indexPath.item]
let mediaItem = jsqMessage.media as! JSQPhotoMediaItem
let photos = IDMPhoto.photos(withImages: [mediaItem.image])
let browser = IDMPhotoBrowser(photos: photos)
self.present(browser!, animated: true, completion: nil)
}
if message.type == MessageType.video {
let jsqMessage = jsqMessages[indexPath.item]
if jsqMessage.isMediaMessage {
if let mediaItem = jsqMessage.media as? JSQVideoMediaItem {
print("mediaItem.fileURL is \(mediaItem.fileURL)")
let player = AVPlayer(url: mediaItem.fileURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
}
}
}
}//end of extension
mediaItem.fileURL is Optional(file:///Users/bogdanbarbulescu/Library/Developer/CoreSimulator/Devices/7FB42206-997D-4AC2-B0BD-CEE2E22DAFBE/data/Containers/Data/Application/8F17DF31-16B1-4B27-ACB2-015AA55D8979/Documents/-LK7WSGtGAQ2anpDxkCQ)
更新
在上面的url后面加上.MOV后现在可以播放视频了,但是播放的时候没有声音。如何解决这个问题?
问题是我在保存数据到磁盘时,没有在路径末尾添加.MOV
扩展名,因此AVPlayer无法播放视频。
之前: mediaItem.fileURL 是文件:///Users/bogdanbarbulescu/Library/Developer/CoreSimulator/Devices/7FB42206-997D-4AC2-B0BD-CEE2E22DAFBE/data/Containers/Data/Application/8F17DF31-16B1-4B27-ACB2-015AA55D8979/Documents/-LK7WSGtGAQ2anpDxkCQ
之后:文件:///Users/bogdanbarbulescu/Library/Developer/CoreSimulator/Devices/7FB42206-997D-4AC2-B0BD-CEE2E22DAFBE/data/Containers/Data/Application/8F17DF31-16B1-4B27-ACB2-015AA55D8979/Documents/-LK7WSGtGAQ2anpDxkCQ.MOV