14

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

Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
NSException
  • 1,268
  • 3
  • 14
  • 28

3 Answers3

65

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];
}
Niall Kiddle
  • 1,477
  • 1
  • 16
  • 35
iNoob
  • 3,364
  • 2
  • 26
  • 33
  • Thanks for your help, I wanna ask one thing that is this code is capable to play mp4 files if I will change the file Type??? – NSException Mar 21 '12 at 12:02
  • 1
    @IphoneDevloper, Ofcourse, if its above ios 4.2 it shouldn't be a problem, my mp4 file didn't play for a ios 4.2 device so had to change it to mov, but it did fine for ios 5 device. So just check if formats compatible with that ios then you'll be fine. – iNoob Mar 21 '12 at 12:08
  • When I add subview, in subview I can see status also. It is like two status bars. – Durgaprasad Mar 18 '14 at 11:05
  • Using "MOV" as the file extension gives me a nil path. I had to use "mov" to get this to work. – user3344977 May 08 '15 at 22:18
  • show some gap of milliseconds when video starts playing again. how can i solve this. please help me. – Yogendra Patel May 15 '20 at 05:48
5

Just Replace your URL with below code

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

NSURL *fileURL    =   [NSURL fileURLWithPath:filepath];  
nsgulliver
  • 12,655
  • 23
  • 43
  • 64
Mangesh
  • 2,257
  • 4
  • 24
  • 51
0

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()
        }
    }
Shrikant K
  • 1,988
  • 2
  • 23
  • 34