如何在MPMoviePlayerController上添加UIImageView iOS |目标C.

时间:2012-06-20 05:34:04

标签: ios uiimageview mpmovieplayercontroller

我有一个控制器,我使用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];

1 个答案:

答案 0 :(得分:2)

尝试将图像视图添加到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];
}