【问题标题】:Video is not playing in AVPlayer with视频没有在 AVPlayer 中播放
【发布时间】:2019-07-17 06:41:59
【问题描述】:

我正在使用AVPlayer 播放带有localPath URL 的视频,但它没有在AVPlayer 上播放。

我得到了localPath这个代码:

var selectedAssets = [TLPHAsset]()

for abcd in self.selectedAssets {
        let asset = abcd

        if asset.type == .video {


          //------------- Video Path --------------------//
          let fm = FileManager.default
          let docsurl = try! fm.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
          let url = docsurl.appendingPathComponent(stringUrl!)

      }
 }

这里是路径:-

file:///var/mobile/Containers/Data/Application/915BA33E-5DB9-42C4-B5CD-3898D81FBDC5/Documents/77666c29-75a3-4d89-aecf-15d0f47fbe83.mp4

let video = dbImageDataModel[indexPath.row].fileUrl
print(video)
playerView = AVPlayer(url: URL(fileURLWithPath: video))
playerViewController.player = playerView
self.present(playerViewController, animated: true, completion: {
      self.playerViewController.player!.play()
})

【问题讨论】:

  • URL(fileURLWithPath: video) 打印什么?
  • @TheTiger 实际上我正在获取任何视频的本地路径并存储在本地数据库中并从中获取。打印后得到这个:file:///var/mobile/Containers/Data/Application/915BA33E-5DB9-42C4-B5CD-3898D81FBDC5/Documents/77666c29-75a3-4d89-aecf-15d0f47fbe83.mp4
  • 这是print(video) 的打印描述...我问这里打印URL(fileURLWithPath: video) 是什么。打印您的网址并分享。
  • 这是print(video) 结果file:///var/mobile/Containers/Data/Application/915BA33E-5DB9-42C4-B5CD-3898D81FBDC5/Documents/77666c29-75a3-4d89-aecf-15d0f47fbe83.mp4
  • @Coding 我在问当你将它传递给播放器时会打印什么 URL。所以在这里打印playerView = AVPlayer(url: URL(fileURLWithPath: video))。分享URL(fileURLWithPath: video) 而不是print(video) 的打印说明。因为你的字符串已经包含file://

标签: ios swift avplayer


【解决方案1】:

您使用了错误的 API。

absoluteString 返回包含 file:// 方案的 URL 字符串。要创建新 URL,您必须使用 URL(string 而不是 URL(fileURLWithPath

澄清

  • absoluteString 返回file:///var/mobile/Containers/Data/Application...。要创建 URL,请使用 URL(string:
  • path 返回/var/mobile/Containers/Data/Application...。要创建 URL,请使用 URL(fileURLWithPath:

【讨论】:

  • 我使用absoluteString 只是因为我将路径与String 格式存储在领域数据库中。并且Realm 不支持数据库中的URL NSURL 类型属性。
  • 然后使用正确的 API 创建 URL,如我的回答中所述。对于文件系统 URL,我更喜欢 .pathURL(fileURLWithPath
  • 我已根据您更改了我的代码,但它不起作用。没玩
猜你喜欢
  • 2016-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多