Ios 如果NSString值用于audioFilePath AVAudioPlayer,则出现“Nil”值错误

Ios 如果NSString值用于audioFilePath AVAudioPlayer,则出现“Nil”值错误,ios,nsstring,avaudioplayer,nsinteger,Ios,Nsstring,Avaudioplayer,Nsinteger,我有114个名为0到113的音频文件。现在AVAudioPlayer将根据用户选择的行加载音频文件。例如,如果用户选择indexPath 0处的行,将加载0.mp3文件。现在,为了让它正常工作,我正在尝试将NSInteger*_selectedRow值转换为字符串值,并将此字符串值用作AVAudioPlayer的音频文件路径。但我的应用程序给出了字符串错误的Nil值,结果崩溃了。我可能做了些蠢事 这就是我尝试过的 -(viewDidLoad){ NSString *selectRow=[NSSt

我有114个名为0到113的音频文件。现在AVAudioPlayer将根据用户选择的行加载音频文件。例如,如果用户选择indexPath 0处的行,将加载0.mp3文件。现在,为了让它正常工作,我正在尝试将NSInteger*_selectedRow值转换为字符串值,并将此字符串值用作AVAudioPlayer的音频文件路径。但我的应用程序给出了字符串错误的Nil值,结果崩溃了。我可能做了些蠢事

这就是我尝试过的

-(viewDidLoad){
NSString *selectRow=[NSString stringWithFormat:@"%d",_selectedRow];
NSLog(@"Value %@",selectRow);//this is working 
[self audioFilling:selectRow];
}

如果您的audioFilePath变量在运行pathForResource后返回为nil,则在您的包中根本找不到该文件

由于您已经检查了值字符串以确保其正确性,并且您知道stringWithFormat调用正在工作,因此留下了最有可能的解释——文件确实不存在于您的包中

您是如何将这些MP3文件添加到应用程序的?您确定它们是应用程序目标的一部分吗?如果不是,它们将不会被放入捆绑包中

- (void)audioFilling:(NSString*)value{


    NSError *error;
    // this audioFilePath is getting Nil for pathFor Resource
    NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:value ofType:@"mp3"];
    NSURL *pathAsURL = [[NSURL alloc] initFileURLWithPath:audioFilePath];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:pathAsURL error:&error];
    NSString *fileString = [NSString stringWithContentsOfFile:[[NSBundle   mainBundle]pathForResource:(NSString*)value ofType:@"txt"]
                                                     encoding:NSUTF8StringEncoding
                                                        error:nil];
    timeArray=[fileString componentsSeparatedByString:@"\n"];

}