How to play a transparent background video of .flv / .mp4 format in iOS application?

1.5k views Asked by At

How to play a transparent background video of .flv / .mp4 format in iOS application?

I am getting black background instead of transparent background when I play the video using MPMoviePlayerController.

Any idea how to play with transparent background?

            MPMoviePlayerController *player = [[MPMoviePlayerController alloc] init];
            player.controlStyle = MPMovieControlStyleNone;
            player.repeatMode = MPMovieRepeatModeOne;
            player.view.frame = videoImageView.frame;
            player.shouldAutoplay = YES;
            for(UIView* subV in player.view.subviews) {
                subV.backgroundColor = [UIColor clearColor];
            }
            for(UIView* subV in player.backgroundView.subviews) {
                subV.backgroundColor = [UIColor clearColor];
            }
            player.view.backgroundColor = [UIColor clearColor];
            player.backgroundView.backgroundColor = [UIColor clearColor];

            player.view.superview.backgroundColor = [UIColor clearColor];
            player.backgroundView.superview.backgroundColor = [UIColor clearColor];

            player.view.layer.backgroundColor = [[UIColor clearColor]CGColor];
            player.backgroundView.layer.backgroundColor = [[UIColor clearColor]CGColor];

I had also Used AVPlayer and it is also showing black background

        avAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Video_Thumb_01" ofType:@"mp4"]]];
        avPlayerItem =[[AVPlayerItem alloc]initWithAsset:avAsset];
        avPlayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];
        avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:avPlayer];
        [avPlayerLayer setFrame:videoImageView.frame];
        [avPlayerLayer setBackgroundColor:[[UIColor clearColor]CGColor]];
        [avPlayer seekToTime:kCMTimeZero];
        [avPlayer play];
1

There are 1 answers

3
Amandir On

MPMoviePlayerController is displayed in it's own context. If you want to add a video to a UIViewcontroller (and I guess that's what you want as you want to deisplay something below the video), you should use AVPLayer to play the Video and use AVPlayerLayer to add the Video to your View.