NSException
NSException

Reputation: 1278

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 !!

Upvotes: 15

Views: 37900

Answers (3)

iNoob
iNoob

Reputation: 3364

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

Upvotes: 65

Shrikant K
Shrikant K

Reputation: 2018

The above solutions explaining how to play video which is present in Xcode using NSBundle. My answer will be helpful for those who is looking for dynamically select video from device and play.

class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate

import AVFoundation
import AVKit
import MobileCoreServices 

(Don't forget to add MobileCoreServicesFramework in Build Phases)

Set the properties for video. for e.g.

@IBAction func buttonClick(sender: AnyObject)
{
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.mediaTypes = [kUTTypeMovie as String]
    imagePicker.allowsEditing = true
    self.presentViewController(imagePicker, animated: true,
        completion: nil)
}

Then implement UIImagePickerControllerDelegate function

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
        var filename = ""
        let mediaType = info[UIImagePickerControllerMediaType] as! NSString
        if mediaType.isEqualToString(kUTTypeMovie as String)
        {
            let url = info[UIImagePickerControllerMediaURL] as! NSURL
            filename = url.pathComponents!.last!
        }
        self.dismissViewControllerAnimated(true, completion: nil)
        self.playVideo(filename)
    }

and using above filename you can play the video :)

    func playVideo(fileName : String)
    {
        let filePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(fileName)
        let player = AVPlayer(URL: filePath)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self.presentViewController(playerViewController, animated: true)
        {
            player.play()
        }
    }

Upvotes: 0

Mangesh
Mangesh

Reputation: 2285

Just Replace your URL with below code

NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"videoFileName" ofType:@"m4v"];  

NSURL *fileURL    =   [NSURL fileURLWithPath:filepath];  

Upvotes: 5

Related Questions