如何在 MPMoviePlayer Controller 上添加 UIImageView |苹果 |目标C.



我有一个控制器,我可以使用MPMoviePlayerController显示视频。我需要在视频上放一张图片。

我正在尝试使用以下代码,但它没有显示。我错过了什么?

// method to play the video
- (void)playVideoInLoopMode:(BOOL)loop {
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myvideo" ofType:@"m4v"]];
    MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:url];
    mp.controlStyle = MPMovieControlStyleNone;
    if (loop) {
        mp.repeatMode = MPMovieRepeatModeOne;        
    }
    mp.view.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height);
    self.player = mp;
    [self.view addSubview:self.player.view];
    [self.player prepareToPlay];
    [self.player play];
}
// method to add the image
- (void) addImageLayer {
    image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myimage"]];
    image.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:image];
}

首先,我使用以下方法运行视频:[self playVideoInLoopMode:YES],5秒后,我尝试使用[self addImageLayer]方法放置图像层;

在我的AppDelegate.h中,我在didFinishLaunchingWithOptions中有以下代码:

 MyViewController *myVC = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil];
    [self.window addSubview:myVC.view];
    [self.window makeKeyAndVisible];

尝试将图像视图添加到 mp.view,然后将 mp.view 添加到常规视图中例如,如果图像始终相同,则可以将其添加到起始代码中并删除添加视图的方法...

// method to play the video
- (void)playVideoInLoopMode:(BOOL)loop 
{
   NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myvideo" ofType:@"m4v"]];
   MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:url];
   mp.controlStyle = MPMovieControlStyleNone;
   if (loop) 
   {
       mp.repeatMode = MPMovieRepeatModeOne;        
   }
   mp.view.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height);
   self.player = mp;
   [self.view addSubview:self.player.view];
   image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myimage.png"]];
   image.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
   [self.player addSubview:image];
   [self.player prepareToPlay];
   [self.player play];
}

最新更新