2017-08-03 220 views
0

我正在開發應用程序,在其中顯示當前播放時間和視頻總時間。我得到了總時間。現在顯示當前播放時間將調用哪個方法。誰能幫忙?我已經使用avplayer。這是代碼:Avplayer視頻播放方法

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
    [self.avplayer pause]; 
    self.avplayer = [AVQueuePlayer playerWithURL:[NSURL URLWithString:@""]]; 
    self.avplayer = nil; 
} 
- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:NO]; 

    AVPlayerItem *currentItem = self.avplayer.currentItem; 
    CMTime duration = currentItem.duration; //total time 
    CMTime currentTime = currentItem.currentTime; //playing time 

    NSUInteger durationSeconds = (long)CMTimeGetSeconds(duration); 
    NSUInteger minutes = floor(durationSeconds % 3600/60); 
    NSUInteger seconds = floor(durationSeconds % 3600 % 60); 
    NSString *time = [NSString stringWithFormat:@"%02ld:%02ld", (unsigned long)minutes, (unsigned long)seconds]; 
    NSLog(@"Time|%@", time); 
    lblTotaltime.text = time; 


    NSUInteger durationSeconds1 = (long)CMTimeGetSeconds(currentTime); 
    NSUInteger minutes1 = floor(durationSeconds1 % 3600/60); 
    NSUInteger seconds1 = floor(durationSeconds1 % 3600 % 60); 
    NSString *time1 = [NSString stringWithFormat:@"%02ld:%02ld", (unsigned long)minutes1, (unsigned long)seconds1]; 
    NSLog(@"Time|%@", time1); 
    lblRemaningTime.text = time1; 
} 

#pragma mark PlayerMethods 
- (void)itemDidFinishPlaying:(NSNotification *)notification { 
    AVPlayerItem *player = [notification object]; 
    [player seekToTime:kCMTimeZero]; 

} 
- (void)playerItemDidReachEnd:(NSNotification *)notification { 

    AVPlayerItem *p = [notification object]; 
    [p seekToTime:CMTimeMake(0, 3)]; 
} 

- (void)playerStartPlaying 
{ 
    [self.avplayer play]; 
} 
+0

顯示您的嘗試密碼 –

回答

0

您可以通過使用AVPlayerItem

AVPlayerItem *getcurrentItem = yourAVPlayerName.currentItem; 

currentItem屬性獲取當前出場時間,得到總持續時間

CMTime fullDuration = getcurrentItem.duration; 

用於獲取當前時間

CMTime playercurrentTime = getcurrentItem.currentTime; 

備用

NSTimeInterval playercurrentTime = CMTimeGetSeconds(getcurrentItem.currentTime); 
NSLog(@" get current Time of video :%f ",playercurrentTime); 
+0

我得到了持續時間。但我完全不知道在哪裏編寫顯示視頻當前播放時間的代碼 – Niharika

+0

@Niharika - 如果您的yourAVPlayerName在視頻開始後是全球性的,您將獲得此信息,您可以在類中的任何位置使用 –

+0

downvote的原因,如果downvote是合理的,我會更新我的回答 –