detecting if audio file is playing – iphone – Education Career Blog

I’m playing an audio clip using a UIButton that calls a method (see code below). I’m trying to figure out how to write a statement to detect if the audio is running, and if the audio file is playing then disable/hide the UIButton.

At the moment if you keep touching the play button multiple instances of the audio clip play.

-(void) audioMethod {


NSString *path = NSBundle mainBundle pathForResource:@"affs" ofType:@"mp3";

theAudio=AVAudioPlayer alloc initWithContentsOfURL:NSURL fileURLWithPath:path error:NULL;

theAudio.delegate=self;

theAudio.numberOfLoops = -1;
theAudio prepareToPlay;

theAudio play;

}

thanks for any help

,

You simply declare the calling class as AVAudioPlayerDelegate (as I see you have done).

Then you write a method:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player 
                       successfully:(BOOL)flag {

    myButton.enabled = YES;

}

and disable the button when you start playing the sound:

...
theAudio.numberOfLoops = -1;
theAudio prepareToPlay;

myButton.enabled = NO;

theAudio play;

Leave a Comment