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

标签 ios objective-c audio playback resume

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

相关文章:

objective-c - iAd 横幅 View 委托(delegate)未调用方法

iphone - 核心动画 : ignoring exception: *** -[NSPlaceholderString initWithString:]: nil argument

iphone - 有没有办法从 iPhone 地址簿 API 获取 "Me"卡?

ios - 在单个按钮单击中隐藏多个 UIButton

actionscript-3 - 音频ActionScript 3

ios - 挑战 : Best way to check if model object array contains properties of model in Objective-C

ios - 在应用程序启动时, subview 在设备的不同初始方向上采用不同的帧大小

ios - CocoaLumberjack 的全局日志级别

javascript - JavaScript音频分析语音

android - 如何停止 Activity 中的所有声音?