How to play video using MPMoviePlayerController?

2019-06-17 18: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 ?

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条回答
霸刀☆藐视天下
2楼-- · 2019-06-17 19:01

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:

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.

查看更多
姐就是有狂的资本
3楼-- · 2019-06-17 19:01

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

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

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

MPMoviePlayerViewController *moviePlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
moviePlayerView.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentViewController:moviePlayerView animated:YES completion:^{}];
查看更多
淡お忘
4楼-- · 2019-06-17 19:08

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

NSString *path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"mov"];
查看更多
祖国的老花朵
5楼-- · 2019-06-17 19:13
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


    }
}
查看更多
甜甜的少女心
6楼-- · 2019-06-17 19:15

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.

Supported video formats by MPMoviePlayerController class are following

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

查看更多
Lonely孤独者°
7楼-- · 2019-06-17 19:24
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];

hope Help it

查看更多
登录 后发表回答