Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 观察currentPlaybackTime并每隔一段时间显示一个覆盖图_Ios_Objective C_Cocoa_Media Player_Mpmediaplayercontroller - Fatal编程技术网

Ios 观察currentPlaybackTime并每隔一段时间显示一个覆盖图

Ios 观察currentPlaybackTime并每隔一段时间显示一个覆盖图,ios,objective-c,cocoa,media-player,mpmediaplayercontroller,Ios,Objective C,Cocoa,Media Player,Mpmediaplayercontroller,我正在制作一个视频,以视频的特定间隔显示覆盖层。我已经设法让视频播放了。现在,我的目标是观看/观察currentPlaybackTime值,并在视频达到2秒时暂停视频 经过一些研究发现,currentPlaybackTime不支持KOV。所以我需要实现,但我不知道把代码放在哪里-我对Objective C非常陌生。我一直试图把它放在ViewController(我唯一的视图)中,但它的书面提示被放在其他地方的方式 我应该为它创建一个新的控制器吗?还是重写MediaPlayer框架中的方法 这是我

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

经过一些研究发现,currentPlaybackTime不支持KOV。所以我需要实现,但我不知道把代码放在哪里-我对Objective C非常陌生。我一直试图把它放在ViewController(我唯一的视图)中,但它的书面提示被放在其他地方的方式

我应该为它创建一个新的控制器吗?还是重写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
#导入
#进口
@界面ViewController:UIViewController
@属性(强,非原子)MPMoviePlayerController*movieController;
@属性(非原子)NSTimeInterval currentPlaybackTime;
@结束
#导入“ViewController.h”
@界面视图控制器()
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(无效)视图显示:(BOOL)动画{
self.movieController=[[MPMoviePlayerController alloc]init];
NSString*moviePath=[[NSBundle mainBundle]路径资源:@“m4v”类型的“电影”;
NSURL*movieURL=[NSURL fileURLWithPath:moviePath];
self.movieController=[[MPMoviePlayerController alloc]initWithContentURL:movieURL];
[self.movieController.view setFrame:CGRectMake(0,0480326)];
[self.view addSubview:self.movieController.view];
[self.movieController play];
[self.movieController当前播放时间];
}
@结束

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

紧接着

[self.movieController play];
调用跟踪方法

[self BeginPlayerPolling];
在您初始化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];
}

您好,非常感谢您的帮助:)。不幸的是,我在ViewDidDisplay中将类注册为观察者时遇到错误“使用未声明的标识符movieController”。这很奇怪,因为我认为我在头文件中声明了它。现在试试,我犯了一个错误,将movieController作为ivar而不是属性访问。希望它现在能工作。好的,现在没有错误:),谢谢。所以我不使用链接中的代码吗?我可以使用您的代码观察currentPlaybackTime值吗?当电影播放器完成播放电影时,已将Observer添加到曲目中。您仍然需要这些BeginPlayerPoll和EndPlayerPoll来跟踪currentPlaybackTime的值;