0

I have constructed a string that is the directory path and file name of file i would like to play in AVPlayer. But I'm struggling to convert this to a 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)

print(dataPath) returns:

/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) returns: - the url part:

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

I don't really understand this stuff but i can see 2 issues with this.

1) file:// : i have purposely removed this from my "dataPath" as when using file manager it cant find anything with this prefix. i have used this code for that:

userDirectory.removeSubrange(userDirectory.startIndex..<userDirectory.index(userDirectory.startIndex, offsetBy: 7))
let packDirectory = userDirectory.appending("Next-\(self.selectedPack!)")

2) the encoding in the .lastComponent has been changed from %20 to %2520

Very confused !

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

print(urlAsset.url.path) returns :

/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!) returns:

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

prints:

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

even using something like:

let asset = AVAsset(url: URL(string: urlAsset.url.path)!) as AVAsset

results in losing some of the 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>>

And nothing plays. So basically i think it won't play with the file:// prefix but when i plug in the string without it something cuts the path at the i in applications???

WanderingScouse
  • 281
  • 1
  • 3
  • 16

1 Answers1

1
  1. You can read about absoluteString and path of URL documentation. For getting self.audioPlayerItem's URL with lacking of file:/// you can access asset of self.audioPlayerItem and get path of asset.url

    let asset = self.audioPlayerItem?.currentItem?.asset
    if asset == nil {
        return nil
    }
    if let urlAsset = asset as? AVURLAsset {
        return urlAsset.url.path
    }
    

Edited: if you use local file, init URL with URL.init(fileURLWithPath: ) instead of URL.init(string: ). If you use URL.init(string: ), string will be full path (contains file:///), not path.

  1. That's ok, the decoding result is the same. Try to read reference A html space is showing as %2520 instead of %20

P/S: that snippet is just pseudo, please follow the idea from that snippet.

Community
  • 1
  • 1
nynohu
  • 1,628
  • 12
  • 12
  • URL.init(fileURLWithPath: ) returns file:///Users..... URL.init(string: ) returns /Users/.. and cuts off at the application part as posted in question... i am perplexed. – WanderingScouse Feb 23 '17 at 08:14
  • is the file:// the problem? it seems common enough. i just know that file manager cant find files with it. so why is it there? to what end? – WanderingScouse Feb 23 '17 at 08:31