Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...