I am trying to implement Pre-Roll Video Ads to MPMoviePlayerController. I want to add advertisement before my main video starts. So here is my code:
override func viewDidLoad() {
super.viewDidLoad()
moviePlayer = MPMoviePlayerController()
moviePlayer.view.frame = CGRect(x: 0, y: self.view.frame.height/2 - self.view.frame.height/6, width: self.view.frame.width, height: self.view.frame.height/3)
self.view.addSubview(moviePlayer.view)
moviePlayer.fullscreen = true
moviePlayer.controlStyle = MPMovieControlStyle.Embedded
self.moviePlayer.playPrerollAdWithCompletionHandler { (var error: NSError!) -> Void in
self.moviePlayer.contentURL = NSURL(string: videoUrl)
self.moviePlayer.play()
}
}
Also I have added MPMoviePlayerController.preparePrerollAds() to my AppDelegate.
However I can't add Pre-Roll Video Ads which refers to adUrl in my code. Any help would be appreciated.
Related
I have an app which plays video clips on demand. This has worked well in previous versions of Xcode but I've upgraded to 8.3.3 and am having problems with the AVPlayerViewController.
The video plays and displays correctly. The control bar appears at the bottom of the view but does not respond to taps and, once faded out, does not reappear on tapping the video - unless, that is, I tap near the top left of the view.
My guess is that the actual controls are hidden in some kind of overlay which has the wrong size i.e. it does not properly overlay the whole of the video view. Is there some way to force the AVPlayerViewController to relayout?
I've tried adding:
_playerController!.view.setNeedsLayout()
_playerController!.view.layoutIfNeeded()
But this has no effect.
Here's my code:
override func viewDidLoad() {
super.viewDidLoad()
_player = AVPlayer()
_playerController = AVPlayerViewController()
_playerController!.showsPlaybackControls = true
_playerController!.allowsPictureInPicturePlayback = false
_playerController!.videoGravity = AVLayerVideoGravityResizeAspectFill
_playerController!.player = _player
self.addChildViewController(_playerController!)
videoView.addSubview(_playerController!.view)
...
override func viewDidLayoutSubviews() {
let frame = UIScreen.main.bounds
_vidWidth = frame.width - 256.0
_vidHeight = _vidWidth / 854.0 * 480.0
videoView.frame = CGRect(x: 0, y: 10.0, width: _vidWidth, height: _vidHeight)
videoView.backgroundColor = UIColor.black
_playerController?.view.frame = CGRect(x: 0, y: 0, width: _vidWidth, height: _vidHeight)
...
func playVideo(_ clip: Clip) {
var videoUrl: URL? = nil
if clip.offlineLocation != nil && clip.state == 2 {
_videoPath = clip.offlineLocation!
videoUrl = URL(fileURLWithPath: _videoPath!)
}
else {
_videoPath = "https://xxxx.cloudfront.net/" + clip.location
videoUrl = URL(string: _videoPath!)
}
NSLog(_videoPath!)
let asset = AVURLAsset(url:videoUrl!, options:nil)
let playerItem = AVPlayerItem(asset: asset)
_player!.replaceCurrentItem(with: playerItem)
_player!.play()
}
By setting app's AVAudioSession's category to AVAudioSessionCategoryPlayback with option mixWithOthers, will make sounds from video in UIWebView be mixed with third party background music (such as Spotify).
But it do NOT work at all once switch to WKWebView. Code sample:
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
try audioSession.setActive(true)
} catch {
print(error)
}
/*
// Mixed background music & video sound in web works with UIWebView
let webview = UIWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 300))
self.view.addSubview(webview)
webview.loadRequest(URLRequest(url: URL(string: "http://www.ultimedia.com/deliver/generic/iframe/mdtk/01509739/src/8l50lp/ad/yes")!))
*/
// Mixed background music & video sound in web do NOT work with WKWebView
let webview = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 300))
self.view.addSubview(webview)
webview.load(URLRequest(url: URL(string: "http://www.ultimedia.com/deliver/generic/iframe/mdtk/01509739/src/8l50lp/ad/yes")!))
// end of viewDidLoad
Code above tested on Xcode 8.2.1, iOS 10.2.1
Any idea or workaround would be really helpful.
Many thanks.
I want to show a video of a topic in top half of the view and its matter in textview in the bottom half of the view. For that video control i want to have the features like play,pause,stop,ff etc. Also i want to play it from local resource as my web services hasn't been setup yet. pls suggest a good solution
I have tried UIWebView and added constraints to webview and textview but for some reason the web view is not showing the video correctly. below is my code
let purl = NSURL(fileURLWithPath: "/Users/Rohit/Desktop/videos/demo/demo/video1.mp4") webView.loadHTMLString("<iframe width = \" \(webView.frame.width) \" height = \"\(webView.frame.height)\" src = \"\(purl)\"></iframe>", baseURL: nil)
webView.backgroundColor = UIColor.green
webView.mediaPlaybackRequiresUserAction = true
webView.scrollView.isScrollEnabled = true
webView.isUserInteractionEnabled = true
Import AVFoundation and AVKit
Then play the video using an URL object (in Swift 3 NSURL is renamed to URL)
let player = AVPlayer(URL: URI)
let controller = AVPlayerViewController()
controller.player = player
self.addChildViewController(controller)
let screenSize = UIScreen.main.bounds.size
let videoFrame = CGRect(x: 0, y: 10, width: screenSize.width, height: (screenSize.height - 10) * 0.5)
controller.view.frame = videoFrame
self.view.addSubview(controller.view)
player.play()
You can use AVPlayerLayer and give it bounds.
private func inits() {
//let rootLayer: CALayer = self.layer
// rootLayer.masksToBounds = true
avPlayerLayer = AVPlayerLayer(player: player)
avPlayerLayer.bounds = self.bounds
// avPlayerLayer.backgroundColor = UIColor.yellowColor().CGColor
self.layer.insertSublayer(avPlayerLayer, atIndex: 0)
}
The following code has a video being played. But the video starts immediately when the view controller is launch. When the view controller is launch I would like the user to hit the play button for the video to play. Now the video plays as soon as the view controller is launched.
let path = NSBundle.mainBundle().pathForResource("x", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
self.moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = self.moviePlayer {
player.view.frame = CGRect(x: 55, y: 75, width: self.view.frame.size.width/2, height: self.view.frame.size.height / 5)
player.view.sizeToFit()
player.scalingMode = MPMovieScalingMode.AspectFit
player.fullscreen = false
player.controlStyle = MPMovieControlStyle.Default
player.movieSourceType = MPMovieSourceType.File
player.repeatMode = MPMovieRepeatMode.One
player.play()
self.view.addSubview(player.view)
I created a Video Player using AVPlayer and AVPlayerViewController. I have set "allowsExternalPlayback" property to true and also "usesExternalPlaybackWhileExternalScreenIsActive" property to true. But still I am not getting Airplay Icon in Player Controls.
player = AVPlayer(URL: url!)
player!.allowsExternalPlayback = true
player?.usesExternalPlaybackWhileExternalScreenIsActive = true
I am running my app on ios 9.2.
You need to add an MPVolumeView in order to get this. You can read about this here: https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AirPlayGuide/EnrichYourAppforAirPlay/EnrichYourAppforAirPlay.html
func appleTv()
{
let rect = CGRect(x: -100, y: 0, width: 0, height: 0)
let airplayVolume = MPVolumeView(frame: rect)
airplayVolume.showsVolumeSlider = false
self.view.addSubview(airplayVolume)
for view: UIView in airplayVolume.subviews {
if let button = view as? UIButton {
button.sendActions(for: .touchUpInside)
break
}
}
airplayVolume.removeFromSuperview()
}