影片无法在AVPlayer中播放

时间:2019-07-17 06:41:59

标签: ios swift avplayer

我正在使用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()
})

1 个答案:

答案 0 :(得分:0)

您使用了错误的API。

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

要澄清

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