观察当前播放时间并每隔一段时间显示叠加层



我正在制作一个视频,以视频的特定间隔显示叠加层。我已经设法播放了视频。现在我的目标是观看/观察当前的播放时间值,并在视频达到 2 秒时暂停视频。

经过一些研究发现,当前播放时间不支持KOV。所以我需要实现这个解决方案,但我不知道将代码放在哪里 - 我对目标 C 很陌生。我一直试图把它放在视图控制器(我唯一的视图)中,但它的书面暗示它被放置在其他地方......

我应该为它创建一个新的控制器吗?还是要重写媒体播放器框架中的方法?

这是我的代码:

#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
您可以在同一

视图控制器中找到的解决方案中实现以下方法。

紧接着

[self.movieController play];

调用以下方法

[self BeginPlayerPolling];

在 viewDidAppear 之前将此类注册为观察者,其中初始化了电影控制器

[[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];
}

相关内容

  • 没有找到相关文章

最新更新