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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9802403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:26:37  来源:igfitidea点击:

how to play local video file?

iphoneobjective-ciosvideouiwebview

提问by NSException

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

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

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

现在我使用默认视频链接在 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 !!

请帮忙!!

回答by iNoob

EDIT:MPMoviePlayerControlleris Deprecated Now. So I have used AVPlayerViewController. and written the following code:

编辑: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];

Please do not forget to import following frameworks:

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

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

You can use MPMoviePlayerControllerto play local file.

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

1.Add Mediaplayerframework and do #import <MediaPlayer/MediaPlayer.h>in your viewController.

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

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

2.将您在桌面上创建的视频文件拖放到 xcode 中。

3.Get the path of the local video.

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

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

4.Initialize the moviePlayer with your path.

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.To control what is to be done after playback:

5.控制播放后要做什么:

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

EDIT 2: To handle completion for AVPlayerViewControllerrather than MPMoviePlayerController, use the following...

编辑2:要处理完成AVPlayerViewController而不是MPMoviePlayerController,使用以下...

AVPlayerItem *playerItem = player.currentItem;

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

In this example, I dismiss the AVPlayerViewControllerafter completion:

在这个例子中,我AVPlayerViewController在完成后关闭:

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

    [playerViewController dismissViewControllerAnimated:false completion:nil];
}

回答by Mangesh

Just Replace your URL with below code

只需用以下代码替换您的网址

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

NSURL *fileURL    =   [NSURL fileURLWithPath:filepath];  

回答by Shrikant K

The above solutions explaining how to play video which is present in Xcode using NSBundle. My answer will be helpful for those who is looking for dynamically select video from device and play.

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

class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate

import AVFoundation
import AVKit
import MobileCoreServices 

(Don't forget to add MobileCoreServicesFramework in Build Phases)

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

Set the properties for video. for e.g.

设置视频的属性。例如

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

Then implement UIImagePickerControllerDelegate function

然后实现 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)
    }

and using above filename you can play the 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()
        }
    }