6

次のコードを使用して、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];
}

ビデオは再生されていますが、画面全体には表示されません (必要に応じて拡大されます)。幅と高さの比率を維持したままサイズ変更されるだけです。

どうすれば解決できますか?ありがとうございました

4

1 に答える 1

10

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];
}
于 2015-06-10T14:14:23.683 に答える