如何使用MPMoviePlayerController播放视频?

问题描述:

我正在使用以下代码使用MPMoviePlayerController播放视频,但未播放视频。谁能告诉我为什么?如何使用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]; 
+0

提供的分步说明书上工作你有没有指定文件的扩展名? –

这很奇怪,但它似乎工作正常,如果你让你的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]; 

当我在模拟器中运行你的代码,路径是这样的:

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

这应该只是这样的:

/用户/ mlong /库/ 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作为包含视图控制器的属性。

+0

似乎是你有一个明确的描述。我早已解决了这个问题。问题在于设置框架并添加了preparetoPlay和全屏作为YES。 – Perseus

+0

@Perseus你究竟如何解决这个问题? –

+0

Hi @Ancientar。你可以在上面的评论中看到,我已经提到过,我忘了为MPMoviePlayerController设置框架,并且还添加了一个属性prepareToPlay。它的工作 – Perseus

试着问你的包而不是直接建立文件路径手动

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

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

希望帮助它

好,还有就是应用程序包之间有很大的差异,文档目录。我建议你看看那个。

首先,视频存储在哪里?

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

刚刚与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响应。

+0

我也试过用FilePath,屏幕变黑,但视频没有播放。 – Perseus

+0

仅供参考,我不打算将文件添加到我的项目中,我只想从Documents目录中获取它 – Perseus

+0

任何非常简单的示例,即如何使用MPMoviePlayerController播放视频都足够了。我会非常感谢你,如果你帮我解决这个问题 – Perseus

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 


    } 
} 

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

  • 如果你想MPMoviePlayerViewController从Web URL播放使用: NSURL * URL = [NSURL URLWithString:@ “https://www.test.com/anyMovie.mp4”];

  • 如果你想MPMoviePlayerViewController从应用程序捆绑播放使用: 的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:^{}]; 

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

支持的视频格式通过的MPMoviePlayerController类跟随

  1. .MOV .mpv名为.3gp .MP4

,我不知道有多少,你这篇文章会帮助你。我是新来的。我在this article