如何播放本地视频文件?

14
我使用屏幕捕捉软件创建了 .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;
}

但是我没有想要播放的视频的网址..

请帮忙!!

3个回答

65

编辑: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.将您在桌面上创建的视频文件拖放到Xcode中。

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

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

4. 使用您的路径初始化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.

编辑2: 为了处理AVPlayerViewController的完成,而不是MPMoviePlayerController,请使用以下代码...

AVPlayerItem *playerItem = player.currentItem;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

在这个例子中,我会在视频播放完成后关闭AVPlayerViewController

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

    [playerViewController dismissViewControllerAnimated:false completion:nil];
}

谢谢您的帮助,我想问一件事,如果我更改文件类型,这段代码能否播放mp4文件? - NSException
1
@IphoneDevloper,当然,如果它在iOS 4.2以上版本中运行良好,那就不应该有问题。我的MP4文件无法在iOS 4.2设备上播放,所以我不得不将其更改为MOV格式,但对于iOS 5设备来说效果很好。因此,只需检查格式是否与该iOS兼容,那么你就没问题了。 - iNoob
当我添加子视图时,在子视图中我也可以看到状态。就像两个状态栏一样。 - Durgaprasad
使用“MOV”作为文件扩展名会给我一个空路径。我不得不使用“mov”才能使它工作。 - user3344977
当视频重新开始播放时,显示一些毫秒的间隔。我该如何解决这个问题?请帮帮我。 - Yogendra Patel

5

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

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

NSURL *fileURL    =   [NSURL fileURLWithPath:filepath];  

0

以上的解决方案说明了如何使用NSBundle在Xcode中播放视频。我的答案将对那些正在寻找从设备动态选择视频并播放的人有所帮助。

class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate

import AVFoundation
import AVKit
import MobileCoreServices 

(在 Build Phases 中别忘了添加 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)
    }

使用上述文件名,您可以播放视频:)

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接