如何使用MPMoviePlayerController播放视频? [英] How to play video using MPMoviePlayerController?

查看:94
本文介绍了如何使用MPMoviePlayerController播放视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码使用MPMoviePlayerController播放视频,但是无法播放视频.谁能告诉我为什么?

I am using the following code to play the video using the MPMoviePlayerController , but the video is not played. Can anyone tell me why ?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];

NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];

    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];

    [[moviePlayer view] setFrame:[[self view] bounds]];
    [[self view] addSubview: [moviePlayer view]];


    moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

    [moviePlayer play];

推荐答案

这很奇怪,但是如果将MPMoviePlayerController设置为属性而不是局部变量,则似乎可以正常工作.似乎幕后正在发生某些事情.我认为这与ARC有关.您在使用ARC吗?

It's pretty weird, but it seems to work okay if you make your MPMoviePlayerController a property instead of a local variable. Seems something is going on behind the scenes. I'm thinking it's related to ARC. Are you using ARC?

这也是您过度添加路径的问题:

It's also an issue that you've over-appended your path:

// You've already got the full path to the documents directory here.
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];
// Now you're appending the full path to the documents directory to your bundle path
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];

当我在模拟器中运行您的代码时,路径如下所示:

When I run your code in the simulator, the path looks like this:

/用户/mlong/库/应用程序支持/iPhone 模拟器/5.1/应用程序/8CFB9B94-BD6A-442C-A525-573FE343506D/VidoePlayer.app/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4

/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/VidoePlayer.app/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4

应该就是这样:

/用户/mlong/库/应用程序支持/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4

/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4

所以只需删除这一行:

NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];

然后将您的播放器实例更改为:

And then change your player instantiation to this:

_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];

[[_moviePlayer view] setFrame:[[self view] bounds]];
[[self view] addSubview: [_moviePlayer view]];

_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

[_moviePlayer play];

因此,您应该将MPMoviePlayerController添加为包含的视图控制器的属性.

So you should add the MPMoviePlayerController as a property of your containing view controller.

这篇关于如何使用MPMoviePlayerController播放视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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