在iOS应用程序中使用本地video文件(xcode)

我正在寻找在我的iOS应用程序中播放video文件的最佳方式。 我的应用程序目前正在开发中,将会有大约50个video(每个30秒长)和简短的教程。 如果可能的话,我希望他们都是本地的,所以用户可以在没有互联网连接的情况下观看video。 我无法find堆栈溢出的类似问题(也许我在错误的部分查找,请纠正我,如果我错了)。

所以我想到了两个不同的select:

  • 用户从商店下载应用程序,包括video
  • 用户在没有video的情况下下载应用程序,首次使用应用程序时必须首先下载video并将其保存在本地(永久)

如果有更好的select,我也想知道他们! 所以如果有人有这方面的经验,我会很感激一些帮助! 谢谢

根据用户的观点,人们更喜欢离线模式。 并希望应用程序的大小尽可能低,当他们从AppStore下载应用程序。 所以我的build议是build立一个video播放器,既有用户在线时播放文件的选项,并播放离线下载或caching文件..

一种方法是:

使用networking服务器

简单的java服务器例子:
https://github.com/mooncatventures-group/StreamX
检查: http : //www.onlinevideo.net/2011/05/streaming-vs-progressive-download-vs-adaptive-streaming/

它更好地build立一个应用程序,让应用程序从应用程序商店下载后,下载和存储应用程序内容的权力。 应用程序应该可以select删除应用程序下载的video或清除caching。

下来就是可以同时播放离线和在线video的video播放器的一个例子。

制作自定义电影播放器​​..

//CustomMoviePlayerViewController.h File #import <UIKit/UIKit.h> #import <MediaPlayer/MediaPlayer.h> @interface CustomMoviePlayerViewController : UIViewController { MPMoviePlayerController *mp; NSURL *movieURL; } - (id)initWithPath:(NSString *)moviePath; - (id)initWithURL:(NSString *)moviePath; - (void)readyPlayer; @end 

CustomMoviePlayerViewController.m文件

 #import "CustomMoviePlayerViewController.h" #pragma mark - #pragma mark Compiler Directives & Static Variables @implementation CustomMoviePlayerViewController /*--------------------------------------------------------------------------- * *--------------------------------------------------------------------------*/ - (id)initWithPath:(NSString *)moviePath { // Initialize and create movie URL if (self = [super init]) { movieURL = [NSURL fileURLWithPath:moviePath]; [movieURL retain]; } return self; } - (id)initWithURL:(NSString *)moviePath{ // Initialize and create movie URL if (self = [super init]) { movieURL = [NSURL URLWithString:moviePath]; [movieURL retain]; } return self; } /*--------------------------------------------------------------------------- * For 3.2 and 4.x devices * For 3.1.x devices see moviePreloadDidFinish: *--------------------------------------------------------------------------*/ - (void) moviePlayerLoadStateChanged:(NSNotification*)notification { // Unless state is unknown, start playback if ([mp loadState] != MPMovieLoadStateUnknown) { // Remove observer [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil]; // When tapping movie, status bar will appear, it shows up // in portrait mode by default. Set orientation to landscape [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; // Rotate the view for landscape playback [[self view] setBounds:CGRectMake(0, 0, 480, 320)]; [[self view] setCenter:CGPointMake(160, 240)]; [[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)]; // Set frame of movieplayer [[mp view] setFrame:CGRectMake(0, 0, 480, 320)]; // Add movie player as subview [[self view] addSubview:[mp view]]; // Play the movie [mp play]; } } /*--------------------------------------------------------------------------- * For 3.1.x devices * For 3.2 and 4.x see moviePlayerLoadStateChanged: *--------------------------------------------------------------------------*/ - (void) moviePreloadDidFinish:(NSNotification*)notification { // Remove observer [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerContentPreloadDidFinishNotification object:nil]; // Play the movie [mp play]; } /*--------------------------------------------------------------------------- * *--------------------------------------------------------------------------*/ - (void) moviePlayBackDidFinish:(NSNotification*)notification { [[UIApplication sharedApplication] setStatusBarHidden:YES]; // Remove observer [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; [self dismissModalViewControllerAnimated:YES]; } /*--------------------------------------------------------------------------- * *--------------------------------------------------------------------------*/ - (void) readyPlayer { mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; if ([mp respondsToSelector:@selector(loadState)]) { [mp setMovieSourceType:MPMovieSourceTypeFile]; // Set movie player layout [mp setControlStyle:MPMovieControlStyleFullscreen]; [mp setFullscreen:YES]; // May help to reduce latency [mp prepareToPlay]; // Register that the load state changed (movie is ready) [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil]; } else { // Register to receive a notification when the movie is in memory and ready to play. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerContentPreloadDidFinishNotification object:nil]; } // Register to receive a notification when the movie has finished playing. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; } /*--------------------------------------------------------------------------- * *--------------------------------------------------------------------------*/ - (void) loadView { [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; [[self view] setBackgroundColor:[UIColor blackColor]]; } /*--------------------------------------------------------------------------- * *--------------------------------------------------------------------------*/ - (void)dealloc { [mp release]; [movieURL release]; [super dealloc]; } @end 

当你点击TableListView单元格时,让你的播放器视图可见。

 //- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filePath = [NSString stringWithFormat:@"%@",[documentsDirectory stringByAppendingPathComponent:[item valueForKey:@"URL"]]]; bool b=[[NSFileManager defaultManager] fileExistsAtPath:filePath]; CustomMoviePlayerViewController *moviePlayer; if (b) { moviePlayer = [[[CustomMoviePlayerViewController alloc] initWithPath:filePath] autorelease]; [self presentModalViewController:moviePlayer animated:YES]; [moviePlayer readyPlayer]; }else{ NSDictionary *item = [tableData objectAtIndex:[indexPath row]]; NSString *strURL = [NSString stringWithFormat:[item valueForKey:@"URL"]]; moviePlayer = [[[CustomMoviePlayerViewController alloc] initWithURL:strURL] autorelease]; [self presentModalViewController:moviePlayer animated:YES]; [moviePlayer readyPlayer]; } 

制作一个URL下载器。 保存文件。

https://github.com/AFNetworking/AFNetworking

 -(void)downloadFile:(NSString *)UrlAddress { NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:UrlAddress]]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; NSString *fileName = UrlAddress; NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Successfully downloaded file to %@", path); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { NSLog(@"Download = %f", (float)totalBytesRead / totalBytesExpectedToRead); }]; [operation start]; } 

所以这将允许您保存文件,并播放和播放已经存在于您的应用程序中的预先加载的文件。