AVPlayerで再生するファイルのディレクトリパスとファイル名を表す文字列を作成しました。しかし、私はこれをURLに変換するのに苦労しています。AVPlayerで使用するためにStringから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の一部:
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でのエンコードは非常に混乱して2520
%に20%から変更されました!
---- EDIT ----
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
でも何か使用:URLの一部を失うことで
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>>
、何も果たしているが。だから基本的に私はそれがファイルで再生されないと思う:/ /プレフィックスが私はそれがない文字列をプラグインするとき何かアプリケーションのiでパスをカット?
私の編集した回答を確認してください。 – nynohu