我在 aUISlider
上添加了 a 作为进度条UIToolbar
。
toolbar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
toolbar1.barStyle = UIBarStyleBlackOpaque;
UISlider *progressBar = [[UISlider alloc] initWithFrame:CGRectMake(105,10,175,10)];
currentTime = [[UILabel alloc] initWithFrame:CGRectMake(65,12,40,15)];
currentTime.backgroundColor = [UIColor clearColor];
currentTime.textColor = [UIColor whiteColor];
currentTime.text = @"0:00";
duration = [[UILabel alloc] initWithFrame:CGRectMake(285,12,40,15)];
duration.backgroundColor = [UIColor clearColor];
duration.textColor = [UIColor whiteColor];
duration.text = @"0:00";
NSArray *toolbarItem = [NSArray arrayWithObjects: home, flexItem1, flexItem1, slider, flexItem1, nil];
[toolbar1 setItems:toolbarItem];
[toolbar1 addSubview:progressBar];
[toolbar1 addSubview:currentTime];
[toolbar1 addSubview:duration];
// Actions
- (void)playAction:(id)sender {
if ([player isPlaying]) {
[sender setImage:[UIImage imageNamed:@"1play.png"] forState:UIControlStateSelected];
[player pause];
[timer invalidate];
timer = nil;
} else {
[sender setImage:[UIImage imageNamed:@"audiopause.png"] forState:UIControlStateNormal];
[player play];
slidertimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateProgressBar:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:progressTimer forMode:NSRunLoopCommonModes];
timer = progressTimer;
[timer invalidate];
}
}
- (void)updateProgressBar:(NSTimer *)timer {
duration.text = [NSString stringWithFormat:@"%d:%02d", (int)self.player.duration / 60, (int)self.player.duration % 60];
_progressBar.maximumValue = self.player.duration;
currentTime.text = [NSString stringWithFormat:@"%d:%02d", (int)self.player.currentTime / 60, (int)player.currentTime % 60, nil];
_progressBar.value = self.player.currentTime;
}
点击播放按钮时,它不显示音频的持续时间并且不更新当前时间。如果我编码错误或遗漏了什么。任何帮助将不胜感激。