1

如何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"]; 
}

如何通过同一个按钮恢复?

4

2 回答 2

7

使用它来识别按钮状态:

在 .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];
}
于 2012-12-17T08:30:42.533 回答
0

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

于 2012-12-17T08:30:10.277 回答