如何让MPMoviePlayerViewController当前播放时间?

时间:2017-06-03 10:37:07

标签: ios objective-c mpmovieplayercontroller

我希望获得当前的视频播放时间,而不是总持续时间。

我的代码是

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

- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification {
    if ((self.moviePlayer.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK) {
        NSLog(@"content play length is %g seconds", self.moviePlayer.duration);
//        self.lblVideoDuration.text = [NSString stringWithFormat:@"%@", self.moviePlayer.duration];
    }
}

但上面会给我总持续时间,但我不想要总持续时间。

如果我的视频时长为1分钟,目前播放时间约为30秒,我想要实时播放时间。

我怎么能得到它?

我可以通过这样的启动计时器获得视频播放时间。

- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification {
    if (self.moviePlayer.playbackState == MPMoviePlaybackStatePlaying) {
        [self.activityIndicator stopAnimating];
        [self.activityIndicator removeFromSuperview];
        [self startDurationTimer];
    }
}

- (void)startDurationTimer {
    self.durationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(monitorMoviePlayback) userInfo:nil repeats:YES];
}

- (void)stopDurationTimer {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.durationTimer invalidate];
        self.durationTimer = nil;
    });
}

- (void)monitorMoviePlayback {
    double currentTime = floor(self.moviePlayer.currentPlaybackTime);
    double totalTime = floor(self.moviePlayer.duration);
    double minutesElapsed = floor(currentTime / 60.0);
    double secondsElapsed = fmod(currentTime, 60.0);

    NSLog(@"Minute = %f && Second = %f", minutesElapsed, secondsElapsed);
    }
}

但是当我调用stoptimer方法时,Timer没有失效或没有停止。

如何使Timer无效?

2 个答案:

答案 0 :(得分:0)

如果您可以选择使用AVPlayerViewController代替MPMoviePlayerViewController,则可以通过

获取当前时间
myAVPlayerViewController.player.currentTime;

虽然建议切换到AVPlayerViewController,因为不推荐使用MPMoviePlayerViewController,要从MPMoviePlayerViewController获取当前时间,您可以使用以下代码:

self.moviePlayer.currentPlaybackTime;

答案 1 :(得分:0)

非常完美的解决方案: - )

我创建了示例项目,我找到了你的问题。我得到了解决方案。

由于不推荐使用MPMoviePlayerController,我们需要使用AVPlayerViewController。

Apple Document says

  

MPMoviePlayerController类在iOS 9中已正式弃用。   ( MPMoviePlayerViewController类也正式弃用。)   要在iOS 9及更高版本中播放视频内容,请改用   AVPictureInPictureController或 AVPlayerViewController 类来自   AVKit框架,或WebKit的WKWebView类。

所以我们必须使用AVPlayerViewController或更高版本。我使用AVPlayerViewController。

我获得了视频播放的当前时间和持续时间。

  

首先你需要添加和导入AVKit和AVFoundation

ViewController.h

#import <UIKit/UIKit.h>
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>


@interface ViewController : UIViewController

@property (strong, nonatomic) AVPlayerViewController *playerViewController;

- (IBAction)actionPlayVideoWithTime:(id)sender;


@end

ViewController.m

#import "ViewController.h"

@interface ViewController (){
    NSURL *vedioURL;
    AVPlayerItem *playerItem;
    AVPlayer *playVideo;
}

@end

@implementation ViewController

@synthesize playerViewController;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)actionPlayVideoWithTime:(id)sender {
    NSString *strVideoURL   =   [[NSBundle mainBundle] pathForResource:@"IMG_0034" ofType:@"MOV"];
    vedioURL =[NSURL fileURLWithPath:strVideoURL];
    playerItem = [AVPlayerItem playerItemWithURL:vedioURL];
    playVideo = [[AVPlayer alloc] initWithPlayerItem:playerItem];
    playerViewController = [[AVPlayerViewController alloc] init];
    playerViewController.player = playVideo;
    playerViewController.player.volume = 20;
    playerViewController.view.frame = self.view.bounds;
    [self.view addSubview:playerViewController.view];
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
    [playVideo play];
}


- (void)updateTime:(NSTimer *)timer {
    playerItem = playVideo.currentItem;
    CMTime totalDuration = playerItem.duration;
    NSTimeInterval currentDuration = CMTimeGetSeconds(playerItem.duration);
    NSLog(@" Capturing Duration :%f ",currentDuration);
    CMTime currentTime = playerItem.currentTime;
    int time = ceil(currentTime.value/currentTime.timescale);
    NSLog(@"time : %d",time);
    NSTimeInterval currentTimeNow = CMTimeGetSeconds(playerItem.currentTime);
    NSLog(@" Capturing Current Time :%f ",currentTimeNow);
}



@end

我的Bundle中有视频文件,名称为IMG_0034.MOV

请参阅我的应用和输出的屏幕截图。

请参阅播放按钮

enter image description here

我点击下面播放视频的按钮

enter image description here

最后我打印当前时间和持续时间

enter image description here

相关问题