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.
Related
i want create a login page for my app, in this case i want to use a video for background of login page, like vine,spotify,uber,...
i play video without problem and add some button and lable in storyboard to my view controllerŲ after run app i can not see objects of view controller
i just see video and objects hide behind of video background how can fix this problem,,,
thanks for helping
this is my code for playing video,,,
lazy var playerLayer:AVPlayerLayer = {
let player = AVPlayer(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("BWWalkthrough", ofType: "mp4")!))
player.muted = true
player.allowsExternalPlayback = false
player.appliesMediaSelectionCriteriaAutomatically = false
var error:NSError?
// This is needed so it would not cut off users audio (if listening to music etc.
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
} catch var error1 as NSError {
error = error1
} catch {
fatalError()
}
if error != nil {
print(error)
}
var playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.frame
playerLayer.videoGravity = "AVLayerVideoGravityResizeAspectFill"
playerLayer.backgroundColor = UIColor.blackColor().CGColor
player.play()
NSNotificationCenter.defaultCenter().addObserver(self, selector:"playerDidReachEnd", name:AVPlayerItemDidPlayToEndTimeNotification, object:nil)
return playerLayer
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.layer.addSublayer(self.playerLayer)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
playerLayer.frame = self.view.frame
}
func playerDidReachEnd(){
self.playerLayer.player!.seekToTime(kCMTimeZero)
self.playerLayer.player!.play()
}
You need to add your playerLayer below all the views created when your storyboard loads. Replacing
self.view.layer.addSublayer(self.playerLayer)
with
self.view.layer.insertSublayer(self.playerLayer, atIndex:0)
should do the job, unless there are any subviews of the view controller view that you actually want to be behind the video layer.
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.
Please see below code. I am trying to remove the video subview from view when the 'done' button is pressed or video stops playing. I show no errors in the code but the removeFromSubview method does not seem to be working. I am not sure if my syntax is wrong or if it is something to do with having the movieplayer code within the IBAction method and the moviePlayBackDidFinish outside below the viewDidLoad. Any advise much appreciated. Thanks
import Foundation
import UIKit
import MediaPlayer
class VideoViewController: UIViewController {
var moviePlayer:MPMoviePlayerController!
#IBAction func videoLaunch(sender: AnyObject) {
playVideo()
}
func playVideo() {
let path = NSBundle.mainBundle().pathForResource("MyVideo", 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()
self.view.addSubview(player.view)
}
}
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "moviePlayBackDidFinish:",
name: MPMoviePlayerPlaybackDidFinishNotification,
object: moviePlayer)
func moviePlayBackDidFinish(notification: NSNotification){
self.view.removeFromSuperview()
}
}
}
You are trying to remove self.view not moviePlayer.view.
Change your moviePlayBackDidFinish code to:
func moviePlayBackDidFinish(notification: NSNotification)
{
if let player = moviePlayer
{
player.view.removeFromSuperview()
}
}
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)
}
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