Alessandro
Alessandro

Reputation: 4100

Using AVPlayer to view transparent video

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

Upvotes: 4

Views: 8050

Answers (1)

Till
Till

Reputation: 27597

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.

Upvotes: 3

Related Questions