Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How a play a video inside a view not in fullscreen?

Tags:

ios

swift

video

I need to play a video inside a view not in full screen(like facebook)! Can anyone help? I'm using swift 2 and xcode 7. I've googled about it but all I find is run with AVPlayerViewController and fullscreen.

Ps: Sorry for the bad english.

like image 347
aline Avatar asked Jan 14 '16 19:01

aline


People also ask

How do I embed an embedded video in Powerpoint in full screen?

In Normal view, click the video frame on the slide that you want to play full screen. Under Video Tools, on the Playback tab, in the Video Options group, select the Play Full Screen check box.

How do you make a video full screen?

Tap the video you'd like to watch. At the bottom of the video player, tap full screen .

How do I make YouTube videos full screen automatically?

Click on your username at the top right and choose YouTube Settings. Make sure its YouTube settings and not just the regular settings for your Google account. In the settings, click on Playback from the left sidebar. Here, check the box in front of Always play HD on full-screen (when available).


3 Answers

This works:

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)
        self.player = AVPlayer(url: url)
        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.
}
like image 51
aline Avatar answered Oct 21 '22 20:10

aline


You can try to use AVPlayerLayer with desired size and position and play video on it.

Example code setting player and layer:

- (void)setupPlayer
{
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:<Your video item URL>];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

    self.player = [AVPlayer playerWithPlayerItem:playerItem];

    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];

    self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

    [self.view.layer addSublayer:self.playerLayer];
}

More about AVPlayer and AVPlayerLayer

like image 3
MichK Avatar answered Oct 21 '22 21:10

MichK


import AVFoundation
import AVKit

var player = AVPlayer()
var playerController = AVPlayerViewController()

func playVideo() {
    let videoURL = NSURL(string: videoUrl)
    player = AVPlayer(url: videoURL! as URL)
    let playerController = AVPlayerViewController()
    playerController.player = player
    self.addChildViewController(playerController)

    // Add your view Frame
    playerController.view.frame = self.view.frame

    // Add sub view in your view
    self.view.addSubview(playerController.view)

    player.play()
}

for player stop or pause

func stopVideo() {
   player.pause()
}

This Code is work for me please try it.

like image 3
Sazzadhusen Iproliya Avatar answered Oct 21 '22 21:10

Sazzadhusen Iproliya