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 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退