【问题标题】:Music progress bar音乐进度条
【发布时间】:2011-10-24 18:20:29
【问题描述】:

我目前正在编写一个基于 Apple 的音乐应用程序,我想为其添加一个进度条。

如何在我的应用上制作一个进度条来显示在默认 iOS 的 iPod 应用中播放的音乐的进度?

【问题讨论】:

  • 你用什么来播放音乐?
  • 我的应用显示了默认 iPod 应用的信息,所以 iPod 应用正在播放音乐

标签: objective-c ios cocoa-touch ipod


【解决方案1】:

您可能想查看MPMusicPlayerController 类引用——特别是currentPlayBackTime 属性。从来没有玩过它,但看起来你想拿它并以某种方式在界面中呈现它。

【讨论】:

    【解决方案2】:

    我就是这样实现的 在头文件中我声明了这些变量。

    NSTimer * currentTimeUpdateTimer;

    'UISlider *timeSlider;

    BOOL userIsScrubbing;

    - (IBAction)handleScrubberTouchDown:(id)sender;

    - (IBAction)handleScrubberTouchUp:(id)sender;

    - (IBAction)handleScrub:(id)sender;

    在 viewDidLoad 方法下的主文件中,我运行了一个 NSTimer currentTimeUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateCurrentiPodItemTime) userInfo:NULL repeats:YES];

    userIsScrubbing = NO;

    在那之后,就是更新所有内容的问题。

    - (void)updateCurrentiPodItemTime {
        MPMediaItem *nowPlayingItem = [self.musicPlayer nowPlayingItem];
    
    if (nowPlayingItem == nil) {
        self.minLabel.text = @"00:00";
    
    } else {
        NSNumber *duration = [nowPlayingItem valueForProperty:MPMediaItemPropertyPlaybackDuration];
        double currentTime = self.musicPlayer.currentPlaybackTime;
        self.minLabel.text = [NSString stringWithFormat: @"%02d:%02d", (int) currentTime / 60, (int) currentTime % 60];     
        self.maxLabel.text = [NSString stringWithFormat: @"%02d:%02d", [duration intValue] / 60, [duration intValue] % 60];
    
        if (!userIsScrubbing)
            self.timeSlider.value = (float) currentTime;
        }
    }
    
    - (IBAction)handleScrubberTouchDown:(id)sender {
         userIsScrubbing = YES;
    }
    
    - (IBAction)handleScrubberTouchUp:(id)sender {
        userIsScrubbing = NO;
    }
    
    - (IBAction)handleScrub:(id)sender {
        self.musicPlayer.currentPlaybackTime = timeSlider.value;
    }
    

    【讨论】:

      【解决方案3】:

      添加一个每秒触发一次的计时器 (1.0) 并让它调用一个方法来更新进度条,如下所示:

      - (void) updateProgressBar:(NSTimer *) timer {
          if (currentPlaybackTime != lengthOfSong) {
              [self.progressBar setValue:self.musicPlayer.currentTime];
          }
          else {
              [timer invalidate], timer = nil;
              [self.progressBar setValue:lengthOfSong];
          }
      }
      

      【讨论】:

        【解决方案4】:

        UIProgressView 添加到您的视图并定期更新。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-27
          • 2012-07-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-30
          相关资源
          最近更新 更多