2017-02-22 17 views
0

我已经构建了一个字符串,它是AVPlayer中要播放的文件的目录路径和文件名。但我努力将其转换为URL。从AVPlayer使用字符串获取URL的问题

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) 

打印(数据路径)返回:

/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 

打印(self.audioPlayerItem)返回: - 网址部分:

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!) 

打印(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 

打印(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 

导致失去一些的url:

<AVPlayerItem: 0x79716ed0, asset = <AVURLAsset: 0x797185d0, URL = /Users/Geoff/Library/Developer/CoreSimulator/Devices/A31BF8F8-21F6-4227-97B6-9DBDED26CA3E/data/Containers/Data/Applicati ... 9-92.mp3>> 

并没有什么发挥。所以基本上我认为它不会与文件:/前缀播放,但当我插入字符串没有它的东西削减我在应用程序的路径?

+0

请检查我编辑的答案。 – nynohu

回答

1
  1. 你可以阅读有关absoluteStringURL documentationpath。 为了得到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 
    } 
    

编辑路径:如果您使用本地文件时,init URL与URL.init(fileURLWithPath:)而不是URL.init(string:)。如果使用URL.init(string:),则字符串将为完整路径(包含file:///),而不是路径。

  1. 没关系,解码结果是一样的。尝试阅读参考A html space is showing as %2520 instead of %20

P/S:该片段只是伪,请按照该片段的想法。

+0

URL.init(fileURLWithPath:)返回file:/// Users ..... URL.init(string:)返回/ Users/..并在应用程序部分切断,如问题所示...我感到困惑。 – WanderingScouse

+0

是文件://问题?它似乎很普遍。我只知道文件管理器无法找到它的文件。那为什么呢?到底是什么? – WanderingScouse