Shreedhar
Shreedhar

Reputation: 271

how to stop sound on pressing a button

I am Using Following code to play sound

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

i am Using following line to stop sound

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

Problem is.. When i play sound again, its starting from the point where it was stopped.. i mean to say its working as pause.. help me out

Upvotes: 2

Views: 253

Answers (3)

iCrazyDev
iCrazyDev

Reputation: 925

Try this:

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

}

Upvotes: 1

Dair
Dair

Reputation: 16240

Try resetting the time and then playing, again put the code in your play method:

laughTone.currentTime = 0;
[laughTone play];

Upvotes: 4

Prabhjot Singh Gogana
Prabhjot Singh Gogana

Reputation: 1408

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

Upvotes: 8

Related Questions