2017-02-22 16 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