1

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!

ytpm
  • 4,962
  • 6
  • 56
  • 113
  • check below answer http://stackoverflow.com/a/15586468/1713478 change url to your file path. try this you will be succeed. – Pratik Mar 28 '13 at 04:09
  • What's with the graphics context there? – jjv360 Mar 28 '13 at 12:21
  • @jjv360 because i've got this error: `: CGContextSaveGState: invalid context 0x0 : CGContextClipToRect: invalid context 0x0 : CGContextTranslateCTM: invalid context 0x0 : CGContextDrawShading: invalid context 0x0 : CGContextRestoreGState: invalid context 0x0` – ytpm Mar 28 '13 at 22:57
  • Can you give the code in `presentMoviePlayerViewControllerAnimated`? – jjv360 Mar 29 '13 at 06:06
  • @jjv360 What do you mean? When i'm writing only `self.moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL]; [self presentMoviePlayerViewControllerAnimated:self.moviePlayerViewController];` those 2 lines, I have this context error. Don't know why. – ytpm Mar 29 '13 at 09:02
  • @Pratik I've tried what you've told me to, but still it's not working, but now, it just keeps loading. – ytpm Mar 29 '13 at 09:39
  • which file format of video are you using? – Pratik Mar 29 '13 at 09:46
  • mp4. Now i'm using this code: `NSURL *urlString = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"NO" ofType:@"mp4" inDirectory:@""]]; NSLog(@"\n\nthe string %@",urlString);` and it prints me `null` I don't know why. The files are inside 'Copy Bundle Resources' but still returns null again. – ytpm Mar 29 '13 at 09:49
  • 1
    try below NSString *path =[[NSBundle mainBundle] pathForResource:@"NO" ofType:@"mp4"]; NSURL *url = [NSURL fileURLWithPath:path]; – Pratik Mar 29 '13 at 09:54
  • It's working man, thanks a lot! really! put it as an answer and I will accept it. – ytpm Mar 29 '13 at 09:56
  • you are always welcome and i wiil put in answer – Pratik Mar 29 '13 at 10:01

2 Answers2

3

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.

Community
  • 1
  • 1
Pratik
  • 2,399
  • 17
  • 36
0

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];
dhaya
  • 1,522
  • 13
  • 21