如何使用MPMoviePlayerController播放video?

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

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吗?

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

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

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

 _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]]; [[_moviePlayer view] setFrame:[[self view] bounds]]; [[self view] addSubview: [_moviePlayer view]]; _moviePlayer.scalingMode = MPMovieScalingModeAspectFit; [_moviePlayer play]; 

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

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

首先,video存储在哪里?

如果您的video位于文档目录中,请不要将文档目录路径附加到包路径。

只需尝试使用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响应中的内容。

 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 } } 

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

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

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

MPMoviePlayerController类支持的video格式如下

  1. .mov .mpv .3gp .mp4

我不确定你这篇文章对你有多大帮助。 我是新来的。 我研究了本文中提供的分步说明

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

希望帮助它

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

  • 如果你想从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:^{}];