2014-10-05 50 views
0

好,所以代碼假設當按鈕被第一次點擊時播放聲音文件,當再次點擊同一個按鈕時播放另一個聲音。我把所有的MP3文件夾放在一個文件夾中,它只播放文件是的(我有其他的聲音,嗨,你好,但是當我把它放在pathresresce它不會播放它們),我得到一個異常的錯誤當我再次單擊按鈕播放第二個聲音(進入構建階段並且聲音文件夾存在但它不在其他音頻文件中播放時,只有它播放?)。所以我拿出了兩個文件的資源位置路徑來檢查我的聲音文件是否正常工作,並且它仍在播放yes文件。當yes文件只在該聲音文件夾中的一個位置時,這怎麼可能?我甚至從整個項目中拿出了這個聲音文件夾,它仍然播放着沒有聲音文件,沒有路徑資源?爲什麼xcode播放沒有任何資源路徑的聲音文件?

#import <AVFoundation/AVAudioPlayer.h> 
    #import "ViewController.h" 


    @interface ViewController() 


    @end 
    int flag; 

    @implementation ViewController 
    AVAudioPlayer *theAudio; 
    - (IBAction)Yes:(id)sender { 


     if(flag==0){ 
      //play file one on first button click 
      theAudio =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@""ofType:@"mp3"]] error:NULL]; 

      [theAudio play]; 
       flag=1; 
     } 

      else if(flag==1) 
      { 
      //play file second on first button click 


       theAudio =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@""ofType:@"mp3"]] error:NULL]; 



       [theAudio play]; 
          flag=0; 

      } 


    } 
@end 
+0

你解決你的問題? – orkenstein 2014-10-29 16:22:47

回答

2

被刪除的資源的剩菜通常留在模擬器上。

  1. 刪除應用

enter image description here

  • 清潔項目,然後再試一次。
  • enter image description here

    +0

    我不明白這一點。如果我的項目更大,你告訴我,我必須從頭開始?假設假設我花了一個月的時間來製作一個應用程序,而且我遇到了類似的問題,爲了解決一個簡單的問題,我希望刪除整個1個月的工作。 – cloudhalo 2014-10-05 21:49:23

    +0

    @cloudhalo,檢查我的更新。它不會傷害你的代碼。 – orkenstein 2014-10-06 10:11:42

    +1

    這是絕對正確的。每當我看到一個「嘿,資源在模擬器上壞」刪除應用程序,然後做一個乾淨的構建修復它。 – 2014-10-06 13:40:50