如何使用的MPMoviePlayerController播放视频?(How to play vide

2019-09-17 15:17发布

我使用下面的代码打使用的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];

Answer 1:

这是很奇怪的,但它似乎工作没关系,如果你让你的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 /库/应用程序支持/ iPhone模拟器/ 5.1 /应用/ 8CFB9B94-BD6A-442C-A525-573FE343506D / VidoePlayer.app /用户/ mlong /库/应用程序支持/ iPhone模拟器/ 5.1 /应用/ 8CFB9B94-BD6A -442C-A525-573FE343506D /文档/ one.mp4

它应该只是这样的:

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



Answer 2:

好吧,有应用程序包和文件目录之间有很大的区别。 我建议你看一看这一点。

首先,当存储在视频?

如果您的视频是在文档目录,不追加到束路径的文件目录路径。

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



Answer 3:

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


    }
}


Answer 4:

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

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


Answer 5:

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

支持的视频格式通过的MPMoviePlayerController类是以下

  1. .MOV .mpv .3gp使用.MP4

我不知道有多少,你这篇文章会帮助你。 我是新来的。 我的工作是一步一步的说明中提供此文章



Answer 6:

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

希望帮助它



Answer 7:

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

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


文章来源: How to play video using MPMoviePlayerController?