2011-03-08 55 views
-1

我正在嘗試使用AVFoundation框架創建一個帶有很多按鈕的「互動式採訪」音板。音板完成,但我有一個問題,當我按下我的一個聲音時,它不會停止,當我同時按下一個新的。如果按下新的聲音,是否有簡單的方法來停止以前的聲音?我的代碼看起來像這樣如何使用AVAudioPlayer發佈聲音?

- (IBAction)playSound0 { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"about" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

    [theAudio play]; 
} 

- (IBAction)playSound1 { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"chillwhatup" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

    [theAudio play]; 
} 

- (IBAction)playSound2 { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"gethighorgetlow" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

    [theAudio play]; 
} 

- (IBAction)playSound3 { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"icamefromthere" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

    [theAudio play]; 
} 

- (IBAction)playSound4 { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"idoeverythinggood" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 


    [theAudio play]; 

} 
+0

這與Xcode無關。此外,它拼寫「Xcode」,而不是**「XCODE」,「xCode」,「XCode」或其他任何東西。已經瞭解您的拼寫和術語! – 2014-01-01 12:08:19

回答

0

我只想有我的課跟蹤上次啓動的聲音,在此之前我開始玩一個新的聲音,停止以前的一個,如果它仍然在播放。

AVAudioPlayer有停止方法停止聲音和方法,看它是否被打。

+0

謝謝,但那並不能告訴我很多:( – 2011-03-08 22:21:06

相關問題