簡體   English   中英

按鈕執行多項操作時出錯

[英]Error with a button performing multiple actions

我正在嘗試播放音頻並使用同一按鈕切換視圖。

- (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-03-02 19:07:46.817 Balloon Tap [847:70b] *由於未捕獲的異常而終止了應用程序
'NSInvalidArgumentException',原因:'* -[NSURL initFileURLWithPath:]:無字符串參數'

當我啟動模擬器並單擊執行該操作的播放按鈕時,它會崩潰,並且在我的日志中會出現上述錯誤。

如何解決此問題並編輯代碼?

似乎soundFilePath為Nil

試試這個NSURL * url = [NSURL fileURLWithPath:stringPath];

這將刪除分配,這可能會在此處引起一些問題。 另外,我確定您已經檢查過,但是文件名是否存在問題? 它實際上存在於您的捆綁包中嗎? 即它是否與其余代碼一起編譯? 確保將該文件添加到項目中,並在Xcode的可執行文件目標中的“復制捆綁資源”構建階段中顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM