2014-10-05 48 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