繁体   English   中英

使用同一按钮播放/暂停

[英]Play/Pause with the same button

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

创建一个布尔值,例如buttonIsPlayButton 如果按钮是播放按钮,则在开始时将其设置为true。 然后,当按下按钮时,将其设置为false。 每次按布尔值时,都应该更改图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM