2016-11-15 70 views
0

我有一個視頻播放器。當我嘗試以全屏模式播放時,它來自左上角。我怎樣才能解決這個問題?`左上角的視頻播放

-(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]; 
} 

`

回答

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) 
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視頻通過它與許多控件一起顯示給用戶。

這些控件可以根據需要通過代碼以自定義的方式製作,僅需幾行代碼即可完成,而且一點也不困難。

0

設置玩家的框架[UIScreen mainScreen] .bounds

化妝_playerViewController.view transfrom

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