2017-04-04 130 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); 
    }); 
} 
相關問題