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];
}

This can not read the audio file, It returns audioFile nil. For same code when I pass url from NSBundle like

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

It works fine, Any sugestions. Thank you

2 Answers2

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"];
Arun Gupta
  • 2,628
  • 1
  • 20
  • 37
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.

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];
}
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
  • AVAudioFile not reading video from camera roll, so I store that video in NSDocumentDirectory which is also gives nil for audio file for other videos which is downloaded from server there is no issue – Prathmesh Angachekar Apr 22 '16 at 09:57