LPPloveROU 2012-12-19 06:35 采纳率: 50%
浏览 2257

xcode中播放和暂停键

用代码实现一个播放/暂停键

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

用同一个按钮。

  • 写回答

1条回答

  • Mr_me 2012-12-19 08:28
    关注

    在.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];
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算