【问题标题】:Play/Pause with the same button使用相同的按钮播放/暂停
【发布时间】:2012-12-17 08:23:37
【问题描述】:

如何使用相同的代码制作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"]; 
}

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

【问题讨论】:

    标签: ios objective-c audio playback resume


    【解决方案1】:

    使用它来识别按钮状态:

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

    【讨论】:

    • 能否请写更多关于 // 播放和 // 暂停部分的详细信息?
    • @Anusha 这一步意味着什么“button.selected = !button.selected;”?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      • 2014-12-05
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多