0

I am working on App in which I want to display current playing time and total time of video. I got the total time. Now for showing current playing time Which method will get called. Can anyone help? I have used avplayer. This is the code:

- (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];
}
Niharika
  • 1,188
  • 15
  • 37

1 Answers1

0

You can get the current played time by using currentItem property using AVPlayerItem

AVPlayerItem *getcurrentItem = yourAVPlayerName.currentItem;

for get total Duration

CMTime fullDuration = getcurrentItem.duration; 

for get current Time

CMTime playercurrentTime = getcurrentItem.currentTime; 

alternate

NSTimeInterval playercurrentTime = CMTimeGetSeconds(getcurrentItem.currentTime);
NSLog(@" get current Time of video :%f ",playercurrentTime);
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143