如何播放本地video文件?

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

现在,我使用默认的video链接在UIWebview中播放video。

这里是代码:

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

但我没有我想播放的videourl

请帮助!

编辑: MPMoviePlayerController现在MPMoviePlayerController 。 所以我用AVPlayerViewController 。 并写下如下代码:

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

请不要忘记导入以下框架:

 #import <AVFoundation/AVFoundation.h> #import <AVKit/AVKit.h> 

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

1.添加Mediaplayer框架,并在viewController中执行#import <MediaPlayer/MediaPlayer.h>

2.将您在桌面上创build的video文件拖放到xcode中。

3.获取本地video的path。

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

4.用你的path初始化moviePlayer。

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

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

只需用下面的代码replace您的url

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

以上解决scheme解释了如何使用NSBundle播放Xcode中存在的video。 我的答案将有助于那些正在寻找从设备dynamicselectvideo和播放。

 class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate import AVFoundation import AVKit import MobileCoreServices 

(不要忘记在Build阶段添加MobileCoreServicesFramework)

设置video的属性。 例如

 @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) } 

然后实现UIImagePickerControllerDelegate函数

 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) } 

并使用上面的文件名,你可以播放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() } }