我构建了一个字符串,它是我想在AVPlayer中播放的文件的目录路径和文件名。但我很难将其转换为网址。
let dataPath = ("\(packDirectory)/").appending((cachedFilePath as NSString).lastPathComponent)
print(dataPath)
audioFileURL = dataPath
self.audioPlayerItem = AVPlayerItem(url: NSURL.fileURL(withPath: audioFileURL) as URL)
print(self.audioPlayerItem)
print(dataPath)返回:
/Users/Geoff/Library/Developer/CoreSimulator/Devices/A31BF8F8-21F6-4227-97B6-9DBDED26CA3E/data/Containers/Data/Application/788085B9-242E-46E7-9644-6A3BF9D515DB/Documents/Next-pk000/7b54d8a0f1a64b710058d4408ca4d696_The%20Name%20of%20the%20Wind%2029-92.mp3
print(self.audioPlayerItem)返回: - url部分:
URL = file:///Users/Geoff/Library/Developer/CoreSimulator/Devices/A31BF8F8-21F6-4227-97B6-9DBDED26CA3E/data/Containers/Data/Application/788085B9-242E-46E7-9644-6A3BF9D515DB/Documents/Next-pk000/7b54d8a0f1a64b710058d4408ca4d696_The%2520Name%2520of%2520the%2520Wind%252029-92.mp3
我不太了解这些东西,但我可以看到2个问题。
1)file://:我故意从我的" dataPath"因为使用文件管理器时它无法找到任何带有此前缀的内容。我已经使用了这个代码:
userDirectory.removeSubrange(userDirectory.startIndex..<userDirectory.index(userDirectory.startIndex, offsetBy: 7))
let packDirectory = userDirectory.appending("Next-\(self.selectedPack!)")
2).lastComponent中的编码已从%20更改为%2520
很困惑!
----编辑----
let urlItem = AVPlayerItem(url: URL(fileURLWithPath: audioFileURL))
if let urlAsset = urlItem.asset as? AVURLAsset {
self.audioPlayerItem = AVPlayerItem(url: NSURL(string: urlAsset.url.path) as! URL)
print(urlAsset.url.path)
}
print(self.audioPlayerItem!)
print(urlAsset.url.path)返回:
/Users/Geoff/Library/Developer/CoreSimulator/Devices/A31BF8F8-21F6-4227-97B6-9DBDED26CA3E/data/Containers/Data/Application/BAD0194A-8CDD-44CE-BF99-B9FF35E23BCA/Documents/Next-pk000/7b54d8a0f1a64b710058d4408ca4d696_The%20Name%20of%20the%20Wind%2029-92.mp3
print(self.audioPlayerItem!)返回:
<AVPlayerItem: 0x7bf81b60, asset = <AVURLAsset: 0x7bf87c70, URL = /Users/Geoff/Library/Developer/CoreSimulator/Devices/A31BF8F8-21F6-4227-97B6-9DBDED26CA3E/data/Containers/Data/Applicati ... 9-92.mp3>>
self.audioPlayerItem = AVPlayerItem(url: URL(fileURLWithPath: urlAsset.url.path))
打印:
file:///Users/Geoff/Library/Developer/CoreSimulator/Devices/A31BF8F8-21F6-4227-97B6-9DBDED26CA3E/data/Containers/Data/Application/7C212656-8E1C-44C8-9951-4444FB5EF853/Documents/Next-pk000/7b54d8a0f1a64b710058d4408ca4d696_The%2520Name%2520of%2520the%2520Wind%252029-92.mp3
甚至使用类似的东西:
let asset = AVAsset(url: URL(string: urlAsset.url.path)!) as AVAsset
导致丢失一些网址:
<AVPlayerItem: 0x79716ed0, asset = <AVURLAsset: 0x797185d0, URL = /Users/Geoff/Library/Developer/CoreSimulator/Devices/A31BF8F8-21F6-4227-97B6-9DBDED26CA3E/data/Containers/Data/Applicati ... 9-92.mp3>>
没有任何戏剧。所以基本上我认为它不会使用file://前缀,但是当我插入字符串时没有它会切断应用程序中i的路径???
答案 0 :(得分:1)
您可以阅读URL documentation的absoluteString
和path
。
要在self.audioPlayerItem's URL
缺少file:///
的情况下获取self.audioPlayerItem
,您可以访问asset.url
的资产并获取let asset = self.audioPlayerItem?.currentItem?.asset
if asset == nil {
return nil
}
if let urlAsset = asset as? AVURLAsset {
return urlAsset.url.path
}
的路径
URL.init(fileURLWithPath: )
已修改:如果您使用的是本地文件,则初始网址为URL.init(string: )
而不是URL.init(string: )
。如果您使用file:///
,则字符串将为完整路径(包含k | v
),而不是路径。
P / S:该代码段只是伪代码,请按照该代码段中的提示进行操作。