Play video not in fullscreen, swift 3 - swift

I am facing a problem with embedding a video into my app. I am trying to play the video inside a box rather than in full screen mode. Because I need a Label at the top of the screen, The video should be in the middle and an another button at the bottom of the screen. The following link is the quick sketch of that I am trying to achieve. Please note that, I want to play the video in that box, not in the fullscreen. The code I have used is shown below, however this code makes the video to go into full screen. Any help on the topic will be very much appreciated, thank you.
override func viewDidAppear(_ animated: Bool) {
let fileURL = NSURL(fileURLWithPath: "videoOne")
playerView = AVPlayer(url: fileURL as URL)
playerViewController.player = playerView
self.present(playerViewController, animated: true){
self.playerViewController.player?.play()
}
}

I'm assuming your video box is called boxView and you should add playerViewController.view into boxView like this:
playerViewController.view.frame = boxView.bounds
boxView.addSubview(playerViewController.view)

Related

Black Screen when stream video with avkit

Hi I want to stream remote video on my app. URL is: http://test.ext/myvideo.mkv
I use AVKit with this code but screen and audio not works. I see only progress bar that go forward.
let player = AVPlayer(url: url)
// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player
controller.player?.automaticallyWaitsToMinimizeStalling = false
controller.player?.playImmediately(atRate: 1.0)
// Modally present the player and call the player's play() method when complete.
self.present(controller, animated: true) {
player.play()
}
AVPlayer don't support mkv video format.
Anyone have a solution for playing all video format ?
I've tested MobileVLCKit but it's too heavy (1,5Go)...

AV Player not working in a container view Swift

I am trying to get a video to play within a container view controller. I would like the video to play only within the container view but have the option to full screen it while playing.
I have a piece of code that can play a video with the screen automatically going full screen using an ‘off container’ button but, I cannot get the video to play within the container. Also, I am unaware of how to use the play button showing on the container view in my image above. The AV Player controller is embedded in the container.
Code below:
#IBAction func playButton(_ sender: Any) {
if let path = Bundle.main.path(forResource: "SampleVideo", ofType: "mp4") {
let video = AVPlayer(url: URL(fileURLWithPath: path))
let videoPlayer = PlayerViewController()
videoPlayer.player = video
present(videoPlayer, animated: true, completion: {
video.play()
})
}
}
No longer an issue. I found out I should have been using AV Player Layer. Now I have the video playing in a UIImageView and is doing what I needed.

Is it possible to make preview image while scroll video timeline like in Netflix app?

Is it possible to make peview image while listing video timeline, like in Netflix App with AVPlayer?
exmple here
I found that mp4 video has that preview window, but m3u8 format has not it
func playVideo(cell: MovieCell) {
guard let urlVideo = URL(string: cell.movieSrc) else { return }
// Create an AVPlayer, passing it the HTTP Live Streaming URL.
let player = AVPlayer(url: urlVideo)
// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player
// Modally present the player and call the player's play() method when complete.
self.present(controller, animated: true) {
player.play()
}
}
This is called "Trick Play" and must be included in the HLS manifest for AVPlayer to handle this for you.
See the Trick Play section in HLS Authoring Specification for Apple Devices:
6.1. I-frame playlists (EXT-X-I-FRAME-STREAM-INF) MUST be provided to support scrubbing and scanning UI.

How to add a video in the UI in Swift

I want to be able to add a video (called "Logo-Animation4.mp4") into the UI in Swift, just like you can a UIImage in a UIImageView.
Right now, I've tried just putting an AVPlayer on the view, but it comes up with the default iOS AVPlayer, which contains the regular fullscreen, controls, and volume stuff that I don't want. I just want the video to play in the UI without any way to have the user interact with it.
I've thought about animating the video using a regular UIImageView's animation feature, but my video isn't that short, and it would be hard to get every single frame, and input it into the code.
How should I go about doing this?
To achieve what you are asking, you'll need to use AVPlayerLayer
Add a UIView outlet
#IBOutlet weak var videoView: UIView!
Import AVFoundation
Create player variables
var player : AVPlayer!
var avPlayerLayer : AVPlayerLayer!
Add the following code to your viewDidLoad
guard let path = Bundle.main.path(forResource: "Logo-Animation4", ofType:"mp4") else {
debugPrint("Logo-Animation4.mp4 not found")
return
}
player = AVPlayer(url: URL(fileURLWithPath: path))
avPlayerLayer = AVPlayerLayer(player: player)
avPlayerLayer.videoGravity = AVLayerVideoGravity.resize
videoView.layer.addSublayer(avPlayerLayer)
player.play()
Add this method
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
avPlayerLayer.frame = videoView.layer.bounds
}

How can I play an intro video the first time I launch an iPhone app using swift (NOT objective-c)?

I want to play an intro video for my iPhone app the first time it launches. I have it set up to detect that it's the first launch, but how do I play the video from the app (not a url) using SWIFT? Do I set it up in the first ViewController file or in the AppDelegate? I've tried using MPMoviePlayerController, but I just can't seem to get it to actually play. Thank you for your help!
I had this same trouble. Here's my solution:
func playVideo() {
let path = NSBundle.mainBundle().pathForResource("video", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = self.view.bounds
player.controlStyle = .None
player.prepareToPlay()
player.scalingMode = .AspectFit
self.view.addSubview(player.view)
}
}
Don't forget to import MediaPlayer and declare your variable - var moviePlayer: MPMoviePlayerController?
Then call your function when you want to play it playVideo()
See my question for how to dismiss it - In iOS how do I programmatically add a video view and then remove it after it finishes playing using Swift?
I got a really helpful answer there.
Also notice that I disabled my controllers because I don't want it to be seen.