2017-04-04 142 views
0

的ProgressView没有更新。通过从其他帖子阅读我已经使用更新方法来更新在每个时间间隔。UIProgressView不会更新音频播放器?而播放歌曲

- (void)viewDidLoad { 
[super viewDidLoad]; 
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Fix you_Coldplay" ofType:@"mp3" inDirectory:@"Coldplay"]; 
NSLog(@"%@",filePath); 
NSURL *url = [NSURL fileURLWithPath:filePath]; 
NSLog(@"%@",url); 
musicPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; 
[musicPlayer prepareToPlay]; 
[trackProgress setProgress:0.0]; 
} 

我有一个播放按钮,它也被用来调用updateTime:方法。

- (IBAction)playButton:(id)sender { 
[musicPlayer play]; 

[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES]; 

NSTimeInterval interval = musicPlayer.currentTime/musicPlayer.duration; 
NSLog(@"%f",interval); 
} 

的更新时间:方法

-(void)updateTime:(NSTimer *)timer { 
trackProgress.progress = (musicPlayer.currentTime/musicPlayer.duration); 
} 

我很新的节目,甚至堆栈溢出。这是我发布的第一个问题。请帮助我,并提前致谢。

回答

0

首先改变定时器值的:

//更改值至0.5或1秒

[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES]; 

确保要设置trackProgress为0作为初始值。

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    trackProgress.progress = 0.0; 
    } 

希望这能解决您的问题。

+0

这不起作用上更新进度视图。当我弹奏时,进度条从零位移动到小前进位置,但在此之后,它再次成为停滞状态 – Finch

+0

您可以检查您获得此分区的最终浮点值:(musicPlayer.currentTime/musicPlayer.duration)。 –

+0

当我没有插入在一个注册NSLog的语句:方法,所述浮子值从0.0开始和最终的浮点值是0.999081。但在结束曲目结束时,当我检查音乐停止播放时是0.002723,然后突然跳到0.993965,结束于0.999081。 – Finch

-1

这可能会有帮助。创建一个Timer并且,如下所示添加到common runloop。还要保留对定时器的引用,以便稍后停止。

self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES]; 
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; 
+0

*计划*定时器隐式添加到运行循环中。 – vadian

0

确保主线程

-(void)updateTime:(NSTimer *)timer { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     self.trackProgress.progress = (musicPlayer.currentTime/musicPlayer.duration); 
    }); 
}