Question

I'm trying to play a video with MPMoviePlayerViewController, I present the MPMoviePlayerViewController but after 1 second he is dismissing itself.

This is my code:

.h:

#import <MediaPlayer/MediaPlayer.h>

@property (strong, nonatomic) MPMoviePlayerViewController *moviePlayerViewController;

.m:

- (void)playmovie
{
    NSString *databaseName = @"NO.mp4";
    NSArray *documentPaths =
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentDir = [documentPaths objectAtIndex:0];
    NSString *databasePath = [documentDir stringByAppendingPathComponent:databaseName];

    NSURL *movieURL = [NSURL fileURLWithPath:databasePath];

    UIGraphicsBeginImageContext(CGSizeMake(1,1));
    self.moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self presentMoviePlayerViewControllerAnimated:self.moviePlayerViewController];
    UIGraphicsEndImageContext();

    [self.moviePlayerViewController.moviePlayer prepareToPlay];
    [self.moviePlayerViewController.moviePlayer play];
}

Please tell me what I did wrong.

Thanks in advance!

Was it helpful?

Solution

check below answer

https://stackoverflow.com/a/15586468/1713478

change url to below code

NSString *path =[[NSBundle mainBundle] pathForResource:@"NO" ofType:@"mp4"]; 
NSURL *url = [NSURL fileURLWithPath:path];

try this you will be succeed.

OTHER TIPS

Try this..

NSString *databaseName = @"NO.mp4";
    NSArray *documentPaths =
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentDir = [documentPaths objectAtIndex:0];
    NSString *databasePath = [documentDir stringByAppendingPathComponent:databaseName];
    NSURL *url = [NSURL fileURLWithPath:databasePath];
    MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    moviePlayer.view.frame = CGRectMake(0, 0, 500, 500);
    moviePlayer.moviePlayer.shouldAutoplay=YES;
    moviePlayer.moviePlayer.controlStyle = MPMediaTypeMusicVideo;
    [moviePlayer.moviePlayer setFullscreen:YES animated:YES];
    [self.view addSubview:moviePlayer.view];
    [moviePlayer.moviePlayer play];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top