0
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/audio.mp3",documentsDirectory];
NSURL *audioPathURL = [NSURL fileURLWithPath:filePath];
NSFileManager *filemanager = [[NSFileManager alloc]init];
    if ([filemanager fileExistsAtPath:filePath])
    {
AVAudioFile *audioFile = [[AVAudioFile alloc] initForReading:audioPathURL error:&err];
}

这无法读取音频文件,它返回 audioFile nil。当我从 NSBundle 传递 url 时,对于相同的代码

NSString *path  = [[NSBundle mainBundle] pathForResource:@"Audio" ofType:@"mp3"];
NSURL *pathURL = [NSURL fileURLWithPath : path]; 

它工作正常,任何建议。谢谢

4

2 回答 2

0

问题可能是您用于将文件名附加到路径的“/”。尝试以下更改

NSString *filePath = [NSString stringWithFormat:@"%@audio.mp3",documentsDirectory];

或尝试以下替代方案:

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"audio.mp3"];
于 2016-04-21T18:19:57.627 回答
0

该类NSBundle用于在应用程序包中查找内容,但 Documents 目录位于包之外,因此生成路径的方式不适用于 Documents 目录。

尝试如下:

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"audio.mp3"];

NSURL *audioPathURL = [NSURL fileURLWithPath:filePath];
NSFileManager *filemanager = [[NSFileManager alloc]init];

if ([filemanager fileExistsAtPath:filePath])
{
      AVAudioFile *audioFile = [[AVAudioFile alloc] initForReading:audioPathURL error:&err];
}
于 2016-04-22T05:03:09.127 回答