我試圖播放音頻和切換與同一個按鈕看法..錯誤的一個按鈕執行多個動作
- (IBAction)yourbuttonClicked:(UIButton *)sender
{
//start a background sound
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"tap2" ofType: @"caf"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
myAudioPlayer.numberOfLoops = -1; //infinite loop
[myAudioPlayer play];
//for Switch view
ViewController *nextView = [[ViewController alloc] initWithNibName:@"ViewController2"
bundle: Nil];
[self.navigationController pushViewController:nextView animated:YES];
}
2014年3月2日19:07:46.817氣球點擊[847:70B ] *終止應用程序由於未捕獲的異常
'NSInvalidArgumentException' 的,原因是: '* - [NSURL initFileURLWithPath:]:無串 參數'
當我拉unch我的模擬器,然後點擊執行它崩潰的操作的播放按鈕,並在我的日誌中出現上述錯誤..
我該如何解決這個問題並編輯我的代碼?
閱讀錯誤。它會告訴你這個問題。你的'soundFilePath'是'nil'。這意味着您的應用包中實際上沒有名爲'tap2.caf'的文件。 – rmaddy
在控制檯中打印soundFilePath和fileURL值。 – Gaurav