iOS版 - 读从Documents目录音频文件 [英] iOS - Reading an Audio file from Documents Directory

查看:426
本文介绍了iOS版 - 读从Documents目录音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保存音频数据的文档目录,并试图将其读回。如果我马上打回它成功播放,但是,如果我开始一个新的会话和尝试,并播放这首歌曲在本地会失败,即使在上市文件中的文件目录显示我的文件仍然存在。请注意该文件从Documents文件夹中以相同的方式回放(同一code)如果立即播放或新的会话中。

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];
}

然后在我的音乐播放器,我要在本地URL加载到这个文件来初始化AVPlayer:

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];
    }

网址不会得到适当的创建为​​AVPlayer不玩了。此外,我都想尽不同的方式将文件数据加载到一个NSData对象来检查字节大小,但在尝试访问文件数据总是返回零。然而,该文件存在,如果我使用的NSFileManager我能够遍历在Documents目录中的项目并打印其文件名/路径,验证了我我已保存在_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.

推荐答案

不要写入数据库的完整目录路径。它会改变。您只需要在文件名保存数据库作为参考。然后使用方法如下:

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

这篇关于iOS版 - 读从Documents目录音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆