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

5

我尝试在单独的UIView中播放视频,这样我就可以留出一些空间供控制工具使用,比如开始/停止等按钮,所以我写了以下代码:
首先,我创建了一个名为Single View Application的iOS项目,然后我在storyboard中添加了一个View,最后,我基于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];

}

好的,我来翻译一下。这段内容与IT技术有关,提到了在iOS上无法看到视频,只能听到声音,作者对iOS还不是很熟悉,希望得到一些提示。



你使用的视频格式是什么?尝试使用本地播放器播放视频,看看是否可以播放。 - Johnykutty
从上面的代码清楚地可以看出格式是mp4文件。 - liunx
1
什么是videoView?它只是UiView吗? - coolcool1994
2个回答

6

看起来您忘记将videoView添加为子视图。请尝试:

[self.view addSubview: videoView];

1

[self.view addSubview:videoView];


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接