3

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];
Alessandro
  • 4,000
  • 12
  • 63
  • 131
  • This can be done with AVFoundation and CoreImage. Details here: https://medium.com/@quentinfasquel/ios-transparent-video-with-coreimage-52cfb2544d54 – nerk Apr 10 '18 at 23:49

1 Answers1

3

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.

Till
  • 27,559
  • 13
  • 88
  • 122
  • Till is correct that alpha channel is not supported by default, but there are a couple of workarounds listed here: http://stackoverflow.com/questions/1401517/iphone-sdk-how-to-play-a-video-with-transparency – MoDJ Jul 25 '13 at 22:43