好,所以代码假设当按钮被第一次点击时播放声音文件,当再次点击同一个按钮时播放另一个声音。我把所有的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
你解决你的问题? – orkenstein 2014-10-29 16:22:47