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];
}
This can not read the audio file, It returns audioFile nil. For same code when I pass url from NSBundle like
这无法读取音频文件,它返回audioFile nil。对于相同的代码,当我从NSBundle传递url时
NSString *path = [[NSBundle mainBundle] pathForResource:@"Audio" ofType:@"mp3"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
It works fine, Any sugestions. Thank you
它工作正常,任何sugestions。谢谢
2 个解决方案
#1
0
Issue could be the "/" you are using for append filename to path. Try below change
问题可能是您用于将文件名附加到路径的“/”。尝试以下更改
NSString *filePath = [NSString stringWithFormat:@"%@audio.mp3",documentsDirectory];
Or try below alternative:
或尝试以下替代方案:
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"audio.mp3"];
#2
0
The NSBundle
class is used for finds thing in applications bundle, but the Documents directory is outside the bundle, so the way you're generating the path won't work with Documents directory.
NSBundle类用于在应用程序包中查找事物,但Documents目录在bundle之外,因此生成路径的方式不适用于Documents目录。
Try as below:
请尝试以下方法:
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];
}
#1
0
Issue could be the "/" you are using for append filename to path. Try below change
问题可能是您用于将文件名附加到路径的“/”。尝试以下更改
NSString *filePath = [NSString stringWithFormat:@"%@audio.mp3",documentsDirectory];
Or try below alternative:
或尝试以下替代方案:
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"audio.mp3"];
#2
0
The NSBundle
class is used for finds thing in applications bundle, but the Documents directory is outside the bundle, so the way you're generating the path won't work with Documents directory.
NSBundle类用于在应用程序包中查找事物,但Documents目录在bundle之外,因此生成路径的方式不适用于Documents目录。
Try as below:
请尝试以下方法:
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];
}