如何在按下按钮时停止声音

时间:2012-02-22 06:05:05

标签: iphone

我正在使用以下代码播放声音

 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];
    }

问题是..当我再次播放声音时,它从停止的位置开始...我的意思是说它的工作暂停..帮帮我

3 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:4)

尝试重置时间然后再播放,再次将代码放入播放方法中:

laughTone.currentTime = 0;
[laughTone play];

答案 2 :(得分: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

}