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.
Related
I have an WKWebView there I show my site and I play a sound effect with js but when I do so the background music is stop playing. I like to mix in this sound effect with background music like Spotify.
Any idea how I do so?
This is my code in ViewController.swift
import WebKit
import AVFoundation
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
// Set so play inline works
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true
if #available(iOS 10.0, *) {
webConfiguration.mediaTypesRequiringUserActionForPlayback = []
} else {
// Fallback on earlier versions
webConfiguration.mediaPlaybackRequiresUserAction = false
}
webView = WKWebView(frame: CGRect.zero, configuration: webConfiguration)
//super.init(coder: aDecoder)
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Set so we can play sound effect without stop background music
// Get the local lang from iphone
var locale = Locale.preferredLanguages[0]
if (locale.contains("-")) {
locale = locale.substring(to: locale.characters.index(locale.startIndex, offsetBy: 2))
}
let url = URL(string: "https://test.com/?lang=" + locale)!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
// add background color to wkwebview to transparent
webView.backgroundColor = UIColor.clear
webView.scrollView.backgroundColor = UIColor.clear
// Change the statusbar to vit
UIApplication.shared.statusBarStyle = .lightContent
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I have found out that this is not possible.
FYI, I am using this Github Player: https://github.com/piemonte/Player
I am trying to play a video, but it wont work. On the View Controller's UIView i use background color blue, and when I run this project, I only see a bue background without a player. This is my code:
import UIKit
import Player
import AVFoundation
class ViewController: UIViewController, PlayerDelegate {
#IBOutlet var myView: UIView!
private var player: Player!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let fileUrl = NSURL(string: "https://parseapi.back4app.com/files/SAQY2fBCiob9UMqITqAEMo7UxNsU6MCCUURf53Ee/d349a0bdd0962cb0f570c95621aee6df_video.mp4")
self.player = Player()
self.player.delegate = self
player.view.frame.size.width = UIScreen.mainScreen().bounds.size.width
player.view.frame.size.height = player.view.frame.size.width
player.fillMode = AVLayerVideoGravityResizeAspectFill
player.view.sizeToFit()
myView.addSubview(player.view)
let videoUrl: NSURL = fileUrl!
self.player.setUrl(videoUrl)
self.player.playFromBeginning()
self.player.playbackLoops = true
let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGestureRecognizer:")
tapGestureRecognizer.numberOfTapsRequired = 1
self.player.view.addGestureRecognizer(tapGestureRecognizer)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: PlayerDelegate Start
func handleTapGestureRecognizer(gestureRecognizer: UITapGestureRecognizer) {
switch (self.player.playbackState.rawValue) {
case PlaybackState.Stopped.rawValue:
self.player.playFromBeginning()
case PlaybackState.Paused.rawValue:
self.player.playFromCurrentTime()
case PlaybackState.Playing.rawValue:
self.player.pause()
case PlaybackState.Failed.rawValue:
self.player.pause()
default:
self.player.pause()
}
}
func playerReady(player: Player) {
}
func playerPlaybackStateDidChange(player: Player) {
}
func playerBufferingStateDidChange(player: Player) {
}
func playerPlaybackWillStartFromBeginning(player: Player) {
}
func playerPlaybackDidEnd(player: Player) {
}
/* override func awakeFromNib() {
self.collectionView.autoresizesSubviews = true
self.collectionView.autoresizingMask = UIViewAutoresizing.FlexibleWidth
self.collectionView.translatesAutoresizingMaskIntoConstraints = true
}*/
// MARK: PlayerDelegate Stop
}
Attempt nr 2:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let fileUrl = NSURL(string: "https://parseapi.back4app.com/files/SAQY2fBCiob9UMqITqAEMo7UxNsU6MCCUURf53Ee/d349a0bdd0962cb0f570c95621aee6df_video.mp4")
self.player = Player()
self.player.delegate = self
self.player.view.frame = self.view.bounds
self.addChildViewController(self.player)
self.view.addSubview(self.player.view)
self.player.didMoveToParentViewController(self)
let videoUrl: NSURL = fileUrl!
self.player.setUrl(videoUrl)
self.player.playFromBeginning()
self.player.playbackLoops = true
let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGestureRecognizer:")
tapGestureRecognizer.numberOfTapsRequired = 1
self.player.view.addGestureRecognizer(tapGestureRecognizer)
}
When I try to open the link in Safari, it doesn't work, but in Chrome it works fine.. Any suggestions what might be wrong?
Actually the player is not actually added to your view yet.
Please follow this to add this on your view as a sub-viewcontroller.
self.player = Player()
self.player.delegate = self
self.player.view.frame = self.view.bounds
self.addChildViewController(self.player)
self.view.addSubview(self.player.view)
self.player.didMoveToParentViewController(self)
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.
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.
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