简体   繁体   中英

Disable a UIButton when a sound is playing AVAudioplayer

this is my first simple app, i am using Xcode 5 iOS7.

My problem is when i press the button a sound is playing but if i press the button when the sound is playing the sound starting from the beginning. I don't know how to fix this, disable the button or if i can code something like this:

@implementation ViewController
AVAudioPlayer *audioPlayer;
BOOL playing;


if([audioPlayer isPlaying]){
  (playing = NO);
}

else if(playing == NO){
    [audioPlayer play];
    (playing = YES);
    }

but this doesn't work :)

Here is the code that i using now.

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
AVAudioPlayer *audioPlayer;


- (void)viewDidLoad

{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)playAudio:(id)sender {
NSURL *audioFile;
audioFile = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"docka" ofType:@"mp3"]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFile error:nil];
[audioPlayer play];
}
@end

Thanks for any help!

You could call [yourButton setEnabled:NO]; in your IBAction to play the audio, then call [yourButton setEnabled:YES]; in the delegate method audioPlayerDidFinishPlaying:successfully: . First you'd have to set the delegate for your AVAudioPlayer, and adopt the protocol in your header file, but that should work.

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