使用相同按钮播放/暂停

Jan*_*jin 1 audio resume objective-c playback ios

如何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"]; 
}
Run Code Online (Sandbox Code Playgroud)

如何通过相同的按钮恢复?

Anu*_*yal 7

使用它来识别按钮状态:

在.h文件中theAudio声明:

AVAudioPlayer *theAudio;
Run Code Online (Sandbox Code Playgroud)

在你的方法中:

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];
}
Run Code Online (Sandbox Code Playgroud)