好,所以代碼假設當按鈕被第一次點擊時播放聲音文件,當再次點擊同一個按鈕時播放另一個聲音。我把所有的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