MPMoviePlayerController播放在视频结束之前终止 [英] MPMoviePlayerController playback terminates before video finishes

查看:264
本文介绍了MPMoviePlayerController播放在视频结束之前终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解此类并使其正常工作时遇到困难,这是我使用它的代码段:

I'm having trouble understanding this class and getting it to work properly, here is the piece of code where I use it:

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:_videoURL];
UIImage *videoThumbnail = [moviePlayer thumbnailImageAtTime:0 timeOption:MPMovieTimeOptionNearestKeyFrame];
[lastImageView setImage:videoThumbnail];

[moviePlayer setControlStyle:MPMovieControlStyleNone];
[moviePlayer setShouldAutoplay:YES];
[moviePlayer prepareToPlay];

[moviePlayer.view setFrame:lastImageView.frame];
moviePlayer.view.transform = CGAffineTransformMakeRotation((90*M_PI)/180);
[self.view addSubview:moviePlayer.view];

[moviePlayer play];

videoThumbnail行仍然存在的唯一原因是,直到尝试将它从那里获取图像之后,我才开始播放视频,然后突然开始工作. . 有点. 现在播放2-3秒,然后终止而不发送MPMoviePlayerPlaybackDidFinishNotification或MPMoviePlayerPlaybackStateDidChangeNotification 我在Google上搜索了一下,找不到任何有用的提示,有人可以告诉我是怎么回事或我忘记了什么

The only reason why the videoThumbnail line is still there is because i didn't get the video to play until I was just trying it out to see if it would get the image from there and then it suddenly began to work... sort of. Now it plays for 2-3 secs and then terminates without sending MPMoviePlayerPlaybackDidFinishNotification or MPMoviePlayerPlaybackStateDidChangeNotification I googled around a bit and couldn't find any useful tips, could someone tell me what's wrong or what i am forgetting

推荐答案

如果您没有将新创建的MPMoviePlayerController实例分配给具有局部范围(moviePlayer)的变量以外的其他任何对象,那么电影播放器​​将在电影开始播放之前将其释放. (我想thumbnailImageAtTime调用会使它保持更长的时间.)

If you're not assigning the newly created MPMoviePlayerController instance to anything other than a variable with local scope (moviePlayer), then the movie player will be deallocated before the movie gets playing. (I imagine the thumbnailImageAtTime call keeps it around for a bit longer.)

尝试将电影播放器​​实例分配给保留的(强)实例变量或属性.当然,它应该在完成后释放,因为多个电影播放器​​实例不能很好地一起播放.

Try assigning the movie player instance to a retained (strong) instance variable or property. Of course it should be released when finished, as multiple movie player instances don't play well together.

此外,请注意,从iOS 5开始,需要调用prepareToPlay.以下摘自Matt Neuberg的 Programming iOS 5,第二版的第28章:

Also, note that, as of iOS 5, calling prepareToPlay is required. The following is from chapter 28 of Matt Neuberg's Programming iOS 5, Second Edition:

在可以使用MPMoviePlayerController在界面中显示电影之前,必须调用通过MPMediaPlayer协议(由MPMoviePlayerController采用)提供的prepareToPlay.此要求是iOS 5中的新增功能,与以前版本的系统有很大不同;如果您未执行此调用,则您的旧代码可能会中断.

Before you can display a movie in your interface with an MPMoviePlayerController, you must call prepareToPlay, which is supplied through the MPMediaPlayer protocol (adopted by MPMoviePlayerController). This requirement is new in iOS 5, and is a major difference from previous versions of the system; your old code can break if it didn’t make this call.

这篇关于MPMoviePlayerController播放在视频结束之前终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆