无法播放AVPLayer本地video

所以我正在使用AVFoundation构建自定义video播放器 – AVPlayerAVPlayerLayer 。 目前,我想让玩家做的就是在资产库中播放video,并使用该video的硬编码url。 我希望这个包含在UIViewSubClass ,所以我可以在我的应用程序周围使用它。

这是我到目前为止的代码: CUPlayer.h

 @interface CUPlayer : UIView { AVPlayer *player; AVPlayerLayer *playerLayer; AVPlayerItem *item; NSURL *url; } @property(nonatomic) UIViewAutoresizing autoresizingMask; @end 

CUPlayer.m

 @implementation CUPlayer - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code self.backgroundColor = [UIColor redColor]; [self setupURL]; } return self; } -(void)setupURL { NSLog(@"URL setting up"); NSString *string = @"assets-library://asset/asset.mov?id=0A937F0D-6265-452D-8800- 1A760E8E88B9&ext=mov"; url = [[NSURL alloc] initFileURLWithPath: string]; [self setupPlayerForURL]; } -(void)setupPlayerForURL { AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil]; item = [AVPlayerItem playerItemWithAsset:asset]; player = [AVPlayer playerWithPlayerItem:item]; [player addObserver:self forKeyPath:@"status" options:0 context:nil]; playerLayer = [AVPlayerLayer playerLayerWithPlayer:player]; //player.actionAtItemEnd = AVPlayerActionAtItemEndNone; [self.layer addSublayer:playerLayer]; playerLayer.frame = CGRectMake(0, 0, 200, 200); //[playerLayer setBackgroundColor:[UIColor greenColor].CGColor]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (object == player && [keyPath isEqualToString:@"status"]) { if (player.status == AVPlayerStatusFailed) { NSLog(@"AVPlayer Failed"); } else if (player.status == AVPlayerStatusReadyToPlay) { NSLog(@"AVPlayer Ready to Play"); [player play]; } else if (player.status == AVPlayerItemStatusUnknown) { NSLog(@"AVPlayer Unknown"); } } } 

然后我从View Controller调用它:

 CUPlayer *cuPlayer = [[CUPlayer alloc]initWithFrame:CGRectMake(0, 0, 250, 250)]; [self.view addSubview:cuPlayer]; 

这符合但只是给我一个没有video播放的红色方块。 本地文件的URL绝对正确。 如果我在视图控制器中保存所有代码并在-(void)viewDidLayoutSubviews运行play,我可以使它工作。

非常感谢帮助,我已多次阅读所有文档,试图解决这个问题。

汤姆