Iphone 如何播放本地视频文件?

Iphone 如何播放本地视频文件?,iphone,objective-c,ios,video,uiwebview,Iphone,Objective C,Ios,Video,Uiwebview,我已经用我的屏幕(桌面)捕获软件创建了.mov视频文件,我希望该视频在我的UIWebview应用程序中播放。是否有任何方式播放该视频或任何其他方式,以便我可以为我的本地视频创建URL 现在我使用默认的视频链接在UIWebview中播放视频 代码如下: - (void)applicationDidBecomeActive:(UIApplication *)application { self.viewVideoDisplay.frame = CGRectMake(0, 0, 1024,

我已经用我的屏幕(桌面)捕获软件创建了.mov视频文件,我希望该视频在我的UIWebview应用程序中播放。是否有任何方式播放该视频或任何其他方式,以便我可以为我的本地视频创建URL

现在我使用默认的视频链接在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;
}
但是我没有我想播放的视频的url


请帮忙

编辑:
MPMoviePlayerController
现在不推荐使用。所以我使用了
avplayervewcontroller
。并编写了以下代码:

    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>
4。使用您的路径初始化电影播放器

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.
编辑2:要处理
avplayervcontroller
而不是
mpmovieplayervcontroller
的完成,请使用以下命令

AVPlayerItem *playerItem = player.currentItem;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
在本例中,我在完成后关闭了
avplayervewcontroller

-(void)playBackFinished:(NSNotification *) notification {
    // Will be called when AVPlayer finishes playing playerItem

    [playerViewController dismissViewControllerAnimated:false completion:nil];
}

只需将您的URL替换为以下代码

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

NSURL *fileURL    =   [NSURL fileURLWithPath:filepath];  

上述解决方案解释了如何使用NSBundle播放Xcode中的视频。我的答案将有助于那些谁是动态选择视频设备和播放寻找

class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate

import AVFoundation
import AVKit
import MobileCoreServices 
(不要忘记在构建阶段添加MobileCoreServicesFramework)

设置视频的属性。例如

@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)
    }
使用上述文件名,您可以播放视频:)


谢谢你的帮助,我想问一件事,如果我改变文件类型,这段代码可以播放mp4文件吗?@iphonedevlooper,当然,如果它高于ios 4.2,这应该不会是一个问题,我的mp4文件不能为ios 4.2设备播放,所以不得不将其改为mov,但对于ios 5设备它做得很好。所以只要检查格式是否与ios兼容就可以了。当我添加子视图时,在子视图中我也可以看到状态。它就像两个状态栏。使用“MOV”作为文件扩展名会给我一个nil路径。我不得不用“mov”来让它工作。当视频再次开始播放时,显示一些毫秒间隔。我怎样才能解决这个问题。请帮帮我。
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)
    }
    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()
        }
    }