AVAudioFile не может прочитать файл из каталога документов

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. Для того же кода, когда я передаю URL-адрес из NSBundle, например

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

Он отлично работает, любые предложения. Спасибо


person Prathmesh Angachekar    schedule 21.04.2016    source источник


Ответы (2)


Проблема может заключаться в том, что вы используете «/» для добавления имени файла к пути. Попробуйте ниже изменить

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

Или попробуйте альтернативу ниже:

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"audio.mp3"];
person Arun Gupta    schedule 21.04.2016

Класс 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];
}
person Ronak Chaniyara    schedule 22.04.2016
comment
AVAudioFile не читает видео с камеры, поэтому я храню это видео в NSDocumentDirectory, что также дает ноль для аудиофайла для других видео, которые загружаются с сервера, нет проблем - person Prathmesh Angachekar; 22.04.2016