How to make a play/Pause
button with the same piece of code.
- (IBAction)min:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"1min" ofType:@"mp3"];
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.numberOfLoops = -1;
[theAudio play];
[[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"];
}
How can I resume by the same button?
Use this to identify button state:
In .h file make theAudio
declaration :
AVAudioPlayer *theAudio;
In your method :
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
if(button.selected)
{
// Play
NSString *path = [[NSBundle mainBundle] pathForResource:@"1min" ofType:@"mp3"];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.numberOfLoops = -1;
[theAudio play];
[[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"];
}
else
{
// Pause
[theAudio pause];
}