首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加载UIView时立即播放视频

加载UIView时立即播放视频
EN

Stack Overflow用户
提问于 2010-12-20 21:32:03
回答 4查看 3.8K关注 0票数 0

你好!我正在编写一个iPad应用程序,我需要能够在UIView加载时播放视频。但是,在初始化MPMoviePlayerController之后,如果我尝试在任何地方发送消息,就会得到一个MPMoviePlayerController。我从*.h文件中删除了MPMediaPlayerController,然后在实现文件中声明了它,现在我在代码下面的底部得到了消息。在构建和分析内存泄漏(或任何问题)方面没有任何问题,而且我找不到关于这方面的任何帖子。这是我的密码:

代码语言:javascript
复制
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }

 NSString *url = [[NSBundle mainBundle] pathForResource:@"p0600c0100cmpintro" ofType:@"m4v"];
 MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
 NSLog(@"%@", movie);
 movie.view.frame = CGRectMake(5, 0, 1035, 768);
 movie.view.contentMode = UIViewContentModeScaleToFill;
 [[movie view] setCenter:CGPointMake(movie.view.center.x-10, movie.view.center.y)];
 [movie setControlStyle:MPMovieControlStyleNone];
    [movie setShouldAutoplay:YES];
 [[self view] addSubview:[movie view]];

    return self;
    }

“NSLog”的"MPMoviePlayerController: 0x1b77f0",但是崩溃时的错误消息是"* -MPMoviePlayerController playbackState: message发送给解除分配的实例0x1473a0“。帮助?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-01-25 00:03:59

几个星期前,我找到了解决办法,忘记了这篇文章。我没有成功地发布MPMoviePlayerController。对于那些好奇的人,为了发布一个MPMoviePlayerController,我们必须首先从NSNotificationCenter中删除通知(如果设置了一个通知),停止电影(即使它已经播放完成),然后释放它。我之前没有在应用程序中使用我的第一个MPMoviePlayerController来执行这个操作,所以它试图引用已释放的实例。当电影完成播放时,下面是成功发行电影的代码:

代码语言:javascript
复制
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:movie];
[movie.view removeFromSuperview];
[movie stop];
[movie release];
票数 0
EN

Stack Overflow用户

发布于 2010-12-20 22:27:25

根据文档,它看起来像电影视图的框架需要匹配它的父视图的视图。还可以尝试将代码从initWithNibName:bundle中移出到viewDidLoad中:

代码语言:javascript
复制
- (void)viewDidLoad {
     [super viewDidLoad];

     UIView *movieContainer = [[UIView alloc] initWithFrame:CGRectMake(5, 0, 300, 400)];
     //Do any other positioning of the view you would like

     NSString *path = [[NSBundle mainBundle] pathForResource:@"p0600c0100cmpintro" ofType:@"m4v"]; 
     NSURL *url = [NSURL fileURLWithPath:path];
     MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
     movie.view.frame = movieContainer.bounds;  //Make sure this is the bounds of its parent view
     movie.scalingMode = MPMovieScalingModeFill;
     movie.controlStyle = MPMovieControlStyleNone;
     movie.shouldAutoplay = YES;

     [movieContainer addSubview:movie.view];
     [self.view addSubview:movieContainer];

     [movieContainer release];
}

最后一个建议是保留对电影的引用,这样一旦视图控制器被取消分配,您就可以将其释放。

票数 0
EN

Stack Overflow用户

发布于 2010-12-20 22:47:32

我建议在viewDidLoad中创建您的viewDidLoad,然后在viewDidAppear中制作电影,以获得最佳效果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4494168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档