I am trying to add a video to a view controller. I tried using the AVPlayerViewController but i was not able to achieve the result i wanted. I do not want the video to open in a new view controller. Instead i want it to be playing in the background. There will be buttons in the foreground and the user can press it. Here is a picture of how the page is supposed to look like.
The globe will be rotating and the user can login or sign-up. From what i have researched i understand that it is only possible through using a gif. But how is youtube able to achieve this? Is it possible for me to do so?
you need to add video first to your main view using Avplayer like following:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"Untitled" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
avPlayer = [AVPlayer playerWithURL:fileURL];
layer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
layer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view.layer addSublayer: layer];
Then u need to create UiView with 2 button as you specify "Login" and "Sign-Up" and make Uivew BackgroundColor Property:clear color. and add this code to place view on your main view.
CGRect viewframe=self.buttonView.frame;
viewframe.origin.x=0;
viewframe.origin.y=0;
viewframe.size.height=self.view.frame.size.height;
viewframe.size.width=self.view.frame.size.width;
self.bnuttonView.frame=viewframe;
[self.view addSubview:self.buttonView];
In this way your video will play in background and Login and Sign-up button are working in foreground.
Related
Using an MPMoviePlayerController.view as a background (think spotify). A user can tap login or signup and they are taken to the appropriate viewController, which has a clear background so that the moviePlayer.view remains as the background (i.e., user continues to see the video regardless of the currently active viewController) throughout the flow.
On some viewControllers the form needs to be lifted up so that the keyboard doesn't cover the field. I'm doing this using a transform.
The background video of the moviePlayer is set to repeat, so the video is on a continuous loop. Each time the video resets (video status goes from 1 to 2 - paused to playing) the transform resets in the child viewControllers. My initial thought was that the view was being redrawn, but this doesn't appear to be the case based on logs (I put nslogs in the drawRect of the views but it's only ever called once at instantiation).
Has anyone come across this?
My setup in the root viewController:
// lazy load moviePlayer
-(MPMoviePlayerController *)moviePlayer
{
if (_moviePlayer) return _moviePlayer;
NSURL *videoURL = [[NSBundle mainBundle] URLForResource:#"resources.bundle/videos/auth_bg" withExtension:#"mp4"];
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
_moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
_moviePlayer.repeatMode = MPMovieRepeatModeOne;
_moviePlayer.shouldAutoplay = true;
return _moviePlayer;
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.moviePlayer.view.frame = self.view.frame;
self.moviePlayer.view.hidden = false;
// 'still' is an imageView of the first frame to show while video loading
[self.navigationController.view insertSubview:self.moviePlayer.view aboveSubview:still];
}
I suspect this has to do with Autolayout -- I found a few other questions where views were being reset (one example here: https://stackoverflow.com/a/17849584/1542275) ... My solution was to adjust the layout constraint constants as opposed to transforming the view coordinates. Things are now staying put.
All of that said, I'm still not sure why the video restart is resetting the transforms.
How does one play a video as a background in iOS? I followed this question How to play a local video with Swift?, but AV Player ViewController is a controller and doesn't let me decide where and how big the view containing video is. It always takes some predefined values.
I've got this working for a project that I've done but not with an AVPlayerViewController - just a plain AVPlayer that I add to a view's layer. I have a collection view but you can just insert the video on a layer wherever you want. Try something like this:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"my_local_file" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.avPlayerItem = [AVPlayerItem playerItemWithURL:fileURL];
self.avPlayer = [AVPlayer playerWithPlayerItem:self.avPlayerItem];
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.avPlayerLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:self.avPlayerLayer below:self.collectionView.layer];
// could also try [self.view.layer insertSublayer:self.avPlayerLayer atIndex:0];
[self.avPlayer play];
Basically, set the AVPlayerLayer.frame to the size that you want.
I am trying to view a video witch has an alpha channel (the background is transparent). The only problem is that I don't seem to get how to make the background of the player transparent. I know I have to use AVplayer, but I can't access it's .view property. How can I add it to the subview and add a layer?
NSString *path = [NSString stringWithFormat:#"%#%#", [[NSBundle mainBundle] resourcePath], #"/New Project 5.m4v"];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
moviePlayer = [[AVPlayer alloc] initWithURL:filePath];
AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:moviePlayer];
self.playerLayer.frame = self.view.bounds;
moviePlayer.view.alpha = 0.3;
[moviePlayer.layer addSublayer:playerLayer];
[moviePlayer play];
The iOS SDK does not properly support alpha channel video playback. That applies for AVFramework as well as the MediaPlayer Framework. Video material that contains an alpha channel will not work as you expect it to when using Apple's API.
And as you actually show within your code, AVPlayer does not use a UIView as its surface for playing videos but a subclass of CALayer, AVLayer.
You will need to rethink your application design or chose a different playback SDK.
I want to create an app which has a default image as the background of a view that has many buttons on it. When a user clicks on any of the buttons, a unique video plays. The buttons are gonna be almost transparent so, I want the video to play in the background of the same view itself (replacing/playing above the image) and not open in a new Player(as it does using the media player). Has anyone tried it, or does anyone have any idea?
Thank You!
#import <MediaPlayer/MPMoviePlayerController.h>
MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath: moviePath]];
moviePlayer.controlStyle = MPMovieControlStyleNone;
[RootViewController.view insertSubview: moviePlayer.view aboveSubview: myView];
[moviePlayer play];
I embedded a MPMoviePlayerController on my mail view. I can play/pause the movie and seek forward/backward. But when I touch the "fullscreen button" the movie stops and the playback state is set to MPMoviePlaybackStateStopped... Should the movie be played in full screen?
Here is my code:
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoUrl];
player.shouldAutoplay = NO;
player.movieSourceType = MPMovieSourceTypeFile;
player.controlStyle = MPMovieControlStyleEmbedded;
player.allowsAirPlay = YES;
player.view.frame = CGRectMake(xPos, yPos, width, height);
[self.view addSubview:player.view];
I found my bug: when pressing the full screen toggle button in MPMoviePlayerController's view, the method "viewWillLayoutSubviews" is invoked. I could never imagine this behavior...
I hope my experience can be useful to other developers.
Remember that any containing ViewController will have its viewWillDisappear, viewDidDisappear methods invoked when the MPMoviePlayerController goes full screen. Also, viewWillAppear and viewWillDisappear get called when it comes back from full screen.
If you have any logic in there that affects video playback behavior, it'll get called unless you use some conditional logic to see if the video is still playing.