我是 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)
所以,给我任何对我的应用程序有帮助的建议和来源......