Search code examples
iphoneobjective-ciosvideouiwebview

how to play local video file?


I have created .mov video file with my screen(desktop) capture software and I want that video to play in my application in UIWebview. Is there any way to play that video or any other way so that I can create a URL for my local video ???

Right now I mm using a default video link for playing video in UIWebview.

Here is the code :

- (void)applicationDidBecomeActive:(UIApplication *)application 
{

    self.viewVideoDisplay.frame = CGRectMake(0, 0, 1024, 1024);
    [self.window addSubview:self.viewVideoDisplay];
    [self.window bringSubviewToFront:self.viewVideoDisplay];
    NSString *urlAddress = @"https://response.questback.com/pricewaterhousecoopersas/zit1rutygm/";
    //Create a URL object.
    NSURL *url = [NSURL URLWithString:urlAddress];            
    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];            
    //Load the request in the UIWebView.
    [self.webViewVideo loadRequest:requestObj];

    IsLoadingSelf = YES;
}

but I dont have url of video which I want to play..

pls help !!


Solution

  • EDIT: 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>
    

    You can use MPMoviePlayerController to play local file.

    1. Add Mediaplayer framework and do #import <MediaPlayer/MediaPlayer.h> in your viewController.

    2. Drag and drop your video file you created on desktop into the xcode.

    3. Get the path of the local video.

    NSString*thePath=[[NSBundle mainBundle] pathForResource:@"yourVideo" ofType:@"MOV"];
    NSURL*theurl=[NSURL fileURLWithPath:thePath];
    

    4. Initialize the moviePlayer with your path.

    self.moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:theurl];
    [self.moviePlayer.view setFrame:CGRectMake(40, 197, 240, 160)];
    [self.moviePlayer prepareToPlay];
    [self.moviePlayer setShouldAutoplay:NO]; // And other options you can look through the documentation.
    [self.view addSubview:self.moviePlayer.view];
    

    5. To control what is to be done after playback:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; 
    //playBackFinished will be your own method.
    

    EDIT 2: To handle completion for AVPlayerViewController rather than MPMoviePlayerController, use the following...

    AVPlayerItem *playerItem = player.currentItem;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
    

    In this example, I dismiss the AVPlayerViewController after completion:

    -(void)playBackFinished:(NSNotification *) notification {
        // Will be called when AVPlayer finishes playing playerItem
    
        [playerViewController dismissViewControllerAnimated:false completion:nil];
    }