如何用相同的代码制作play/Pause
按钮。
- (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"];
}
如何按同一按钮恢复?
最佳答案
使用此标识按钮状态:
在.h文件中,进行theAudio
声明:
AVAudioPlayer *theAudio;
用你的方法:
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];
}
关于ios - 使用同一按钮播放/暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13910585/