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.
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.
Tap the video you'd like to watch. At the bottom of the video player, tap full screen .
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).
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.
}
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With