2013-02-04 27 views
0

我在UIToolbar上添加了UISlider作为进度条。UISlider进度条未更新currentTime

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

当按下播放按钮时,它不显示音频的持续时间,也不更新当前时间。如果我编码错了或错过了什么。任何帮助将不胜感激。

回答

1

两件事: - 我不认为你想在你的if语句的else部分创建它后立即调用[timer invalidate]。 - 在设置currentTime.text结尾处做什么,“零”?

+0

那么你可以用你现在的代码更新你的帖子吗? – onnoweb

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; 

}