2

I'm new here. I'm already 2 days browsing for solutions, but unhappy.

Here is my code:

NSString *docPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *docaPathFull = [docPath stringByAppendingPathComponent:@"/IMG_0003.m4v"];
self.videoURL = [NSURL fileURLWithPath:docaPathFull];

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
UIImage  *thumbnail = [player thumbnailImageAtTime:4.0 timeOption:MPMovieTimeOptionExact];
player = nil;       
self.imageView.image = thumbnail;

Problem is, i can't make video file visible for MPMoviePlayerController. I can't get thumbnail and I can't play it, just black player and loading infinity.

I did try:

NSString*thePath=[[NSBundle mainBundle] pathForResource:@"IMG_0003" ofType:@"m4v"];
self.videoURLtheurl=[NSURL fileURLWithPath:thePath];

and this

self.videoURL = [[NSBundle mainBundle] URLForResource:@"IMG_0005" withExtension:@"mov"];

and this

NSString *filePath = @"/Users/[user]/Documents/video/IMG_0004.mov";
self.videoURL = [NSURL fileURLWithPath:filePath];

If you have any ideas how to save my life, please do!!!

Many thanks for you time.

nzs
  • 3,252
  • 17
  • 22
Robertas Uldukis
  • 801
  • 1
  • 11
  • 20
  • What is the exact path of the video? is it in the documents directory or in the app bundle? *confused* – Ushan87 May 02 '13 at 14:11

3 Answers3

1

Actually this is my working example from my project. Make sure you added your m4v file to your project. The notification was a need for me, maybe it is not necessary for you.

theMovie = [[MPMoviePlayerController alloc] initWithContentURL:
    [NSURL fileURLWithPath: [[NSBundle mainBundle] 
    pathForResource:@"yourmovie" ofType:@"m4v"]]];

theMovie.view.frame = CGRectMake(0, 0, 320, 480);
[theMovie setControlStyle:MPMovieControlStyleNone];
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(moviePlayBackDidFinish:) 
                                             name:MPMoviePlayerPlaybackDidFinishNotification 
                                           object:theMovie];
[self.view addSubview:theMovie.view];    
[theMovie play];
nzs
  • 3,252
  • 17
  • 22
  • Thanks. It is a difference where in the project file is locating? – Robertas Uldukis May 02 '13 at 15:01
  • Here is a error generated with your code: 2013-05-02 16:15:13.592 iphone[15317:18e03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' – Robertas Uldukis May 02 '13 at 15:15
  • it is only a guess but if your NSBundle mainBundle returns nil it is maybe because you created a Command Line Tool type of project... see this: http://stackoverflow.com/questions/3949368/nsbundle-pathforresource-is-null – nzs May 02 '13 at 21:06
  • Or simply the file is not part of your project. You can double check this by accessing your project's Build Phases / Copy Bundle Resources. You should be locate your movie file there. Right click on it and check Show in Finder and/or Reveal in Project Navigator. It should appear at both places.. hope it helps you! – nzs May 02 '13 at 21:11
0

You can use following method to get a thumbnail image for a given Video Url,

    -(UIImage *) giveThumbofVideoUrl:(NSURL *) url{
            NSString *urlString = [url absoluteString];
            AVAsset *asset = [AVAsset assetWithURL:url];
            AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
            imageGenerator.appliesPreferredTrackTransform = YES;
            //imageGenerator.maximumSize = CGSizeMake(320,480);
            CMTime time = CMTimeMake(1, 1);
            CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
            UIImage *image = [UIImage imageWithCGImage:imageRef];
            CGImageRelease(imageRef);
    return image;
   }

How you can use it ?,

NSString*thePath=[[NSBundle mainBundle] pathForResource:@"IMG_0003" ofType:@"m4v"];
NSURL *movieUrl=[NSURL fileURLWithPath:thePath];
UIImage  *thumbnail = [self giveThumbofVideoUrl:movieUrl];
Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45
  • Thanks for that fast response, but i need to play this file to. So MPMoviePlayerController it will be perfect solution. I can play files from http, but localy, no. – Robertas Uldukis May 02 '13 at 14:11
  • Make sure you define the moviePlayer variable globally, @property(nonatomic, strong) MPMoviePlayerController *player; also you should add the moviePlayer.view as a subview to the self.view – Thilina Chamath Hewagama May 02 '13 at 14:14
  • from http url player is working fine, but is not working, when i change url from http to local. – Robertas Uldukis May 02 '13 at 15:02
0

MPMoviePlayerController is Deprecated Now. So I have used AVPlayerViewController. and written the following code:

NSURL *videoURL = [NSURL fileURLWithPath:filePath];
    //filePath may be from the Bundle or from the Saved file Directory, it is just the path for the video
    AVPlayer *player = [AVPlayer playerWithURL:videoURL];
    AVPlayerViewController *playerViewController = [AVPlayerViewController new];
    playerViewController.player = player;
    //[playerViewController.player play];//Used to Play On start
    [self presentViewController:playerViewController animated:YES completion:nil];

Please do not forget to import following frameworks:

#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

Hope this Helps..

Manab Kumar Mal
  • 20,788
  • 5
  • 31
  • 43