播放聲音文件時出現問題:我有多個按鈕,每個按鈕都與一個聲音文件關聯。例如,當聲音n.1顫動時,我按下按鈕開始聲音n.2,兩個聲音重疊。我想讓每個按鈕在按下時停止其他按鈕播放的音頻。這是我的.h文件和我的.m文件的一部分。我曾嘗試使用「if」,但我收到「使用未聲明的標識符」錯誤。請記住,我是一個絕對的初學者,提前謝謝你。播放多個音頻文件時重疊
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController <AVAudioPlayerDelegate> {}
-(IBAction)playSound1;
-(IBAction)playSound2;
@end
@implementation ViewController
-(IBAction)playSound1{
NSString *path=[[NSBundle mainBundle] pathForResource:@"12-Toxicity" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];
theAudio.delegate=self;
[theAudio play];
}
@end
你需要到這兩個'AVAudioPlayer's的存儲性能,並呼籲'[self.audio1停止]''中和playSound2'反之亦然...... –
謝謝西門,我怎麼能存儲他們...我想,在.h文件中?我記得做了一些工作,然後我不小心刪除了代碼... –