我尝试在单独的 UIView 中播放视频,这样我就可以为控制实用程序(例如开始/停止按钮等)留出一些空间,所以我编写了如下代码:
首先,我创建一个单 View 应用程序的 iOS 项目,然后我将一个 View 添加到 Storyboard,最后,我附加一个新的基于 UIView 的类,名为 VideoView,然后我将 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 新手,所以有什么提示吗?
最佳答案
您似乎忘记将 videoView 添加为 subview 。尝试
[self.view addSubview: videoView];
关于ios - 如何播放嵌入在 UIView 中的视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21813624/