观察currentPlaybackTime和示出了在间隔的覆盖(Observing currentP

2019-10-19 03:42发布

我在做,显示在视频的特定间隔重叠的视频。 我设法让视频播放。 现在我的目标是看/观察currentPlaybackTime值和暂停视频当它击中2秒。

经过一番研究,发现currentPlaybackTime不支持KOV。 所以,我需要实现这个解决方案 ,但我不知道在哪里把代码-我很新的目标C.我一直试图把它的视图控制器(我唯一的看法),但方式书面提示,它是放在别的地方...

我应该为它创建一个新的控制器? 还是我覆盖从MediaPlayer的框架方法?

这里是我的代码:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) MPMoviePlayerController *movieController;

@property(nonatomic) NSTimeInterval currentPlaybackTime;

@end



#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (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.
}


- (void)viewDidAppear:(BOOL)animated {

    self.movieController = [[MPMoviePlayerController alloc] init];


    NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
    self.movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

    [self.movieController.view setFrame:CGRectMake (0, 0, 480, 326)];
    [self.view addSubview:self.movieController.view];


    [self.movieController play];
    [self.movieController currentPlaybackTime];




}



@end

Answer 1:

您可以在此同一视图控制器您找到解决方案实现以下方法。

右后

[self.movieController play];

调用下面的方法

[self BeginPlayerPolling];

在viewDidAppear在您初始化您movieController之前注册这个类作为观察员

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(movieFinishedCallback:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object: self.movieController];
self.movieController = [[MPMoviePlayerController alloc] init];

而实现这个通知观察者的方法

- (void)movieFinishedCallback:(NSNotification*)aNotification
{
    // Remove the movie player view controller from the "playback did finish" notification observers
    [[NSNotificationCenter defaultCenter] removeObserver: self
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object: self.movieController];
    [self EndPlayerPolling];
}


文章来源: Observing currentPlaybackTime and showing an overlay at intervals