gpt4 book ai didi

iphone - 如何从 NSDocumentDirectory 读取数据

转载 作者:行者123 更新时间:2023-12-03 18:51:41 26 4
gpt4 key购买 nike

大家好,我正在开发一个应用程序,它记录用户声音并将其保存到 NSDocumentDirectory。数据文件保存得很好。但问题是我想播放这些文件。我正在使用 TableView 来实现用 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 中驻留的所有文件填充 TableView 。

请 2 帮助我...我是这项技术的新手。

提前感谢大家........

最佳答案

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

        //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

关于iphone - 如何从 NSDocumentDirectory 读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4214651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com