如何从 NSDocumentDirectory 读取数据

发布于 2024-10-02 23:56:44 字数 842 浏览 6 评论 0原文

大家好,我正在开发一个应用程序,它记录用户声音并将其保存到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹淡然 2024-10-09 23:56:44

使用枚举器可以简化遍历文档目录来搜索音乐文件的过程。

        //Use an enumerator to store all the valid music file paths at the top level of your App's Documents directory
        NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory];

        for (NSString *path in directoryEnumerator) 
        {
            //check for all music file formats you need to play
            if ([[path pathExtension] isEqualToString:@"mp3"] || [[path pathExtension] isEqualToString:@"aiff"] ) 
            { 
                [myMusicFiles addObject:path];         
            }
        }

        //the NSArray 'myMusicFiles' will be the datasource for your tableview

Use an enumerator to simplify iterating through the Documents directory to search for music files.

        //Use an enumerator to store all the valid music file paths at the top level of your App's Documents directory
        NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory];

        for (NSString *path in directoryEnumerator) 
        {
            //check for all music file formats you need to play
            if ([[path pathExtension] isEqualToString:@"mp3"] || [[path pathExtension] isEqualToString:@"aiff"] ) 
            { 
                [myMusicFiles addObject:path];         
            }
        }

        //the NSArray 'myMusicFiles' will be the datasource for your tableview
盛装女皇 2024-10-09 23:56:44

您需要 NSFileManager 的 contentsOfDirectoryAtPath:error: 方法来获取请求目录中的文件数组。然后迭代该数组以查找您要查找的文件名。

You need NSFileManager's contentsOfDirectoryAtPath: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文