Using AVPlayer to view transparent video - ios

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.

Related

defining view frame size for AV Player ViewController in ios

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.

Embedding video into view controller

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.

iOS AVPlayer won't play

I have the following code set up to play a video in my iOS app, but it just doesn't play. The app compiles and runs, but all I get a red frame where I positioned it. When debugging, I found that the the program doesn't even step into the last line [player play]. Also, the video runs fine in a UIWebView.
NSString *streamingString = [NSString stringWithFormat:#"http://youtu.be/...."];
AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:streamingString]];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
[layer setPlayer:player];
[layer setFrame:CGRectMake(50, 50, 400, 300)];
[layer setBackgroundColor:[UIColor redColor].CGColor];
[layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[self.view.layer addSublayer:layer];
[player play];
I am a new programmer and this is my first post to Stackoverflow, so please excuse me if I have not given enough information or am missing something obvious! Thank you so much!
assetWithURL: is often, but not always, used with a URL that points to a file already on the device, often within the bundle. If you are using an external URL, such as one on the internet, the url must resolve to a video that is in a format that the AVPlayer can understand. A URL to a YouTube web page will not work. In general, you can't play a YouTube video in AVPlayer.
Check out this document from Apple and this video on YouTube.
EDIT:
Some developers have used this library to play a YouTube video in their app. Another option is to use a UIWebView.

Play video with transparent background IOS [duplicate]

This question already has answers here:
iOS - How to play a video with transparency?
(8 answers)
Closed 9 years ago.
I would like to play a video with a transparent background. The only thing is that all the information I found on the internet is "how to make the video background transparent", but the background of my video is already transparent, now I need to make the background of the player transparent. How do I do that? I tried this:
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:#"New Project 5" ofType:#"m4v"];
NSLog(#"%#",resourcePath);
NSURL *url = [NSURL fileURLWithPath:resourcePath];
NSLog(#"%#",url);
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.shouldAutoplay=YES;
moviePlayer.controlStyle = MPMovieControlStyleNone;
[moviePlayer setFullscreen:NO animated:YES];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(200, 600, 400, 300);
[moviePlayer play];
moviePlayer.view.backgroundColor = [UIColor clearColor];
for(UIView *aSubView in moviePlayer.view.subviews) {
aSubView.backgroundColor = [UIColor clearColor];
}
But it doesn't work. It displays a black background.
Here is a link with a real solution to this problem playing-movies-with-an-alpha-channel-on-the-ipad. Her blog post is talking about video on the iPad, but the same library with alpha channel support also works on iPhone. It is also possible to encode to h.264 with alpha channel support using the method described in the blog post comments.

Play Video as the Background on iPhone on button click?

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];

Resources