7

我正在使用以下代码使用 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];
4

7 回答 7

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

应该是这样的:

/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 添加为包含视图控制器的属性。

于 2012-06-08T18:13:09.673 回答
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响应内容。

于 2012-05-30T10:54:22.153 回答
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


    }
}
于 2012-09-03T09:34:59.460 回答
0

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

MPMoviePlayerController 类支持的视频格式如下

  1. .mov .mpv .3gp .mp4

我不确定这篇文章对你有多大帮助。我是新来的。我按照本文中提供的分步说明进行了工作

于 2014-02-19T07:43:17.147 回答
0

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

NSString *path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"mov"];
于 2012-05-30T10:24:45.073 回答
-1
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];

希望帮助它

于 2012-05-30T10:38:20.910 回答
-1

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

  • 如果你想 MPMoviePlayerViewController 从一个 web URL 播放,使用这个: 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:^{}];
于 2013-06-03T09:24:17.223 回答