如何播放UIView中嵌入的视频?

时间:2014-02-16 16:00:55

标签: ios iphone objective-c uiview

我尝试在单独的UIView中播放视频,这样我就可以留出一些空间用于控制工具,例如开始/停止按钮等,所以我写这样的代码:
首先,我创建一个单视图应用程序的iOS项目,然后我将一个视图添加到故事板,最后,我添加了一个名为VideoView的新的基于UIView的类,然后我将View关联到类VideoView,我在下面写了一些代码:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize moviePlayer;

- (IBAction)playMovie:(id)sender
{
    VideoView *videoView = [[VideoView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];

    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"Sanda" ofType:@"mp4"];
    NSURL *videoURL = [NSURL fileURLWithPath:videoPath];
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
    //[moviePlayer.view setFrame:CGRectMake(0, 0, 320, 476)];
    [moviePlayer.view setFrame:videoView.bounds];
    [videoView addSubview:moviePlayer.view];

    [moviePlayer prepareToPlay];
    [moviePlayer play];

}

好吧,我构建并运行它,结果是我可以听到声音但没有看到视频,我真的是iOS的新来者,所以任何提示?

2 个答案:

答案 0 :(得分:6)

您似乎忘了将videoView添加为子视图。尝试

[self.view addSubview: videoView];

答案 1 :(得分:1)

[self.view addSubview:videoView];