0

我在 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;
 }

点击播放按钮时,它不显示音频的持续时间并且不更新当前时间。如果我编码错误或遗漏了什么。任何帮助将不胜感激。

4

2 回答 2

1

两件事: - 我认为您不想在 if 语句的 else 部分创建 [timer invalidate] 之后立即调用它。- 设置 currentTime.text 结束时的 ", nil" 是什么?

于 2013-02-04T18:52:59.880 回答
0

终于解决了。

toolbar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];

 toolbar1.barStyle = UIBarStyleBlackOpaque;

 _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";

UIBarButtonItem *sliderAsToolbarItem = [[UIBarButtonItem alloc] initWithCustomView:_progressBar];

UIBarButtonItem *flexItem1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                          target:nil
                                                                          action:nil];
//Add buttons to the array

NSArray *toolbarItem = [NSArray arrayWithObjects:  home, flexItem1, flexItem1, sliderAsToolbarItem, flexItem1, nil];

[toolbar1 setItems:toolbarItem];

[toolbar1 addSubview:_progressBar];

[toolbar1 addSubview:_currentTime];

[toolbar1 addSubview:_duration];

- (void)playAction:(id)sender
  {
if([player isPlaying])
{
    [sender setImage:[UIImage imageNamed:@"1play.png"] forState:UIControlStateSelected];
    [player pause];


}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:slidertimer forMode:NSRunLoopCommonModes];
   timer = slidertimer;

    }}

- (void)updateProgressBar:(NSTimer *)timer
 {
_duration.text = [NSString stringWithFormat:@"%d:%02d", (int)player.duration / 60, (int)player.duration % 60, nil];
_progressBar.maximumValue = player.duration;

_currentTime.text = [NSString stringWithFormat:@"%d:%02d", (int)player.currentTime / 60, (int)player.currentTime % 60, nil];
_progressBar.value = player.currentTime;

}
于 2013-02-05T16:23:11.860 回答