0

I have a video player. when I try to play it in full screen mode, it comes from top left corner. how can I resolve this?`

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

`

Sumi
  • 39
  • 1
  • 10

3 Answers3

0

I found a solution that is more simple and its working for me. Please try this,

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))

Before one is not working means please try the below code,

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

Instead of using AVPlayerViewController, you can do the same as below :

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

Don't forget to add - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context method in your class.

Create a subclass of UIView - PlayerView as below , and create a property of it in your VideoPlayerController class.

#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

For full screen set your viewToPlay's frame to desired frame.

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

Check Playback from developer.apple.com - The Player View for reference.

The property "videoGravity" that you have used in your code cuts the video if set to AVLayerVideoGravityResizeAspectFill (default value is 'AVLayerVideoGravityResize'). AVPlayerViewController provides a view controller environment through which AVPlayer video is displayed to the user together with a number of controls.

These controls can be made by code in custom way as required with just few lines of code and its not at all difficult.

0

set the player's frame to [UIScreen mainScreen].bounds

make _playerViewController.view transfrom

[_playerViewController.view setTransform:CGAffineTransformMakeRotation(-M_PI_2)];
yu xiao
  • 3
  • 3