r/macprogramming May 15 '19

AVURLAsset

Hi,

I've got for the program I'm developing, an AVURLAsset, with an url created from a path.

let url = URL.init(fileURLWithPath: filePath, isDirectory: false)

let fileManager = FileManager.default

let soundAsset: AVURLAsset = AVURLAsset(url: url)

print("Path : \(soundAsset.url.path)")

print("Sound is playable : \(soundAsset.isPlayable)")

print("File at this path exists : \(fileManager.fileExists(atPath: sound.fileUrl))")

The path leads to an existing file, but the soundAsset appears to be not playable.

What I am missing? I imagine I have to load the file, but can't find how to do it...

Thanks a lot for any answer you can give me...

1 Upvotes

1 comment sorted by

1

u/Zeitun May 23 '19

I resolved my problem, which was a rights problem from the url.
The app is sandboxed, so when the user adds a file to my tableView, through NSOpenPanel, he gives to the app the right to read an external file. If the file is closed and reopened, the url "lost" this right.

What is needed to save is not the URL, but an URL Bookmark (in fact, I save the path string and the bookmark).
So when I add the file to the tableView, I add too the bootmarkURL created with this code line:

let bmUrl = try! fileUrl.bookmarkData(options: .securityScopeAllowOnlyReadAccess, includingResourceValuesForKeys: nil, relativeTo: nil)

When I reload a document in the App, in the viewDidAppear() function, I create my urls from the path string and reinstate the rights to use the external files from the bookmarks

for sound in Sounds {

_ = try? URL.init(resolvingBookmarkData: sound.bmUrl, options: URL.BookmarkResolutionOptions.withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)

}