视频播放从左上角开始

时间:2016-11-15 06:23:57

标签: ios objective-c avplayer avplayerviewcontroller

我有一个视频播放器。当我尝试在全屏模式下播放时,它来自左上角。我怎么解决这个问题?`

-(void)video_player:(int)index {

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:[_video_Array objectAtIndex:index] ofType: nil];
url = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVPlayerItem *currentItem = [AVPlayerItem playerItemWithURL:url];
_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.videoGravity = AVLayerVideoGravityResize;
_playerViewController.view.frame = self.view.bounds;
_playerViewController.showsPlaybackControls = NO;
_video = [AVPlayer playerWithPlayerItem:currentItem];
_playerViewController.player = _video;
_playerViewController.view.userInteractionEnabled = false;
_playerViewController.view.backgroundColor = [UIColor whiteColor];
[_playerViewController.player play];
self.view.autoresizesSubviews = YES;

[self.view addSubview:_playerViewController.view];
}

`

3 个答案:

答案 0 :(得分:0)

我找到了一个更简单的解决方案,它适用于我。请试试这个,

let screenWidth = UIScreen.mainScreen().bounds.size.width;
let screenHeight = UIScreen.mainScreen().bounds.size.height'

_playerViewController.view.frame = UIView(frame: CGRect(x: 10, y: 10, width: screenWidth-20, height: screenHeight-20))

在一个人不工作之前,请尝试以下代码,

_playerViewController.frame = CGRectMake(10, 10, screenWidth-20, screenHeight-20)

答案 1 :(得分:0)

您可以执行以下操作,而不是使用AVPlayerViewController:

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:self.videoURLStr] options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
player = [AVPlayer playerWithPlayerItem:playerItem];
[self.viewToPlay setPlayer:player];
[player addObserver:self forKeyPath:@"status" options:0 context:nil];

不要忘记在班上添加- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context方法。

如下所示创建UIView - PlayerView的子类,并在VideoPlayerController类中创建它的属性。

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface PlayerView : UIView
@property (nonatomic) AVPlayer *player;
@end

----------
#import "PlayerView.h"
@implementation PlayerView

+ (Class)layerClass {
    return [AVPlayerLayer class];
}
- (AVPlayer*)player {
    return [(AVPlayerLayer *)[self layer] player];
}
- (void)setPlayer:(AVPlayer *)player {
    [(AVPlayerLayer *)[self layer] setPlayer:player];
}

@end

对于全屏,将viewToPlay的帧设置为所需的帧。

- (void)fullScreenAction:(id)sender
{
    self.viewToPlay.frame = desiredFrame;
}

检查Playback from developer.apple.com - 玩家视图以供参考。

如果设置为AVLayerVideoGravityResizeAspectFill(默认值为'AVLayerVideoGravityResize'),则您在代码中使用的属性“videoGravity”会剪切视频。 AVPlayerViewController提供了一个视图控制器环境,通过该环境,AVPlayer视频与许多控件一起显示给用户。

这些控件可以通过代码以自定义方式根据需要进行,只需几行代码,而且一点都不困难。

答案 2 :(得分:0)

将播放器的帧设置为[UIScreen mainScreen] .bounds

make _playerViewController.view transfrom

[_playerViewController.view setTransform:CGAffineTransformMakeRotation(-M_PI_2)];