如何播放本地视频文件? [英] how to play local video file?

查看:173
本文介绍了如何播放本地视频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的屏幕(桌面)捕获软件创建了.mov视频文件,我想在UIWebview中的应用程序中播放该视频。有没有办法播放该视频或任何其他方式,以便我可以为我的本地视频创建一个URL ???

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

现在我使用默认视频链接用于在UIWebview中播放视频。

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

以下是代码:

- (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..

请帮助!!

推荐答案

编辑: MPMoviePlayerController 现已弃用。所以我使用了 AVPlayerViewController 。并编写以下代码:

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>

您可以使用 MPMoviePlayerController 播放本地文件。

You can use MPMoviePlayerController to play local file.

1。添加 Mediaplayer 框架并执行#在viewController中导入< MediaPlayer / MediaPlayer.h>

2。拖放视频文件您在桌面上创建了xcode。

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

3. 获取本地视频的路径。

3. Get the path of the local video.

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

4. 用您的路径初始化moviePlayer。

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. 要控制播放后要执行的操作:

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.

编辑2 :处理 AVPlayerViewController的完成而不是 MPMoviePlayerController ,请使用以下内容...

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

在这个例子中,我解雇 AVPlayerViewController 完成后:

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

这篇关于如何播放本地视频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆