观察currentPlaybackTime并以一定间隔显示叠加

编程入门 行业动态 更新时间:2024-10-14 04:29:26
本文介绍了观察currentPlaybackTime并以一定间隔显示叠加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

一些研究发现currentPlaybackTime不支持KOV。因此,我需要实施此解决方案,但我不知道在哪里放代码 - 我对Objective C非常新。我不断尝试把它放在ViewController(我唯一的视图),但它的书面提示的方式放置在其他地方...

我应该为它创建一个新的控制器吗?

$ b

这是我的代码:

#import< UIKit / UIKit.h> #import< MediaPlayer / MediaPlayer.h> @interface ViewController:UIViewController @property(强,非原子)MPMoviePlayerController * movieController; @property(nonatomic)NSTimeInterval currentPlaybackTime; @end #importViewController.h @interface ViewController() @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //在加载视图之后执行任何其他设置,通常来自nib。 } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; //处理可以重新创建的任何资源。 } - (void)viewDidAppear:(BOOL)animated { self.movi​​eController = [[MPMoviePlayerController alloc] init]; NSString * moviePath = [[NSBundle mainBundle] pathForResource:@MovieofType:@m4v]; NSURL * movieURL = [NSURL fileURLWithPath:moviePath]; self.movi​​eController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; [self.movi​​eController.view setFrame:CGRectMake(0,0,480,326)]; [self.view addSubview:self.movi​​eController.view]; [self.movi​​eController play]; [self.movi​​eController currentPlaybackTime]; } @end

解决方案

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

之后

[self.movi​​eController play];

调用以下方法

[self BeginPlayerPolling];

在你的viewDidAppear中注册这个类作为观察者,你初始化你的movieController

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

并实现此通知的观察者方法

- (void)movieFinishedCallback:(NSNotification *)aNotification { //从播放完成通知观察者中删除电影播放器​​视图控制器 [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.movi​​eController]; [self EndPlayerPolling]; }

I’m making a video that shows overlays at certain intervals of a video. I’ve managed to get the video to play. Now my objective is to watch/observe the currentPlaybackTime value and pause the video when it hits 2 seconds.

After some research found that currentPlaybackTime does not support KOV. So I need to implement this solution but I have no idea where to put the code - I’m very new to Objective C. I keep trying to put it in the ViewController (My only view) but the way its written hints to it being placed somewhere else…

Should I create a new controller for it? Or do I override methods from the MediaPlayer framework?

Here's my code:

#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

解决方案

You can implement following method in your found solution in this same view controller.

Right after

[self.movieController play];

call following method

[self BeginPlayerPolling];

Register this class as an observer before in your viewDidAppear where you initialised your movieController

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

and implement this notification observer's method

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

更多推荐

观察currentPlaybackTime并以一定间隔显示叠加

本文发布于:2023-11-22 03:48:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615862.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并以   间隔   currentPlaybackTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!