我正在尝试添加一个 UISlider 来控制 AVPlayerViewController 中的音量,为此我在“AVPlayerViewController.contentOverlayView”上添加了一个 UISlider。UISlider 不允许我拖动,就好像它没有启用一样,并且它的选择器操作方法“changeVolumeLevelOfPlayer”没有被调用。
这是我的代码:
-(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;
}