Capture AVPlayer video with AVCaptureSession Screen Recording - swift

I'm capturing one view through AVCaptureSession for Screen Recording.Now I want to record one mp4 video in that view which will play through AVPlayer.Screen Recording is working fine if I set some background color on that particular view but whenever I put video through AVPlayer, its not recording that video although screen records successfully but without mp4 video frame.
I'm using Screen Recording by https://github.com/alskipp/ASScreenRecorder.
Code for playing mp4 video through AVPlayer:
let fileURL = Bundle.main.url(forResource:"file_example_MP4_480_1_5MG", withExtension: "mp4")
let player = AVPlayer(url: fileURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = viewToRecord.frame
viewToRecord.layer.addSublayer(playerLayer)
player.play()
In above code viewToRecord is the UIView which I need to record with mp4 video in Screen Recording.
Looking for best possible solutions!!!

Related

CALayer message while playing video (Xcode 12.5.1, Swift 5)

I'm using a stack view to hold various buttons to go to other view controllers, and one button plays a video. Whenever I press this, I get the following messages but the app works fine and the video plays properly:
CATransformLayer changing property masksToBounds in transform-only layer, will have no effect and changing property allowsGroupBlending in transform-only layer, will have no effect
Any idea? Video plays ok in simulator and on device. I'm using the view controller as player like below, Thanks!
guard let url = Bundle.main.path(forResource: "previewMovie", ofType: "m4v") else{return}
let player = AVPlayer(url: URL(fileURLWithPath: url))
let vc = AVPlayerViewController()
vc.player = player
present(vc, animated: true, completion: nil)
Can I ignore the message as it doesn't look like a warning or an error and considering the video plays and the app works fine? I have to resubmit the app. Thanks!
Yes, ignore it. Nothing's wrong with your code.

Always show AVPlayer controls

I have an AVPlayerViewController with a AVPlayer in it, and what I need is that the controls of the player (play, pause, time slider) never hides. Right now after more or less 4 seconds of playing the video the become hidden, and you have to tap the screen to show them again. I haven't been able to find a solution to that.. any ideas? thx!
You can set that by using Key-Value Coding
Swift:
// playerController : AVPlayerViewController
playerController.setValue(false, forKey: "canHidePlaybackControls")

playing video iphone - mpmovieviewcontroller

I'm playing video with MPMoviePlayerViewController. I get video url from web service. But first I select video quality with AlertView. For example 240p 360p 480p… All video plays except 240p. MPMoviePlayerView appears and then disappears with notification MPMovieFinishReasonPlaybackEnded. I try to play url of 240p video in simulator, everything is perfect but in device it's not playing. I receive MPMovieFinishReasonPlaybackEnded all the time. If I select 360p or more, everything is good. Here is some code:
-(void) playVideoFromURL:(NSString *) url
{
NSLog(#"%#",url); //URL is valid! Browser plays it
if (!videoPlayerView) {
NSURL *fURL = [NSURL URLWithString:url];
videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:fURL];
videoPlayer = videoPlayerView.moviePlayer;
[videoPlayer prepareToPlay];
}
}
It doesn't play even from the local folder. This is an iPhone app, my device is iPad 2!

UIImagePickerController AVAudioPlayer

When I open a UIImagePickerController, I can display the device's camera input stream.
But when I play an AVAudioPlayer with [player play] then the camera stops working.
How can I deal with that?
you can't because UIIMagePickerController is controlling the audio, you will need to use low level AVFoundation in order to do that.
checkout AVCam sample code how to record a movie and add the view as subview , that way you will stay in control of the audio and will be able to play audio.

Show camera stream while AVCaptureSession's running

I was able to capture video frames from the camera using AVCaptureSession according to http://developer.apple.com/iphone/library/qa/qa2010/qa1702.html. However, it seems that AVCaptureScreen captures frames from the camera without showing the camera stream on the screen. I would like to also show camera stream just like in UIImagePicker so that the user knows that the camera is being turned on and sees what the camera is pointed at. Any help or pointer would be appreciated!
AVCaptureVideoPreviewLayer is exactly what you're looking for.
The code fragment Apple uses to demonstrate how to use it is:
AVCaptureSession *captureSession = <#Get a capture session#>;
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
UIView *aView = <#The view in which to present the layer#>;
previewLayer.frame = aView.bounds; // Assume you want the preview layer to fill the view.
[aView.layer addSublayer:previewLayer];