简体   繁体   中英

How to pause or stop the current playing audio?

In my application, I have one audio file. I want pause or stop the audio by clicking the button. I have tried with some code, but it still plays the audio. Please tell how to pause the audio or stop it by clicking the button.

Stop code.

- (IBAction)back:(id)sender {
    [self.audioPlayer isPlaying];
    [self.audioPlayer stop];
}

I have used the above to stop the playing audio but its not working. where am I doing wrong? How to stop the audio?

Stopping Audio is easy just you need to take 2 Action Method for 2 separate buttons 1->Play and 2->Stop then code is as fallows

-(IBAction)Play:(id)sender
{
[self.audioPlayer play];
}

-(IBAction)Stop:(id)sender
{
[self.audioPlayer stop];
}

Another way is to use BOOL and use the 1 Action method to play and stop like this

First declare BOOl object

BOOL AudioBool;


-(IBAction)Audio:(id)sender
{
   if(AudioBool == YES)
    {
      [self.audioPlayer play];
       AudioBool = NO;
    }
   else
     {
       [self.audioPlayer stop];
        AudioBool = YES;
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM