gpt4 book ai didi

ios - 使用同一按钮播放/暂停

转载 作者:行者123 更新时间:2023-12-01 17:33:04 28 4
gpt4 key购买 nike

如何用相同的代码制作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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com