0
votes

I am preparing the URL for one method of AVURLAsset:

+ (AVURLAsset *)URLAssetWithURL:(NSURL *)URL options:(NSDictionary *)options

My URL belongs to a file which is written to the document path of my app. The result of NSLog the path is:

/var/mobile/Containers/Data/Application/E0E64E72-9A47-4E62-B9C6-818AB8BF3F4C/Documents/audio.wav

After got the NSString *path, I tried to convert NSString to NSURL by [NSURL URLWithString:path] and [NSURL fileURLWithPath:path], but both of them are incorrect for URLAssetWithURL.

Error of URLWithString is:

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x170269f80 {NSUnderlyingError=0x17405f440 "The operation couldn’t be completed. (OSStatus error -12780.)", NSLocalizedFailureReason=An unknown error occurred (-12780), NSLocalizedDescription=The operation could not be completed}

And fileURLWithPath crashed the app.

I know url = [[NSBundle mainBundle] URLForResource:fileURL withExtension:@"wav"] will fit the situation. But I want to get the url of a local file to fit URLAssetWithURL as well. Any idea? Thanks.

1
The fileURLWithPath is the right way to convert path to a file URL. You say it "crashed the app". What precisely happened? Did the file URL look OK? Did you confirm that the file was actually found at that location?Rob
@Rob When it crashed, Xcode only displayed a message: terminate called throwing an exception (lldb). And the crash happened before the file URL can be log in console (same code, except replacing URLWithString with fileURLWithPath). If using URLWithString, the file URL was "file:///var/mobile/Containers/.../Documents/audio.wav".WangYudong

1 Answers

0
votes

This way of getting a file from the documents should work:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths.firstObject stringByAppendingPathComponent:@"file"] stringByAppendingPathExtension:@"wav"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
    NSURL *url = [NSURL fileURLWithPath:path];
}