如何播放本地视频文件?

NSE*_*ion 14 iphone video objective-c uiwebview ios

我用我的屏幕(桌面)捕获软件创建了.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;
}
Run Code Online (Sandbox Code Playgroud)

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

请帮助!!

iNo*_*oob 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];
Run Code Online (Sandbox Code Playgroud)

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

#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

NSString*thePath=[[NSBundle mainBundle] pathForResource:@"yourVideo" ofType:@"MOV"];
NSURL*theurl=[NSURL fileURLWithPath:thePath];
Run Code Online (Sandbox Code Playgroud)

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];
Run Code Online (Sandbox Code Playgroud)

5.控制播放后要执行的操作:

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

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

AVPlayerItem *playerItem = player.currentItem;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
Run Code Online (Sandbox Code Playgroud)

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

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

    [playerViewController dismissViewControllerAnimated:false completion:nil];
}
Run Code Online (Sandbox Code Playgroud)


Man*_*esh 5

只需使用以下代码替换您的URL即可

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

NSURL *fileURL    =   [NSURL fileURLWithPath:filepath];  
Run Code Online (Sandbox Code Playgroud)