Индикатор прогресса музыки - PullRequest
2 голосов
/ 24 октября 2011

В настоящее время я пишу музыкальное приложение на основе Apple и хочу добавить к нему индикатор выполнения.

Как создать индикатор выполнения, отображающий ход воспроизведения музыки вприложение iPod по умолчанию для iOS в моем приложении?

Ответы [ 4 ]

3 голосов
/ 08 июня 2012

Вот как я это сделал В заголовочном файле я объявил эти переменные.

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 голосов
/ 24 октября 2011

Возможно, вы захотите взглянуть на ссылку на класс MPMusicPlayerController , в частности на свойство currentPlayBackTime.Никогда не играл с этим, но похоже, что вы хотите взять это и как-то отрендерить в интерфейсе.

2 голосов
/ 24 октября 2011

Добавить таймер для срабатывания каждую секунду (1.0) и заставить его вызывать метод для обновления индикатора выполнения, как показано ниже:

- (void) updateProgressBar:(NSTimer *) timer {
    if (currentPlaybackTime != lengthOfSong) {
        [self.progressBar setValue:self.musicPlayer.currentTime];
    }
    else {
        [timer invalidate], timer = nil;
        [self.progressBar setValue:lengthOfSong];
    }
}
0 голосов
/ 24 октября 2011

Добавьте UIProgressView к вашему представлению и периодически обновляйте его.

...