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;
}
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)
To make it work just change this line:
[avPlayerViewController.contentOverlayView addSubview:sliderForVolumeControl];
to this:
[avPlayerViewController.view addSubview:sliderForVolumeControl];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With