Hiding video controls MPMoviePlayerController - ios

I have referenced several responses to this same question but their solution does not work for me. I just wish to hide all user controls whilst watching video of local content. It faults on the line moviePlayer?.controlStyle = MPMovieControlStyleNone with error "Use of unresolved identifier for MPMovieControlStyleNone.
Here is my full block of code.
import UIKit
import MediaPlayer
var moviePlayer : MPMoviePlayerController?
class ViewController: UIViewController {
func playVideo() {
let path = NSBundle.mainBundle().pathForResource("IntroVideo", ofType: "mp4")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = self.view.bounds
moviePlayer?.controlStyle = MPMovieControlStyleNone
player.prepareToPlay()
player.scalingMode = .AspectFill
self.view.addSubview(player.view)
}
}
override func viewDidLoad() {
super.viewDidLoad()
playVideo()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

moviePlayer!.controlStyle = MPMovieControlStyle.None for ios 8 and 9

You can remove the moviePlayer?.controlStyle = MPMovieControlStyleNone and try with player.controlStyle = .None just after player.scalingMode = .AspectFill.
That's working for me with iOS 7, but not in iOS 8.

moviePlayer?.controlStyle = .None

Fix by Fork project and fix method presentInView in XCDYouTubeVideoPlayerViewController.h by changing controlStyle to "MPMovieControlStyleNone" then pod your git to your project
That's the easiest way :P

Related

AVPlayer layer not showing video content sometimes

AVPlayer layer not showing video content sometimes but plays audio.
This happens some times not each time
Here is my lines of code :
override func viewDidLoad() {
super.viewDidLoad()
self.tempVideoPath = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("tmpMov.mov")
self.player = AVPlayer(url: self.tempVideoPath!)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.playerLayer = AVPlayerLayer(player: self.player)
self.playerLayer?.frame = self.videoPlayer.bounds
self.playerLayer?.backgroundColor = UIColor.yellow.cgColor
self.videoPlayer.layer.addSublayer(playerLayer!)
print("player = \(playerLayer?.bounds)")
}
Help me to solve this issue.
the problem with you code is that the bounds of videoPlayer can change.
So you need to add the following code to your view controller to change AVPlayerLayer's frame accordingly:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
playerLayer?.frame = view.bounds
}
Hope it helps.
Although the videoplayer has been initialized correctly or created from storyboard, it seems that the layer is non-existent and creates this annoying problem.
At the beginning of your viewDidAppear first, try to create the layer , than add to it>
let layer = CALayer()
self.videoPlayer.layer = layer
//self.playerView.wantsLayer = true // this line is only for osx

MPMovieVIewController shows blank screen

Yep, this is my code:
import Foundation
class FbVideoPostDetailedViewController: FbDetailedPostViewController {
var mc = MPMoviePlayerController()
override func viewDidLoad() {
super.viewDidLoad()
let movieURL = NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
self.mc = MPMoviePlayerController.init(contentURL: movieURL)
self.mc.allowsAirPlay = false
self.mc.shouldAutoplay = true
self.mc.view.frame = UIScreen.mainScreen().bounds
self.mc.controlStyle = MPMovieControlStyle.Embedded
self.view.addSubview(self.mc.view)
self.mc.fullscreen = true
self.mc.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I'm just trying to open video in fullscreen mode. Please guide me in right way because FbVideoPostDetailedViewController appears only as a blank screen and video wasn't play.
I have few outlets in FbDetailedPostViewController but I want even to start play in fullscreen at this point.

SWIFT: MediaPlayer - Done Button doesn't do anything

so I have the following code working great! for anyone wanting to get a video working in SWIFT. The issue is, after the video finishes it remains on the video. Even when pressing Done nothing happens...
How can I close the video back to one of the existing View Controllers?
class ViewController: UIViewController {
var moviePlayer : MPMoviePlayerController?
override func viewDidLoad() {
super.viewDidLoad()
playVideo()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func playVideo() {
//Get the Video Path
//You need to put this in Project->Target->copy bundle resource for this to work
let videoPath = NSBundle.mainBundle().pathForResource("Quizzes", ofType:"mov")
//Make a URL from your path
let url = NSURL.fileURLWithPath(videoPath!)
//Initalize the movie player
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
//Make the player scale the entire view
player.view.frame = self.view.bounds
player.scalingMode = .AspectFill
//Add it as a subView to your currentView
self.view.addSubview(player.view)
//Play the video
player.prepareToPlay()
}
else {
print("Movie player couldn't be initialized")
}
}
You have added it as subview
self.view.addSubview(player.view)
You also need to remove it where is that code?
Please use removeFromSuperview when done playing.

MPMoviePlayerController Stops Playing After 5 seconds - Swift

I have a video I'm trying to play using MPMoviePlayerController and it loads fine, but cuts out after 5 seconds. I found this post, but it isn't really applicable for swift.
MPMoviePlayerController stops playing the video after 5s
Here is my code.
import MediaPlayer
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var moviePlayer: MPMoviePlayerController?
let url = NSURL(string: "http://path/to/video.m3u8")
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
player.view.sizeToFit()
player.scalingMode = MPMovieScalingMode.None
player.movieSourceType = MPMovieSourceType.Streaming
//player.repeatMode = MPMovieRepeatMode.One
player.play()
self.view.addSubview(player.view)
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "metadataUpdated",
name: MPMoviePlayerTimedMetadataUpdatedNotification,
object: nil)
}
}
Could your moviePlayer be going out of scope? Have you tried making it a member variable?
moviePlayer is a local variable of viewDidLoad, so once that function finishes, I don't see any reason why your player would not be deallocated.
If you instead make it a variable of the class, its lifetime will be extended to match your class's lifetime.
something like
class ViewController: UIViewController {
var player: MPMoviePlayerController?
override func viewDidLoad() {
// ...
self.player = MPMoviePlayerController(contentURL: url) // won't go out of scope at end of viewDidLoad()
// ...
}

In iOS how do I programmatically add a video view and then remove it after it finishes playing using Swift?

I'm trying to make a little intro video for an app before you land on the main view. Code as follows:
import UIKit
import MediaPlayer
class ViewController: UIViewController {
var moviePlayer: MPMoviePlayerController?
//var player: MPMoviePlayerController?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
playVideo()
self.view.removeFromSuperview()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func playVideo() {
let path = NSBundle.mainBundle().pathForResource("paint-me_intro", 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)
}
}
}
All I want it to do is after the video finishes playing, it needs to go away. That's all. Any help would be greatly appreciated before I smash my face against the wall.
MPMoviePlayerController uses notifications for message passing, unlike the delegate/protocol pattern of so many other classes. Regardless, to answer your question. Add an observer for the appropriate notification in your view did load, and point to a function which removes the view.
Adding observer
NSNotificationCenter.defaultCenter().addObserver(self, selector: "movieFinished", name:
MPMoviePlayerPlaybackDidFinishNotification, object: nil)
And the function to remove it.
func movieFinished() {
moviePlayer!.view.removeFromSuperview()
NSNotificationCenter.defaultCenter().removeObserver(self, name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)
}

Resources