Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISlider added on AVPlayerViewController contentOverlayView is not responding

I am trying to add a UISlider to control volume in AVPlayerViewController, for this i have added a UISlider on 'AVPlayerViewController.contentOverlayView'. UISlider is not allowing me to drag as if it is not enabled and its selector action method 'changeVolumeLevelOfPlayer' is not getting called.

Here is my code:

-(void)viewDidLoad {

 [super viewDidLoad];

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

 [[AVAudioSession sharedInstance] setActive:YES error:nil];

 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

 AVPlayerViewController *avPlayerViewController = [[AVPlayerViewController alloc] init];

 AVPlayer *player = [AVPlayer playerWithURL:[self getVideoURLAtIndex:[self.indexOfCurrentTrailer intValue]]];

 avPlayerViewController.player = player;

 avPlayerViewController.delegate = self;

 [player play];

 avPlayerViewController.view.frame = CGRectMake(0, 0, 300, 250);

 UISlider *sliderForVolumeControl = [[UISlider alloc] initWithFrame:CGRectMake(0, avPlayerViewController.view.frame.size.height/2, 100, 40)];

 [sliderForVolumeControl setMinimumValue:0.0];

 [sliderForVolumeControl setMaximumValue:1.0];

 [sliderForVolumeControl setValue:0.5];

 sliderForVolumeControl.continuous = YES;

 [sliderForVolumeControl setBackgroundColor:[UIColor orangeColor]];

 [sliderForVolumeControl addTarget:self action:@selector(changeVolumeLevelOfPlayer:) forControlEvents:UIControlEventValueChanged];

 [sliderForVolumeControl setUserInteractionEnabled:YES];

 [sliderForVolumeControl setEnabled:YES];

 [avPlayerViewController.contentOverlayView addSubview:sliderForVolumeControl];

 [self addChildViewController:avPlayerViewController];

 [self.view addSubview:avPlayerViewController.view];

 [avPlayerViewController didMoveToParentViewController:self];

 [player addObserver:self forKeyPath:@"status" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial) context:nil];

}

 -(void)changeVolumeLevelOfPlayer:(UISlider *)sender {

    player.volume = sender.value;

 }
like image 864
Suraj Mirajkar Avatar asked Sep 25 '22 19:09

Suraj Mirajkar


1 Answers

From the apple docs:

Use the content overlay view to add additional custom views between the video content and the controls.

So it looks like this: (with a bunch of other layers over your UISlider)

enter image description here

To make it work just change this line:

[avPlayerViewController.contentOverlayView addSubview:sliderForVolumeControl];

to this:

[avPlayerViewController.view addSubview:sliderForVolumeControl];
like image 108
Segev Avatar answered Nov 04 '22 19:11

Segev