ios - 如何在 MPMoviePlayerController 上添加 UIImageView | iOS | objective-c

标签 ios uiimageview mpmovieplayercontroller

我有一个 Controller ,可以使用 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];

最佳答案

尝试将 ImageView 添加到 mp.view,然后将 mp.view 添加到常规 View 例如,如果图像始终相同,您可以将其添加到起始代码中并删除添加 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];
}

关于ios - 如何在 MPMoviePlayerController 上添加 UIImageView | iOS | objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11113375/

相关文章:

ios - 带有多个 Youtube 视频的 UIWebview,获取正在播放哪个视频的通知

ios - cell.textlabel.text 无法正常工作

ios - 如何使顶部图像不透明,以便背景图像不显示出来?

ios - NSFileManager.defaultManager().fileExistsAtPath 在使用 iOS 设备时返回 false

iphone - MpMovieplayer 在提醒警报弹出期间崩溃

ios - 应用程序崩溃 setObject ForKey : object cannot be nil

ios - 如何在没有库的情况下从上到下制作自定义 iOS 分段控件?

ios - iOS 11 中如何判断是否录屏

ios - UIWebView URL拦截

iOS:动态更新启动屏幕