如何使用MPMoviePlayerController播放视频?

时间:2022-05-01 21:29:55

I am using the following code to play the video using the MPMoviePlayerController , but the video is not played. Can anyone tell me why ?

我使用以下代码使用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 个解决方案

#1


20  

It's pretty weird, but it seems to work okay if you make your MPMoviePlayerController a property instead of a local variable. Seems something is going on behind the scenes. I'm thinking it's related to ARC. Are you using ARC?

这很奇怪,但是如果你让你的MPMoviePlayerController成为一个属性而不是一个局部变量似乎没问题。似乎在幕后发生了一些事情。我认为这与ARC有关。你在用ARC吗?

It's also an issue that you've over-appended your path:

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

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

When I run your code in the simulator, the path looks like this:

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

/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 / VidoePlayer.app / Users / mlong / Library / Application Support / iPhone Simulator / 5.1 / Applications / 8CFB9B94-BD6A -442C-A525-573FE343506D /文档/ one.mp4

It should just be this:

它应该是这样的:

/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

So just delete this line:

所以只需删除此行:

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

And then change your player instantiation to this:

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

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

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

_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

[_moviePlayer play];

So you should add the MPMoviePlayerController as a property of your containing view controller.

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

#2


3  

All right, there is a big difference between the app bundle and the documents directory. I suggest you take a look at that.

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

First of all, Where is the video stored?

首先,视频存储在哪里?

If your video is in the documents directory, don't append the documents directory path to the bundle path.

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

Just try with the filePath variable:

只需尝试使用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]];

However, if the file is in the app bundle (you added it to your project in XCode), you should use what is in jinx response.

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

#3


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


    }
}

#4


0  

Try asking your bundle directly and not setting up the file path manually

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

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

#5


0  

Player is initialized with the URL of the video which we want to be played (It can be either a path of local file of a device or a live URL.) after that player is added as a sub view of current view.

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

Supported video formats by MPMoviePlayerController class are following

MPMoviePlayerController类支持的视频格式如下

  1. .mov .mpv .3gp .mp4
  2. .mov .mpv .3gp .mp4

I am not sure how much you this article would help you out. I am new to this. I worked on the step by step instructions provided in this article

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

#6


-1  

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

hope Help it

希望帮助它

#7


-1  

It took me few mins to debug the problem but the answer is quite simple. Here is the deal:

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

  • If you want to MPMoviePlayerViewController to play from a web URL use this: NSURL *url = [NSURL URLWithString:@"https://www.test.com/anyMovie.mp4"];

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

  • And if you want MPMoviePlayerViewController to play it from App Bundle use this: NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"anyMovie" ofType:@"m4v"]; NSURL *url = [NSURL fileURLWithPath:moviePath];

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

The Rest of it goes the same, except you have to set this property "movieSourceType" as follows:

除了你必须设置这个属性“movieSourceType”,其余部分是相同的,如下所示:

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

#1


20  

It's pretty weird, but it seems to work okay if you make your MPMoviePlayerController a property instead of a local variable. Seems something is going on behind the scenes. I'm thinking it's related to ARC. Are you using ARC?

这很奇怪,但是如果你让你的MPMoviePlayerController成为一个属性而不是一个局部变量似乎没问题。似乎在幕后发生了一些事情。我认为这与ARC有关。你在用ARC吗?

It's also an issue that you've over-appended your path:

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

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

When I run your code in the simulator, the path looks like this:

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

/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 / VidoePlayer.app / Users / mlong / Library / Application Support / iPhone Simulator / 5.1 / Applications / 8CFB9B94-BD6A -442C-A525-573FE343506D /文档/ one.mp4

It should just be this:

它应该是这样的:

/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

So just delete this line:

所以只需删除此行:

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

And then change your player instantiation to this:

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

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

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

_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

[_moviePlayer play];

So you should add the MPMoviePlayerController as a property of your containing view controller.

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

#2


3  

All right, there is a big difference between the app bundle and the documents directory. I suggest you take a look at that.

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

First of all, Where is the video stored?

首先,视频存储在哪里?

If your video is in the documents directory, don't append the documents directory path to the bundle path.

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

Just try with the filePath variable:

只需尝试使用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]];

However, if the file is in the app bundle (you added it to your project in XCode), you should use what is in jinx response.

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

#3


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


    }
}

#4


0  

Try asking your bundle directly and not setting up the file path manually

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

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

#5


0  

Player is initialized with the URL of the video which we want to be played (It can be either a path of local file of a device or a live URL.) after that player is added as a sub view of current view.

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

Supported video formats by MPMoviePlayerController class are following

MPMoviePlayerController类支持的视频格式如下

  1. .mov .mpv .3gp .mp4
  2. .mov .mpv .3gp .mp4

I am not sure how much you this article would help you out. I am new to this. I worked on the step by step instructions provided in this article

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

#6


-1  

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

hope Help it

希望帮助它

#7


-1  

It took me few mins to debug the problem but the answer is quite simple. Here is the deal:

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

  • If you want to MPMoviePlayerViewController to play from a web URL use this: NSURL *url = [NSURL URLWithString:@"https://www.test.com/anyMovie.mp4"];

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

  • And if you want MPMoviePlayerViewController to play it from App Bundle use this: NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"anyMovie" ofType:@"m4v"]; NSURL *url = [NSURL fileURLWithPath:moviePath];

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

The Rest of it goes the same, except you have to set this property "movieSourceType" as follows:

除了你必须设置这个属性“movieSourceType”,其余部分是相同的,如下所示:

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