iOS app with MoviePlayer won't show player controls - ios

I'm trying to create a simple app with two buttons that trigger two videos.
Right now I have coded the first video:
import UIKit
import MediaPlayer
class ViewController: UIViewController {
var moviePlayer : MPMoviePlayerController?
func playVideo01() {
let path = NSBundle.mainBundle().pathForResource("video01", ofType: "mp4")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = self.view.bounds
moviePlayer?.controlStyle = MPMovieControlStyle.Fullscreen
player.prepareToPlay()
player.scalingMode = .AspectFit
self.view.addSubview(player.view)
}
}
If I build the app, I cannot stop it: my movie player does not show any controls.
Can anyone help me?

Related

How to show only volume control in AVPlayerviewcontroller?

I'm making demo to play a simple video from url.
I can play the video successfully and can disable all controls
How can i show only volume control?
import UIKit
import AVKit
import AVFoundation
class TestViewController: UIViewController {
let videoURL = URL(string: "https://demo-available.com/send2hub/assets/uploads/video/1570194354.mp4")
var player : AVPlayer?
var playerLayer : AVPlayerLayer?
let playerViewController = AVPlayerViewController()
//hide all controls of player controller
playerViewController.showsPlaybackControls = false
override func viewDidLoad() {
player = AVPlayer(url: videoURL!)
playerViewController.player = player
self.present(playerViewController, animated: true) {
self.playerViewController.player!.play()
}
}

Play Local Video File

I have followed a tutorial and trying to get a video to pop up when I press a button. The video pops up but the actual video does not start playing and I just get this screen....
Blank Screen image
the code is as follows:
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
var playerController = AVPlayerViewController()
var player:AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
let videoString:String? = Bundle.main.path(forResource: "motorbikes", ofType: ".m4v")
if let url = videoString {
let videoURL = NSURL(fileURLWithPath: url)
self.player = AVPlayer(url: videoURL as URL)
}
}
#IBAction func goButton(_ sender: Any) {
self.present(self.playerController, animated: true, completion: {
self.playerController.player?.play()
})
}
}
You have forgot to set playercontroller's player instance as phuc-nguyen said
or else you can make use of AVPlayerLayer to play the video without using AVPlayerViewController
let player = AVPlayer(url: URL(string: "path/myvideo.mp4")!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
Using AVPlayerLayer you can customize the player like adding your own controls or views and it won't be a full screen view usually though.
But if you are not looking for any customization its good to stick with AVPlayerViewController it has all the controls baked in
Depends on your requirement

Swift AVPlayer doesn't appear, only sound

i have this code for stream a video from my server :
import AVFoundation
import UIKit
import AVKit
class VideoController : UIViewController{
let avPlayerViewController = AVPlayerViewController()
var avPlayer : AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
playVideo();
}
func playVideo(){
let movieUrl : NSURL = NSURL(string : "http://---------")!;
self.avPlayer = AVPlayer(URL: movieUrl)
self.avPlayerViewController.player = self.avPlayer
self.avPlayerViewController.player?.play()
print("CI SIAMOOOOOOOOOOOOOOOOOOO");
}
}
the problem is that i can't see anything but able to hear the audio.
What is the problem? sorry for bad english.
Try this:
First add Your View Controller to MainViewController Like this,
self.presentViewController(self.avPlayerViewController, animated: true, completion: nil);
It Works otherwise try this:
self.avPlayerViewController.player = AVPlayer(URL: movieUrl)
self.avPlayerViewController.player?.play()
or
var avPlayerItem = AVPlayerItem(URL: movieUrl);
self.avPlayer = AVPlayer(Item:avPlayerItem);
self.avPlayerViewController.player?.play()

I can't load video file from local server in swift

I'm working with iOS using Swift.
I want to load a video file using AVPlayer in Swift.
I was able to load an image and a text file from the local server. But I couldn't load a video file. This is the code that I'm using.
let movieUrl = NSURL(string: "http://akjfsjf/ajdfl/ajkf/car.mp4")
let player = AVPlayer(URL: movieUrl!)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()
I have tried with following URL and function
func playvideo (){
let movieUrl = NSURL(string: "http://www.w3schools.com/html/movie.mp4")
let player = AVPlayer(URL: movieUrl!)
let playerController = AVPlayerViewController()
playerController.player = player
player.play()
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
}
Also from your code i found you are using http rather than https so you need to add following property in plist file.
Try this one,
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
var playerVC : AVPlayerViewController!
var playerItem : AVPlayerItem!
var player : AVPlayer!
var playerLayer: AVPlayerLayer!
override func viewDidAppear(animated: Bool) {
let url = NSURL.init(fileURLWithPath: "your file path")
self.playerItem = AVPlayerItem.init(URL: url!)
self.player = AVPlayer.init(playerItem: self.playerItem)
self.playerVC = AVPlayerViewController.init();
self.playerVC.player = self.player;
self.player.currentItem!.playbackLikelyToKeepUp
self.presentViewController(self.playerVC, animated: true) { () -> Void in
self.playerVC.player?.play()
}
}
}
Hope this helps!

Implementing video view in the Storyboard

I want to build simple video app thats views a video form youtube links thats users add.
I didn't find "VideoView" I mean If image view is for image what is UIView for videos.
There is no object in the original library that performs video viewing function. But you can import the MediaPlayer framework in your project and add it programmatically.
Here is a Swift example
import MediaPlayer
class Step1ViewController: UIViewController {
var moviePlayer: MPMoviePlayerController?
override func viewDidLoad() {
super.viewDidLoad()
playVideo()
}
func playVideo() {
let videoView = UIView(frame: CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.width, self.view.bounds.height))
let pathToEx1 = NSBundle.mainBundle().pathForResource("myVideoFile", ofType: "mp4")
let pathURL = NSURL.fileURLWithPath(pathToEx1!)
moviePlayer = MPMoviePlayerController(contentURL: pathURL)
if let player = moviePlayer {
player.view.frame = videoView.bounds
player.prepareToPlay()
player.scalingMode = .AspectFill
videoView.addSubview(player.view)
}
self.view.addSubview(videoView)
}
}
As for further customisations and app notifications it has a bunch of in-build possibilities. So check it out.
To play video apple has provided MPMovieViewController
see this https://developer.apple.com/library/prerelease/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html
and this
http://www.brianjcoleman.com/tutorial-play-video-swift/
In youtube Video case we got embedded links are used so for that you will get help from this https://github.com/gilesvangruisen/Swift-YouTube-Player
import AVFoundation
import AVKit
class ViewController: UIViewController {
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 subview in your view
self.view.addSubview(playerController.view)
player.play()
}
func stopVideo() {
player.pause()
}
}
In Xcode 10 (with a bit less code):
In the interface builder (Storyboard), add "AVKit Player View Controller"
Create a View Controller that derives from AVPlayerViewController:
import UIKit
import AVKit
class VideoPlayerViewController: AVPlayerViewController {
override func viewDidLoad() {
super.viewDidLoad()
let videoURL = Bundle.main.url(forResource: "video", withExtension: "mp4")!
self.player = AVPlayer(url: videoURL)
}
override func viewDidAppear(_ animated: Bool) {
self.player?.play()
}
}
Don't forget to connect this custom class in the storyboard's identity inspector :)
Note: this example uses video URL pointing to a bundle's resource.

Resources