如何使用MPMoviePlayerController播放视频?

时间:2012-05-30 10:17:04

标签: iphone objective-c ios mpmovieplayercontroller mpmovieplayer

我使用以下代码使用MPMoviePlayerController播放视频,但视频未播放。谁能告诉我为什么?

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];

7 个答案:

答案 0 :(得分:20)

这很奇怪,但是如果你将MPMoviePlayerController设为属性而不是局部变量,它似乎工作正常。似乎在幕后发生了一些事情。我认为这与ARC有关。你在使用ARC吗?

这也是你过度追加道路的问题:

// 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];

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

  

/ Users / mlong / Library / Application Support / iPhone   模拟器/ 5.1 /应用程序/ 8CFB9B94-BD6A-442C-A525-573FE343506D / VidoePlayer.app / Users / mlong / Library / Application Support / iPhone   模拟器/ 5.1 /应用/ 8CFB9B94-BD6A-442C-A525-573FE343506D /文档/ one.mp4

应该是这样的:

  

/ Users / mlong / Library / Application Support / iPhone   模拟器/ 5.1 /应用/ 8CFB9B94-BD6A-442C-A525-573FE343506D /文档/ one.mp4

所以只需删除此行:

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

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

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

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

_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

[_moviePlayer play];

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

答案 1 :(得分:3)

好吧,应用程序包和文档目录之间存在很大差异。我建议你看一下。

首先,视频存储在哪里?

如果您的视频位于文档目录中,请不要将文档目录路径附加到捆绑路径。

试试filePath变量:

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

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL filePath]];

但是,如果文件位于应用程序包中(您在XCode中将其添加到项目中),则应使用jinx响应中的内容。

答案 2 :(得分:1)

moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];  
        [moviePlayer.view setFrame:CGRectMake(//set rect frame)];
        moviePlayer.controlStyle =  MPMovieControlStyleDefault; 
        moviePlayer.shouldAutoplay=YES;
        moviePlayer.repeatMode = NO;  
        [moviePlayer setFullscreen:YES animated:NO]; 
        [moviePlayer prepareToPlay];
[self.view addsubview:movieplayer.view];

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];


- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification {

    if ((moviePlayer.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK) {
//add your code


    }
}

答案 3 :(得分:0)

尝试直接询问您的捆绑包,而不是手动设置文件路径

NSString *path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"mov"];

答案 4 :(得分:0)

播放器初始化为我们想要播放的视频的URL(它可以是设备的本地文件的路径或实时URL。)在将该播放器添加为当前视图的子视图之后。

MPMoviePlayerController类支持的视频格式如下

  1. .mov .mpv .3gp .mp4
  2. 我不确定这篇文章对你有多大帮助。我是新来的。我参与了 this article

    中提供的分步说明

答案 5 :(得分:-1)

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];

希望帮助它

答案 6 :(得分:-1)

我花了几分钟来调试问题,但答案很简单。这是交易:

  • 如果您想从Web URL播放MPMoviePlayerViewController,请使用以下命令: NSURL * url = [NSURL URLWithString:@“https://www.test.com/anyMovie.mp4”];

  • 如果你想让MPMoviePlayerViewController从App Bundle播放它,请使用: NSString * moviePath = [[NSBundle mainBundle] pathForResource:@“anyMovie”ofType:@“m4v”]; NSURL * url = [NSURL fileURLWithPath:moviePath];

除了你必须按如下方式设置这个属性“movieSourceType”之外,它的其余部分是相同的:

MPMoviePlayerViewController *moviePlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
moviePlayerView.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentViewController:moviePlayerView animated:YES completion:^{}];