I'm trying to display a video when my game ends (after a few minutes).
Here are the requirements :
The video is presented modally. Once it ends, it stays still on the last picture and the game can only be restarted by relaunching the app (this behavior is wanted)
The video does not have any control (no pause, no "Done" button etc.)
Most importantly : It does not occupy the whole screen. I want for example to display it in the middle of the screen, approximatively at 50% of the main screen size. (I need to see the VC underneath with a gray/transparent effect)
I achieved this using MPMoviePlayerController, but I did not notice that it was deprecated. So I'm now using AVPlayerViewController (added directly on storyboard)
This way I can :
Present it modally (using segue)
Set a grey/transparent background so that I can see the VC underneath
Disable the controls
But I CAN'T prevent it from resizing my video (I bet because videoGravity resizes it)
Here is my code :
// Game over
case GameSegue.GameEndSegue.rawValue:
// Get photo VC
let videoVC = segue.destinationViewController as! AVPlayerViewController
videoVC.modalTransitionStyle = .CrossDissolve
// Remove controls on the video
videoVC.showsPlaybackControls = false
// Get video URL and provide it to the player
let videoURL = helperClass.urlForFile(videoName)
videoVC.player = AVPlayer(URL: videoURL!)
videoVC.view.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.6)
// This doesn't change anything (hard coded for example)
videoVC.view.frame = CGRectMake(0, 0, 300, 300)
// Play video
videoVC.player?.play()
Can someone please help on this ?
Related
I'm making a pop over presentation view controller where I need to use multiple arrow directions (up and down). For having multiple permittedArrowDirections I've followed Set Multiple Arrow Directions on UIPopoverController in Swift. Also I tweaked the background color of the pop over so that the arrow blends in with the background dimming view.
The problem is, I need to prefer the UIPopoverArrowDirection.up for almost all cases but for the cases where there is not enough space in right below the source view only then the UIPopoverArrowDirection.down should be used. But I can't find to figure it out. The code snippet is:
let presentationController = .....
presentationController.sourceView = sourceView
presentationController.sourceRect = sourceView.bounds
// changing the array order doesn't help
presentationController.permittedArrowDirections = [.up, .down]
presentationController.backgroundColor = UIColor(white: 0, alpha: 0)
self.present(controller, animated: true)
For making it clear I'm attaching the current output:
The source for the demo that I've tried can be found here if anyone needs to follow along.
How to add an overlay UIImage in PNG format (such as a logo) with alpha channel on top of video playback, especially in case of external playback mode of AVPlayer when video is casted on Apple TV via AirPlay?
I would like add an overlay UIImage in PNG format with alpha channel (such as a logo) on top of video playback. This can be easily done on the phone by using contentOverlayView of AVPlayerViewController. However, when the video is casted and played on Apple TV via AirPlay, the contentOverlayView does not display.
I also tried to apply customised UIImageView on external screen when UIScreen has detected the external screen. However, the image still does not show. Instead, only the video playback is shown on the external screen. Here you can see my code for this approach:
if UIScreen.screens.count > 1 {
let externalScreen = UIScreen.screens[1]
print("Playing: externalScreen.bounds: \(externalScreen.bounds)")
let secondWindow = UIWindow(frame: externalScreen.bounds)
secondWindow.screen = externalScreen
let overlayImage = UIImage(named: "rain.png")
let overlayImageView = UIImageView(frame: externalScreen.bounds)
overlayImageView.image = overlayImage
secondWindow.addSubview(overlayImageView)
secondWindow.isHidden = false
secondWindow.makeKeyAndVisible()
}
It does not sound like a difficult problem as adding overlay views like subtitle or image logo seems to be very common operations. Can somebody help?
Thanks.
I'm creating an app for an art installation which will mimic Apple's own camera app, but with slight modifications (slight video display parameters, front camera only, etc.). Everything is working for the most part, however I will have the camera connected to an external monitor, and therefore need it to display in landscape mode, not portrait mode. I have it connected via Lightning to HDMI.
I am using UIImagePickerController and not AVFoundation, because by default it does face detection which is identical to Apple's own (the yellow squares around the faces). I get the feeling that replicating the face detection in AV foundation would be a significant amount of trouble. However, UIImagePickerController simply refuses to display in landscape mode. This is the message in the reference about it:
The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified, with one exception. You can assign a custom view to the cameraOverlayView property and use that view to present additional information or manage the interactions between the camera interface and your code.
from: https://developer.apple.com/reference/uikit/uiimagepickercontroller
Is what I want to do still possible? I am already using a cameraOverlayView, however that so far has not fixed the problem? Can I just change things for HDMI display? Since I will not be submitting this app to apple, I don't particularly need to follow their standards as long as I can install it on my own iPhone 7 Plus running 10.1.1.
Here is my current code in case it helps:
let imagePicker = UIImagePickerController()
let myView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 200))
myView.backgroundColor = UIColor(colorLiteralRed: 0.5, green: 1.0, blue: 0.2, alpha: 1)
myView.alpha = 0.1
imagePicker.delegate = self
imagePicker.sourceType =
UIImagePickerControllerSourceType.camera
imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.front
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
imagePicker.videoQuality = UIImagePickerControllerQualityType.type640x480
//imagePicker.showsCameraControls = false
imagePicker.cameraOverlayView = myView
imagePicker.modalPresentationStyle = UIModalPresentationStyle.currentContext
self.present(imagePicker, animated: true,
completion: nil)
I am using MPMoviePlayerController for showing video inside UIView and using the code below:
self.moviePlayerSmall = MPMoviePlayerController(contentURL:self.objUrl )
self.moviePlayerSmall.view.frame = self.videoView.bounds
self.videoView.addSubview(self.moviePlayerSmall.view)
self.moviePlayerSmall.view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
for subV: UIView in self.moviePlayerSmall.view.subviews {
subV.backgroundColor = UIColor.whiteColor()
}
self.moviePlayerSmall.fullscreen = false
self.moviePlayerSmall.controlStyle = MPMovieControlStyle.Default
self.moviePlayerSmall.scalingMode = .AspectFill
Where videoview is the UIView in which I am adding the MPMoviePlayer view.
The issue I am facing is that whenever the video player comes out of full screen then the player view shows spacing on the left and right sides.
I have tried setting up contentmode, ScaleMode and even AutorezingMask but nothing is working. Have anyone experienced the same issue?
I'm trying to present a camera on the only the top half of my screen and my code isn't resizing the camera properly. I'm trying to add a view to the top half of my screen and then have the camera's cameraOverlayView property conform to that view's frame. Regardless of what I try however, the camera still appears in full screen mode. If someone can tell me what I'm doing wrong I'd really appreciate it. Thanks!
// Setting Up The Camera View
cameraView = UIView(frame: CGRectMake(0.0, 0.0, view.bounds.width, view.bounds.width))
view.addSubview(cameraView)
// Setting Up The Camera
var cam = UIImagePickerController()
cam.delegate = self
cam.allowsEditing = false
cam.videoMaximumDuration = 7
cam.videoQuality = UIImagePickerControllerQualityType.TypeMedium
cam.mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(UIImagePickerControllerSourceType.Camera)!
cam.sourceType = UIImagePickerControllerSourceType.Camera
cam.cameraDevice = UIImagePickerControllerCameraDevice.Rear
cam.cameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video
cam.cameraFlashMode = UIImagePickerControllerCameraFlashMode.Off
cam.showsCameraControls = true
cam.cameraOverlayView = cameraView
cam.cameraOverlayView?.frame = cameraView.frame
camera = cam
self.presentViewController(camera, animated: false, completion: nil)
I do not think what you want to do can be accomplished using a UIImagePickerController. The AVFoundation is what you want to use if you want complete customization of the camera. You can read about it here https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html#//apple_ref/doc/uid/TP40010188-CH5-SW2.
P.S. It is not the simplest framework to use