5

我正在尝试添加一个 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;

 }
4

1 回答 1

4

来自苹果文档:

使用内容覆盖视图 在视频内容和控件之间添加额外的自定义视图。

所以它看起来像这样:(在你的 UISlider 上有一堆其他层)

在此处输入图像描述

要使其正常工作,只需更改此行:

[avPlayerViewController.contentOverlayView addSubview:sliderForVolumeControl];

对此:

[avPlayerViewController.view addSubview:sliderForVolumeControl];
于 2016-01-05T10:02:40.690 回答