Giter Site home page Giter Site logo

polyv-ios-client-demo's Introduction

polyv-ios-client-demo

初始化设置

加入

MobileCoreServices.framework

SystemConfiguration.framework

libz.dylib

libsqlite3.0.dylib

到项目.

在AppDelegate.m里面

修改对应用户的配置信息:

privateKey,Readtoken,Writetoken,UserId

其中privateKey,Readtoken,Writetoken,UserId在polyv后台系统设置的api选项可以找到

@interface AppDelegate (){
    PolyvSettings* _polyvSettings;
}

...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    // Override point for customization after application launch.
    //设置离线缓存目录
    [PolyvSettings setDownloadDir:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/plvideo/a"]];

    _polyvSettings = [[PolyvSettings alloc] init];
    [_polyvSettings initVideoSettings:@"" Readtoken:@"" Writetoken:@"" UserId:@""];
    return YES;
}

...


//从后台切回需要重载设置
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     [_polyvSettings reloadSettings];
}

播放器

演示中使用了两种视频播放器,PLVMoviePlayerController以及SkinVideoViewController

SkinVideoViewController有自定义播放器皮肤,全屏旋转,以及切换码率等功能。

调用播放器播放视频

播放器PLVMoviePlayerController继承了iOS的MPMoviePlayerController,有MPMoviePlayerController所有属性和方法,可以直接当MPMoviePlayerController来使用。

播放Polyv的视频,只增加了如下接口:

/**传递vid并初始化一个播放器*/
-(id)initWithVid:(NSString*)vid;
/**播放器设置vid*/
- (void)setVid:(NSString*)vid;

/**传递vid和播放的码率,并初始化一个播放器*/
-(id)initWithVid:(NSString*)vid level:(int)level;

/**播放器设置vid和播放的码率*/
- (void)setVid:(NSString*)vid level:(int)level;

/**获取当前视频的有多少个码率*/
-(int)getLevel;

/**切换码率*/
-(void)switchLevel:(int)level;

//非加密视频专用
-(id)initWithLocalMp4:(NSString*)vid level:(int)level;

在线播放,下载,本地播放视频的相关演示在PolyvPlayerDemoViewController.m可以找到。

播放离线视频,跟播放在线视频的调用方式一致,但离线播放需要指定下载好的视频码率,不能用自适应播放。 码率参数level可指定为1,2,3分别代表"流畅","高清"和"超清"

#import "PLVMoviePlayerController.h"
...
//跟MPMoviePlayerController操作一样 

self.videoPlayer = [[PLVMoviePlayerController alloc]initWithVid:_vid];

//播放指定码率的视频
 self.videoPlayer = [[PLVMoviePlayerController alloc]initWithVid:vid level:1];

上传视频

上传视频的演示在PLVDemoViewController.m

该演示从相册找到视频,点击上传,支持断点续传。

- (void)uploadVideoFromAsset:(NSDictionary*)info
{
    NSURL *assetUrl = [info valueForKey:UIImagePickerControllerReferenceURL];
    NSString *fingerprint = [assetUrl absoluteString];

    [[self assetsLibrary] assetForURL:assetUrl
                          resultBlock:^(ALAsset* asset) {
                              self.imageView.image = [UIImage imageWithCGImage:[asset thumbnail]];
                              self.imageView.alpha = .5;
                              PLVAssetData* uploadData = [[PLVAssetData alloc] initWithAsset:asset];
                             
                              
                              PLVResumableUpload *upload = [[PLVResumableUpload alloc] initWithURL:[self endpoint] data:uploadData fingerprint:fingerprint];
                              NSString * surl = [assetUrl absoluteString];
                              NSString * ext = [surl substringFromIndex:[surl rangeOfString:@"ext="].location + 4];
                              NSMutableDictionary* extraInfo = [[NSMutableDictionary alloc]init];
                              [extraInfo setValue:ext forKey:@"ext"];
                              [extraInfo setValue:@"polyvsdk" forKey:@"title"];
                              [extraInfo setValue:@"polyvsdk upload demo video" forKey:@"desc"];
                              [upload setExtraInfo:extraInfo];
                              upload.progressBlock = [self progressBlock];
                              upload.resultBlock = [self resultBlock];
                              upload.failureBlock = [self failureBlock];
                              [upload start];
                          }
                         failureBlock:^(NSError* error) {
                             NSLog(@"Unable to load asset due to: %@", error);
                         }];
}

如果需要指定视频地址,参考如下代码例子:

//视频文件所在地址
NSURL *url = [[NSBundle mainBundle]URLForResource:@"video.mp4" withExtension:nil]
PLVData*uploadData = [[PLVData alloc] initWithData:[NSData dataWithContentsOfURL:url]];
PLVResumableUpload *upload = [[PLVResumableUpload alloc] initWithURL:@"http://v.polyv.net:1080/files/" data:uploadData fingerprint:[url absoluteString]];
NSString * surl = [assetUrl absoluteString];
NSString * ext = [surl substringFromIndex:[surl rangeOfString:@"ext="].location + 4];
NSMutableDictionary* extraInfo = [[NSMutableDictionary alloc]init];
[extraInfo setValue:ext forKey:@"ext"];
[extraInfo setValue:@"polyvsdk" forKey:@"title"];
[extraInfo setValue:@"polyvsdk upload demo video" forKey:@"desc"];
[upload setExtraInfo:extraInfo];

上传界面

alt tag

播放器,视频下载,本地加密演示

alt tag

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.