如何在iOS中计算播放剩余时间的视频?

时间:2014-09-24 12:05:55

标签: ios objective-c iphone xcode

 [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(MPMoviePlayerLoadStateDidChange:)
                                                         name:MPMoviePlayerLoadStateDidChangeNotification object:mPlayer.moviePlayer];


 - (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification
        {
            if ((mPlayer.moviePlayer.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK)
            {
                NSLog(@"content play length is %g seconds", mPlayer.moviePlayer.duration);
                timeRemain = [NSTimeIntervalSince1970 [mPlayer.moviePlayer.duration]-[mPlayer.moviePlayer.currentPlaybackTime]];
                NSLog(@"Time Remaining::%g",timeRemain);
            }
        }

2 个答案:

答案 0 :(得分:2)

MPMoviePlayerController持续时间已经过了几秒钟。 所以

[mPlayer.moviePlayer.duration]-[mPlayer.moviePlayer.currentPlaybackTime]

将以NSTimeInterval的形式返回当前时间和总持续时间的秒数。

答案 1 :(得分:0)

-(void)updateDuration
{
    int remainingDuration = mPlayer.moviePlayer.duration - mPlayer.moviePlayer.currentPlaybackTime;

    if (mPlayer.moviePlayer.currentPlaybackTime != mPlayer.moviePlayer.duration  ) {

        [btnRemainingDuration setTitle:[NSString stringWithFormat:@"Video ends in %d seconds", remainingDuration] forState:UIControlStateNormal];
    }
}