How to put video inside a view

Asked

Viewed 112 times

1

I need to put a video inside a view, in this it has to run only inside that square not fullscreen. (like facebook)

I’ve researched about and only seen full screen tutorials. How to do this?

I got it with 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.
}
  • 2

    Welcome to Sopt. What have you tried to do? Add to question.

  • Thanks! I’ve tried using Avplayer Controller but everything I find on the Internet only shows how the video runs as soon as the view loads. I needed to find a way to get Avplayer into a subview and it would only run inside that view. If anyone has an idea!

  • Do you need the answer in Swift? Still need help?

2 answers

1

You will need to Utilize some Mports to do it with better perfection then see right what each one can do for you.

import UIKit
import MediaPlayer
import AVKit
import AVFoundation

Now it will be necessary to create the component

var videoPlayer: AVPlayerViewController = {
   var view = AVPlayerViewController()
   view.view.translatesAutoresizingMaskIntoConstraints = false
   return view
}()

let player = AVPlayer()

In the viewWillAppear add:

self.addChild(videoPlayer)

Then set the constants of how much the component should occupy on the screen in the example below would be a view like Youtube:

self.view.addSubview(videoPlayer.view)
NSLayoutConstraint.activate([
    self.videoPlayer.view.topAnchor.constraint(equalTo: self.view.topAnchor),
    self.videoPlayer.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
    self.videoPlayer.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
    self.videoPlayer.view.heightAnchor.constraint(equalToConstant: 210)
])

self.videoPlayer.player = player
self.videoPlayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

Now to play a video just create one AVPlayerItem with external or local URL see in the function below:

func reproduzirVideo(item: AVPlayerItem){
    self.videoGravacao.player?.replaceCurrentItem(with: item)
    player.play()
}

I hope I’ve helped.

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

It is running inside a view and resized according to the view.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.