I got base64 string of .mp3 from server
NSData *audioData = [[NSData alloc]initWithBase64EncodedString:[dictResult
objectForKey:@"voiceString"] options:0];
here I got Audio Data object audioData(30571bytes).
using following code I am trying to save nsdata object to .mp3 file in temp directory.
NSURL *audioFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory()
stringByAppendingString:objAddXpnseconfigFromRcnt.bAudioName]];
[audioData writeToFile:[audioFileURL absoluteString] atomically:YES];
but the above code is not working?
the method writeToFile:atomically: doesn't create audio file in temp directory.
The problem lies in the path you are creating:
[NSTemporaryDirectory() stringByAppendingString:objAddXpnseconfigFromRcnt.bAudioName]
Should of course be
[NSTemporaryDirectory() stringByAppendingPathComponent:objAddXpnseconfigFromRcnt.bAudioName]
As you can see I use the stringByAppendingPathComponentL:
which will correctly append the filename.
And Why are are creating a NSURL
for the file path only to convert it back to path?
Also it is good practice to not write everything in one line and always make use of the methods with have an error:
parameter. This wil make debugging easier, the following code is the same but way easier to debug:
NSString *tempPath = NSTemporaryDirectory();
NSString *fileName = [tempPath stringByAppendingPathComponent:objAddXpnseconfigFromRcnt.bAudioName];
NSURL *audioFileURL = [NSURL fileURLWithPath:fileName];
NSError *error = nil;
if (![audioData writeToURL:audioFileURL options:NSDataWritingAtomic error:&error] {
NSLog(@"Failed to write MP3 data: %@", error);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.