在iOS应用中使用的本地视频文件(的Xcode)(Using local video files i

2019-10-21 02:00发布

我在寻找我的iOS应用播放视频文件的最好方法。 我的应用程序是目前正在开发,将有大约50视频(每30秒)短教程。 如果可能的话我希望他们所有的地方,这样用户就可以观看视频的时候,他们没有互联网连接。 我找不到在堆栈溢出类似的问题(也许我找错节,请纠正我,如果我错了)。

所以我在想2个不同的选项:

  • 用户下载从商店应用,包括视频
  • 用户下载应用,但不视频,并具有先下载视频使用首次应用程序时,并保存在本地(永远)

如果有更好的选择,我也想知道他们! 因此,如果任何人有这样的经历,我真的很感激一些帮助! 谢谢

Answer 1:

如按次人的用户点喜欢离线模式。 并希望当他们从下载AppStore的应用程序大小要尽可能低。 所以我的建议是建立既具有选项流文件,而用户在线播放和下载离线或缓存文件,视频播放器..

一种方法是:

使用Web服务器

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

它能够更好地建立一个程序给用户下载的权力和商店应用内容后,该应用程序是从应用商店下载。 应用程序应该有选项,删除应用程序下载的视频或清除缓存。

下来是视频播放器,可以播放离线和在线视频的一个例子。

创建自定义电影播放器​​..


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

让您的播放器查看可见当u点击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];
}

因此,这将允许你保存文件并播放+播放预加载的文件,这已经是你的应用程序中存在。



文章来源: Using local video files in iOS app (xcode)