2014-03-03 75 views
0

我試圖播放音頻和切換與同一個按鈕看法..錯誤的一個按鈕執行多個動作

- (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我的模擬器,然後點擊執行它崩潰的操作的播放按鈕,並在我的日誌中出現上述錯誤..

我該如何解決這個問題並編輯我的代碼?

+0

閱讀錯誤。它會告訴你這個問題。你的'soundFilePath'是'nil'。這意味着您的應用包中實際上沒有名爲'tap2.caf'的文件。 – rmaddy

+0

在控制檯中打印soundFilePath和fileURL值。 – Gaurav

回答

0

似乎soundFilePath是無

嘗試此 NSURL * URL = [NSURL fileURLWithPath:stringPath];

這將刪除alloc,這可能會導致您在此處出現一些問題。另外我相信你已經檢查過了,但是文件名有問題嗎?它是否真的存在於你的包中?我是否正在編譯其餘的代碼?確保將該文件添加到項目中,並且它出現在Xcode中可執行文件目標中的「Copy Bundle Resources」構建階段。

+0

調用'[[NSURL alloc] initFileURLWithPath:]'沒有任何問題。 – rmaddy

相關問題