views:

12

answers:

1

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

+2  A: 

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];
Thomas Børlum
that's great. thanks so much!
hanumanDev
I just tried it and it works perfectly. thanks again.
hanumanDev
You're very welcome. I'm glad it helped you out. Good luck with your project. -- peace
Thomas Børlum