如何从 NSDocumentDirectory 读取数据
大家好,我正在开发一个应用程序,它记录用户声音并将其保存到 NSDocumentDirectory。数据文件保存得很好。但问题是我想播放这些文件。我正在使用表视图来填充它NSDocumentDirectory 中的文件数量。但是该表仅显示当前声音文件的一个单元格。我在某处犯了错误,请帮助我。 我正在使用此代码来玩。
-(void)PlayAudio
{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"strffdsa %@",documentsDirectory);
NSURL *fileURL = [NSURL fileURLWithPath:recorderFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
player.numberOfLoops = 0;
//[fileURL release];
[player play];
[player setDelegate: self];
}
其中 recorderFilePath 是文件的路径。在 NSDate frmattor 的帮助下,该路径每次都是新的。但不知道如何使用 DocumentDirectory 中驻留的所有文件填充表视图。
请2帮助我...我是这项技术的新手。
预先感谢大家..............
Hi everyone I am working on an application which records the user sound and save that to NSDocumentDirectory.The data files are saving well.But the problem is that i want to play those files.I am using the table view for this to populate it with the number of files in the NSDocumentDirectory.But the table shows only one cell the current sound file.I am doing mistake somewhere please help me out.
I am using this code to play.
-(void)PlayAudio
{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"strffdsa %@",documentsDirectory);
NSURL *fileURL = [NSURL fileURLWithPath:recorderFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
player.numberOfLoops = 0;
//[fileURL release];
[player play];
[player setDelegate: self];
}
Where recorderFilePath is the path of file.The path is new everytime with the help of NSDate frmattor.But not getting how to populate the table view with all the files residing in the DocumentDirectory.
Please-2 help me...I am new to this technology.
Thanks in advance to all ........
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用枚举器可以简化遍历文档目录来搜索音乐文件的过程。
Use an enumerator to simplify iterating through the Documents directory to search for music files.
您需要 NSFileManager 的 contentsOfDirectoryAtPath:error: 方法来获取请求目录中的文件数组。然后迭代该数组以查找您要查找的文件名。
You need
NSFileManager
'scontentsOfDirectoryAtPath:error:
method to get an array of files in the request directory. Then iterate over that array to find the file name you're looking for.