如何创建一个按钮作为 MPMoviePlayerController 的子视图?
How to create a button as a subview of MPMoviePlayerController?
我正在尝试添加自定义播放按钮,但无法将其放在视频前面。这是我的代码。
- (IBAction)playMovie:(UIButton *)sender {
NSURL *url = [[NSURL alloc] initWithString:urlStr.text];
self.movie = [[MPMoviePlayerController alloc]initWithContentURL:url];
self.movie.controlStyle = MPMovieControlStyleNone;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
button.backgroundColor=[UIColor yellowColor];
[button setTitle:@"Normal State Title" forState:UIControlStateNormal];
[movie.view addSubview:button];
self.movie.shouldAutoplay = YES;
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:NO];
}
我想这就是您要找的。这是一个简单的例子。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
url=[NSURL URLWithString:@"https://scontent.cdninstagram.com/hphotos-xaf1/t50.2886-16/11179443_819874424728492_389701720_n.mp4"];
movPlayer=[[MPMoviePlayerController alloc] init];
[movPlayer setContentURL:url];
[movPlayer setMovieSourceType:MPMovieSourceTypeFile];
[movPlayer.view setFrame:CGRectMake(_imgVw.frame.origin.x, _imgVw.frame.origin.y, _imgVw.frame.size.width, _imgVw.frame.size.height)];
[movPlayer prepareToPlay];
movPlayer.controlStyle = MPMovieControlStyleNone;
movPlayer.fullscreen = NO;
movPlayer.shouldAutoplay=NO;
[movPlayer setScalingMode:MPMovieScalingModeAspectFill];
[self.view addSubview:movPlayer.view];
UIButton *pauseButton=[[UIButton alloc]init];
[pauseButton setFrame:CGRectMake(100,220, 50, 50)];
[pauseButton setBackgroundColor:[UIColor yellowColor]];
[pauseButton setTag:123456789];
[pauseButton setAccessibilityValue:@"445"];
[pauseButton setAccessibilityLabel:@"445"];
[pauseButton addTarget:self action:@selector(pauseBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pauseButton];
}
-(void)pauseBtnClicked:(UIButton*)btn
{
NSLog(@"clicked on pause button");
UIButton *pause = (UIButton*)[self.view viewWithTag:123456789];
NSLog(@"clicked on pause button");
if ([pause.accessibilityValue intValue] == 455)
{
NSLog(@"Stop");
[movPlayer stop];
[pause setBackgroundColor:[UIColor yellowColor]];
[pause setAccessibilityValue:@"445"];
}
else if ([pause.accessibilityValue intValue] == 445)
{
NSLog(@"Play");
[movPlayer play];
[pause setBackgroundColor:[UIColor brownColor]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:movPlayer];
[pause setAccessibilityValue:@"455"];
}
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
NSLog(@"video playing is completed");
MPMoviePlayerController *moviePlayerController = (MPMoviePlayerController*)[notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController stop];
int pauseViewTag = 123456789;
UIButton *pause = (UIButton*)[self.view viewWithTag:pauseViewTag];
[pause setBackgroundColor:[UIColor yellowColor]];
}
我正在尝试添加自定义播放按钮,但无法将其放在视频前面。这是我的代码。
- (IBAction)playMovie:(UIButton *)sender {
NSURL *url = [[NSURL alloc] initWithString:urlStr.text];
self.movie = [[MPMoviePlayerController alloc]initWithContentURL:url];
self.movie.controlStyle = MPMovieControlStyleNone;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
button.backgroundColor=[UIColor yellowColor];
[button setTitle:@"Normal State Title" forState:UIControlStateNormal];
[movie.view addSubview:button];
self.movie.shouldAutoplay = YES;
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:NO];
}
我想这就是您要找的。这是一个简单的例子。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
url=[NSURL URLWithString:@"https://scontent.cdninstagram.com/hphotos-xaf1/t50.2886-16/11179443_819874424728492_389701720_n.mp4"];
movPlayer=[[MPMoviePlayerController alloc] init];
[movPlayer setContentURL:url];
[movPlayer setMovieSourceType:MPMovieSourceTypeFile];
[movPlayer.view setFrame:CGRectMake(_imgVw.frame.origin.x, _imgVw.frame.origin.y, _imgVw.frame.size.width, _imgVw.frame.size.height)];
[movPlayer prepareToPlay];
movPlayer.controlStyle = MPMovieControlStyleNone;
movPlayer.fullscreen = NO;
movPlayer.shouldAutoplay=NO;
[movPlayer setScalingMode:MPMovieScalingModeAspectFill];
[self.view addSubview:movPlayer.view];
UIButton *pauseButton=[[UIButton alloc]init];
[pauseButton setFrame:CGRectMake(100,220, 50, 50)];
[pauseButton setBackgroundColor:[UIColor yellowColor]];
[pauseButton setTag:123456789];
[pauseButton setAccessibilityValue:@"445"];
[pauseButton setAccessibilityLabel:@"445"];
[pauseButton addTarget:self action:@selector(pauseBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pauseButton];
}
-(void)pauseBtnClicked:(UIButton*)btn
{
NSLog(@"clicked on pause button");
UIButton *pause = (UIButton*)[self.view viewWithTag:123456789];
NSLog(@"clicked on pause button");
if ([pause.accessibilityValue intValue] == 455)
{
NSLog(@"Stop");
[movPlayer stop];
[pause setBackgroundColor:[UIColor yellowColor]];
[pause setAccessibilityValue:@"445"];
}
else if ([pause.accessibilityValue intValue] == 445)
{
NSLog(@"Play");
[movPlayer play];
[pause setBackgroundColor:[UIColor brownColor]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:movPlayer];
[pause setAccessibilityValue:@"455"];
}
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
NSLog(@"video playing is completed");
MPMoviePlayerController *moviePlayerController = (MPMoviePlayerController*)[notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController stop];
int pauseViewTag = 123456789;
UIButton *pause = (UIButton*)[self.view viewWithTag:pauseViewTag];
[pause setBackgroundColor:[UIColor yellowColor]];
}