简体   繁体   English

Avplayer视频播放方法

[英]Avplayer video playing Method

I am working on App in which I want to display current playing time and total time of video. 我正在开发要在其中显示当前播放时间和视频总时间的App。 I got the total time. 我得到了总时间。 Now for showing current playing time Which method will get called. 现在用于显示当前播放时间将调用哪个方法。 Can anyone help? 有人可以帮忙吗? I have used avplayer. 我用过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];
}

You can get the current played time by using currentItem property using AVPlayerItem 您可以使用AVPlayerItem使用currentItem属性来获取当前播放时间

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM