2012-02-22 143 views
2

停止聲音我使用下面的代碼來播放聲音如何在按下一個按鈕

NSString *laughSoundPath = [[NSBundle mainBundle]pathForResource:@"laugh" ofType:@"wav"]; 
    NSURL *laughURL = [[NSURL alloc]initFileURLWithPath:laughSoundPath]; 
    laughTone = [[AVAudioPlayer alloc]initWithContentsOfURL:laughURL error:nil]; 
    [laughTone prepareToPlay]; 
    [laughTone setDelegate:self]; 
    [laughURL release]; 

-(IBAction)play 
{ 
[laughTone play]; 
} 

我使用下面的行停止聲音

-(IBAction)stop 
    { 
    [laughTone stop]; 
    } 

問題是..當我再次播放聲音,它從停止的地方開始..我的意思是說它的工作暫停..幫我出去

回答

4

嘗試重置時間,然後再玩,再次把你的播放方法中的代碼:

laughTone.currentTime = 0; 
[laughTone play]; 
+0

都能跟得上它不工作 – Shreedhar 2012-02-22 06:14:34

+0

@Shreedhar:好的編輯試試吧。 – Dair 2012-02-22 06:20:45

8
-(IBAction)stop 
{ 
    [laughTone stop]; 
    [laughTone playAtTime:0]; 
} 
1

試試這個:

NSString *Str = [[NSBundle mainBundle] pathForResource:@"Ahhh" ofType:@"wav"]; 
AVAudioPlayer *audioSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:ahhhPath] error:NULL]; 

-(IBAction)btnStopsound(id)sender 
{ 
    [audioSound stop]; 
    audioSound.currentTime=0; 
    [audioSound play]; //restart the player 

} 
相關問題