我是iPhone的初学者。我已经获取了plist文件,因为我已经定义了以下代码..
<key>Animalsound</key>
<array>
<string>africanwilddog.mp3</string>
<string>anteater.mp3</string>
<string>baboon.mp3</string>
<string>bat.mp3</string>
<string>bear.mp3</string>
<string>bison.mp3</string>
<string>blackpanther.mp3</string>
</array>
<key>Birdsound</key>
<array>
<string>albatross.mp3</string>
<string>antilleangrackle.mp3</string>
<string>arcticloon.mp3</string>
<string>beeeater.mp3</string>
<string>beltedkingfisher.mp3</string>
<string>bittern.mp3</string>
</array>
所以,为了这个目的,plist文件的不同密钥是如何重复的,我使用了以下代码..
-(IBAction)onclicksound:(id)sender
{
path=[[NSBundle mainBundle]pathForResource:@"Animalfile" ofType:@"plist"];
dict=[NSDictionary dictionaryWithContentsOfFile:path];
animalaudio=[pathdata valueForKey:@"Animalsound"];
NSLog(@"animalaudio:%@",animalaudio);
selectanimalaudio=[animalaudio objectAtIndex:currentsound];
NSString *soundpath=[[NSBundle mainBundle]pathForResource:selectanimalaudio ofType:@"mp3"];
NSLog(@"selectaudio:%@",selectanimalaudio);
NSLog(@"selectpath:%@",soundpath);
AVAudioPlayer *audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:soundpath] error:NULL];
self.audiosound=audioplayer;
[audioplayer release];
[audiosound play];
NSLog(@"audioplayer:%@",audioplayer);
}
但是当我运行这段代码时。它给出了
的运行时错误由于未捕获的异常而终止应用 'NSInvalidArgumentException',原因:' * - [NSURL initFileURLWithPath:]:nil string parameter'和selectpath:(null)
所以,请给我任何有助于我的应用程序的建议和来源......
答案 0 :(得分:0)
在这一行
NSString *soundpath=[[NSBundle mainBundle]pathForResource:selectanimalaudio ofType:@"mp3"];
你搜索的是一个名为 selectanimalaudio (fe bison.mp3)和扩展名“mp3”的文件,所以你搜索的是“ bison.mp3.mp3 “,但我敢打赌,这个文件不存在,必须返回 nil ,所以[NSURL fileURLWithPath:soundpath]
将失败