iOS:如何使用 MPMoviePlayerController

本文介绍了iOS:如何使用 MPMoviePlayerController的处理方法,对大家解决问题具有一定的参考价值

问题描述

我创建了一个空白项目 (iOS) 并将其放入我的 viewDidLoad:

I've created a blank project (iOS) and put this in my viewDidLoad:

NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
[self presentMoviePlayerViewControllerAnimated:playerController];
[playerController.moviePlayer play];

当应用程序启动时,我得到的只是一个白屏,日志中有错误消息:

When the app starts all I get is a white screen with error messages in the log:

 <Error>: CGContextSaveGState: invalid context 0x0
 <Error>: CGContextClipToRect: invalid context 0x0
 <Error>: CGContextTranslateCTM: invalid context 0x0
 <Error>: CGContextDrawShading: invalid context 0x0
 <Error>: CGContextRestoreGState: invalid context 0x0
Warning: Attempt to present <MPMoviePlayerViewController: 0x821e3b0> on <ViewController: 0x863aa40> whose view is not in the window hierarchy!

...还有一堆关于禁用自动播放的行.我特别不明白关于视图不属于层次结构的那一行,因为它是一个空白的单一视图应用程序"iOS 项目,代码在 ViewController.m 中.它位于视图层次结构中.

...and a bunch of lines regarding disabling autoplay. I especially don't understand the line about the view not being part of the hierarchy since it's a blank "Single View Application" iOS project and the code is in ViewController.m. It IS in the view hierarchy.

我知道电影文件本身不是问题,因为我是从 Apple 在 MPMoviePlayer 上的示例代码中得到的.尽管我(似乎)尝试了示例中编写的所有内容,但我还是无法让播放器正常工作.

I know for a fact that the movie file itself is not the problem because I got it from Apple's sample code on MPMoviePlayer. And although I (seemingly) tried everything written in the sample, I just couldn't get the player to work.

这是另一个尝试,这次使用 MPMoviePlayerController(不是 MPMoviePlayerViewController):

Here is another try, this time with MPMoviePlayerController (not MPMoviePlayerViewController):

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player setContentURL:url];
[player setMovieSourceType:MPMovieSourceTypeFile];

[[player view] setFrame:self.view.bounds];
[player view].backgroundColor = [UIColor greenColor];

player.scalingMode = MPMovieScalingModeNone;
player.controlStyle = MPMovieControlModeDefault;
player.backgroundView.backgroundColor = [UIColor whiteColor];
player.repeatMode = MPMovieRepeatModeNone;

[self.view addSubview: [player view]];
[player play];

类似的结果,有白屏和错误.请帮忙....

Similar result, with white screen and errors. Please help....

推荐答案

原来我们要做的就是:

NSURL *movieURL = [NSURL URLWithString:@"http://example.com/somefile.mp4"];
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:movieController];
[movieController.moviePlayer play];

  • movieController 是在 .h 文件中声明的 MPMoviePlayerViewController 的一个实例.

    • movieController is an instance of MPMoviePlayerViewController declared in the .h file.

      重要:在定义 URL 时,如果您想通过网络访问文件并使用 NSURL 的 fileUrlWithPath 方法,请使用 NSURL 的 URLWithString 方法> 如果您在本地拥有该文件!

      Important: when defining the URL use NSURL's URLWithString method if you want to access the file through a network and use NSURL's fileUrlWithPath if you have the file locally!

      [movieController.movi​​ePlayer play] 不是必需的,无论您没有将自动播放设置为否,播放器都会启动,但我观察到如果你把 play 放在里面,它开始得更快一点.这可能只是巧合.

      [movieController.moviePlayer play] is not required and the player will start regardless if you didn't set autoplay to NO, but I observed that if you put play in it it starts a bit quicker. This could be just a coincidence.

      如果您想知道用户何时点击完成按钮(播放器将自动关闭),您应该知道 -viewDidAppear 在当玩家被解散时出现的视图控制器.您可以在播放器启动时设置 BOOL 变量并检查 -viewDidAppear 中的 BOOL 以便您知道 -viewDidAppear 被调用是因为玩家被解雇了.或者,您可以注册 MPMoviePlayerDidExitFullScreen 通知,但这对我不起作用.

      If you want to know when the user tapped the done button (the player will be dismissed automatically) you should know that -viewDidAppear is called on the view controller that appears when the player is dismissed. You could set a BOOL variable when the player starts and check for the BOOL in your -viewDidAppear so that you know that -viewDidAppear was called becaouse the player was dismissed. Alternatively you can register for MPMoviePlayerDidExitFullScreen notification, but that didn't work for me.

      或者,如果这不起作用,您可以执行以下操作

      self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"something" ofType:@"mp4"]]];
      [self.moviePlayer.view setFrame:CGRectMake(0, 0, 320, 320)];
      [self.moviePlayer play];
      [self.view addSubview:self.moviePlayer.view];
      

      • self.movi​​eplayer 是 MPMoviePlayerController(不是 MPMoviePlayerViewController)的一个实例.根据我的经验,将它声明为一个属性很重要(像这样:@property (strong, nonatomic) MPMoviePlayerController *moviePlayer;)而不是一个简单的 ivar,因为有时它只是不工作,如果它是一个 ivar

        • self.movieplayer is an instance of MPMoviePlayerController (not MPMoviePlayerViewController). In my experience it's important to declare it as a property (like so: @property (strong, nonatomic) MPMoviePlayerController *moviePlayer;) rather than a simple ivar, because sometimes it just doesn't work if it's an ivar

          设置frame也很重要,因为如果我们不设置,视频根本就不会出现.框架可以是任何东西,只要您定义的内容在您的视图范围内

          setting the frame is also important, because if we don't set it, the video will not appear at all. The frame can be anything as long as what you define is within the bounds of your view

          重要:如上所述,如果您想通过网络访问文件,请在定义 URL 时使用 NSURL 的 URLWithString 方法并使用 NSURL 的 fileUrlWithPath 如果你有文件本地

          Important: As above, when defining the URL use NSURL's URLWithString method if you want to access the file through a network and use NSURL's fileUrlWithPath if you have the file locally!

          这篇关于iOS:如何使用 MPMoviePlayerController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1169

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1069

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this-&gt;id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:806

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this-&gt;id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:898

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:930

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:876

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:864

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:833

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1721

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1308