我有一个带有透明背景的“.mov”视频,可以在我的视图中添加效果,但是当我尝试播放时,视频会加载n个播放但我得到一个空白的屏幕,这里是我的代码:
@property (nonatomic) AVPlayer *avPlayer;
@property (nonatomic) AVPlayerViewController* avPlayerView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSBundle* bundle = [NSBundle mainBundle];
NSString* path = [bundle pathForResource:@"Spit" ofType:@"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:path];
AVAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
_avPlayer = [AVPlayer playerWithPlayerItem:anItem];
[AVPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
_avPlayerView = [[AVPlayerViewController alloc] init];
_avPlayerView.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
_avPlayerView.player = _avPlayer;
[_avPlayerView view].backgroundColor = [UIColor clearColor];
[_avPlayerView view].alpha = 0.5;//or any value you think is right
[self.view addSubview:_avPlayerView.view];
}
如果有人有任何想法,请提前感谢任何帮助。
答案 0 :(得分:0)
尝试添加" avPlayerView"作为子视图控制器:
[self addChildViewController:_avPlayerView];
[self.view addSubview:_avPlayerView.view];
[_avPlayerView didMoveToParentViewController:self];
如果您需要全屏,那么最好将AVPlayerViewController添加为显示的视图控制器。您只需提供它,而不是添加为子视图:
[self presentViewController:_ avPlayerView animated:NO completion:nil];