How to put video inside a view

1

I need to put a video inside a view, in that it has to rotate only within that square not in fullscreen. (facebook type)

I already researched and only saw full screen tutorials. How to do this?

I got this code:

class BeginController: UIViewController {
 @IBOutlet weak var videoPreviewLayer: UIView!
 var player: AVPlayer!
 var avpController = AVPlayerViewController!()

 override func viewDidLoad() {
    super.viewDidLoad()
    let moviePath = NSBundle.mainBundle().pathForResource("Hello           Moto", ofType: "mp4")
    if let path = moviePath{
        let url = NSURL.fileURLWithPath(path)
        let item = AVPlayerItem(URL: url)
        self.player = AVPlayer(playerItem: item)
        self.avpController = AVPlayerViewController()
        self.avpController.player = self.player
        avpController.view.frame = videoPreviewLayer.frame
        self.addChildViewController(avpController)
        self.view.addSubview(avpController.view)




    }
    // Do any additional setup after loading the view.
}
    
asked by anonymous 14.01.2016 / 20:58

1 answer

0

I'm using it this way in Obj-C.

#pragma mark - Implementação do Video de Informativo.
// EM - Implementação da View com o Video anunciado.
- (void) informationLoadMovieChanged {

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:filePath];

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
    [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];

    self.player = [AVPlayer playerWithPlayerItem:playerItem];
    self.player.volume = 0.3;

    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    playerLayer.frame = _playerView.bounds;
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    playerLayer.needsDisplayOnBoundsChange = YES;


    self.playerView.layer.needsDisplayOnBoundsChange = YES;
    self.playerView.layer.borderColor = [UIColor grayColor].CGColor;
    self.playerView.layer.borderWidth = 2.5f;
    self.playerView.layer.cornerRadius = 6;
    self.playerView.layer.masksToBounds = YES;

    [self.playerView.layer addSublayer:playerLayer];
    [self.playerView.layer addSublayer:playerLayer];
    [self.playerView setPlayer:self.player];
    [self.player play];

    [self.player.currentItem addObserver:self forKeyPath:AVPlayerItemDidPlayToEndTimeNotification options:NSKeyValueObservingOptionNew context:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

}

- (void)moviePlayDidEnd:(NSNotification*)notification{

    AVPlayerItem *item = [notification object];
    [item seekToTime:kCMTimeZero];
    [self.player play];
}

You are running within a view and resized according to the view.

    
27.03.2016 / 13:59