ios – 在AVPlayerLayer上拉伸视频

前端之家收集整理的这篇文章主要介绍了ios – 在AVPlayerLayer上拉伸视频前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用以下代码在我的UIView上显示全屏视频:
- (void)playMovie:(NSString *)name :(NSString *)type
{
    NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:name ofType:type]];

    self.movieAsset = [AVAsset assetWithURL:movieURL];
    self.movieItem = [[AVPlayerItem alloc] initWithAsset:self.movieAsset];
    self.moviePlayer = [[AVPlayer alloc] initWithPlayerItem:self.movieItem];
    self.moviePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
    self.movieLayer.videoGravity = AVLayerVideoGravityResize;

    self.movieLayer = [AVPlayerLayer playerLayerWithPlayer:self.moviePlayer];
    [self.movieLayer setFrame:self.view.frame];

    [self.view.layer addSublayer:self.movieLayer];
    [self.moviePlayer addObserver:self forKeyPath:@"status" options:0 context:nil];

    // Schedule stop after 6 seconds
    [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:@selector(stopCurrentMovie:) userInfo:nil repeats:NO];
}

视频正在播放,但它不会在整个屏幕上填充(如果需要的话),但它只调整大小以保持其宽高比:我已经尝试了“videoGravity”的所有三个值…似乎没有任何改变.

我怎么解决
谢谢

解决方法

你设置Gravity然后重新进入玩家尝试:
- (void)playMovie:(NSString *)name :(NSString *)type
{
 NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:name ofType:type]];

self.movieAsset = [AVAsset assetWithURL:movieURL];
self.movieItem = [[AVPlayerItem alloc] initWithAsset:self.movieAsset];
self.moviePlayer = [[AVPlayer alloc] initWithPlayerItem:self.movieItem];


self.movieLayer = [AVPlayerLayer playerLayerWithPlayer:self.moviePlayer];
[self.movieLayer setFrame:self.view.frame];
self.moviePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
self.movieLayer.videoGravity = AVLayerVideoGravityResize;

[self.view.layer addSublayer:self.movieLayer];
[self.moviePlayer addObserver:self forKeyPath:@"status" options:0 context:nil];

// Schedule stop after 6 seconds
[NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:@selector(stopCurrentMovie:) userInfo:nil repeats:NO];
}

猜你在找的iOS相关文章