iOS - Reading an Audio file from Documents Directory

梦想的初衷 提交于 2019-12-13 01:10:32

问题


I am saving audio data to the Documents directory and trying to read it back. If I play it back immediately it plays successfully, however, if I start a new session and try and play the song locally it will fail even though listing the files in the Documents directory shows that my file is still there. Note that the file is played back from the Documents folder in the same way (same code) if it is played immediately or during a new session.

Here is how I save the audio data to the Documents directory:

+(void)writeDataToAudioFile:(NSData*)data forTrack:(MediaItem*)track
{
    // filename looks like "[track_id].mp3" 
    NSString *fileName = [NSString stringWithFormat:@"%@.%@",track.sc_id,track.original_format];

    NSString *pathName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                  NSUserDomainMask,
                                                                  YES) firstObject]
                             stringByAppendingPathComponent:fileName];

    [[NSFileManager defaultManager] createFileAtPath:pathName
                                            contents:data
                                          attributes:nil];
}

Then in my music player I want to load the local URL to this file to initialize the AVPlayer:

NSURL *url;
    if(_currentTrack.is_local_item)
    {
          url = [NSURL fileURLWithPath:_currentTrack.local_file_path];
    }

url does not get created properly as AVPlayer does not play. Furthermore, I have tried every different way to load the file as data into an NSData object to check the byte size but trying to access the file as data always returns nil. However, the file exists as if I use NSFileManager I am able to iterate over the items in the Documents directory and print their file names/paths, validating that I the path I have saved in "_currentTrack.local_file_path" does exist. Again, if I play the file immediately after saving the file to disk it will play back.

If there is more info I can provide to make this clearer I will. Thank you very much.


回答1:


Do not write the full directory path to DB. It will change. You need to only save the file name to DB as reference. Then use as follows:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; 
NSString *fileName = @"SAVED_FILE_NAME.mp3"; // eg: [track_id].mp3
NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName]; 

This will provide you the actual path of the file.

Keep coding........... :)




回答2:


I found the solution after putting the problem down for a few days. I break-pointed and print-stated the heck out of the program and I found that the file path I was saving was not the same as the file path of the file.

I think this was a simulator issue, as the issue only occurred between different sessions of the simulator, and worked within the same session, so the device id (which is part of the absolute path) was changing - maybe someone more knowledgeable can weigh in on that.

Pay closer attention to the string values of your variables folks!



来源:https://stackoverflow.com/questions/31168113/ios-reading-an-audio-file-from-documents-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!