cineio-ios 0.6.1

cineio-ios 0.6.1

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后一次发布2015年1月

Jeffrey WescottDaniel Klein维护。



 
依赖关系
AFNetworking~> 2.4.1
VideoCore~> 0.2.1.2
 

  • Jeffrey Wescott

cineio-broadcast-ios - cine.io 广播 iOS SDK

这是cine.io广播 iOS SDK。此库允许您从 iOS 设备实时直播视频到任何支持 RTMP 或 HLS 流媒体(iOS、Android、网络)的设备。

目录

安装

使用 SDK 最简单的方法是通过 CocoaPods。创建一个新的 XCode 项目,命名为 Podfile 的文件,其中至少包含以下内容

platform :ios, '7.0'

pod 'cineio-ios', '~> 0.6'

然后,通过运行 pod install 命令安装 Pod

pod install

然后您可以使用 <project>.xcworkspace 文件打开项目

open <project>.xcworkspace

示例应用

查看 cineio-ios-example-appcineio-ios-swift-example-app 仓库中的示例,它们演示了如何使用此 SDK。

基本用法

导入 SDK

#import <cineio/CineIO.h>

实例化客户端

CineClient *client = [[CineClient alloc] init];

设置客户端属性

CineClient 上的大多数 API 都需要设置 projectSecretKey 属性。此外,getProjectsWithCompletionHandler API 需要设置 masterKey 属性。请确保为要使用的 API 设置适当的属性。

client.masterKey = @"YOUR_ACCOUNT_MASTER_KEY";
client.projectSecretKey = @"YOUR_PROJECT_SECRET_KEY";

异步获取您的项目

[client getProjectsWithCompletionHandler:^(NSError *err, NSArray *projects) {
  for (id object in streams) {
    CineProject *project = (CineProject *)object;
    // do something
  }
}];

异步获取项目

[client getProjectWithCompletionHandler:^(NSError *error, CineProject *project) {
  // do something
}];

异步获取流

[client getStreamsWithCompletionHandler:^(NSError *err, NSArray *streams) {
  for (id object in streams) {
    CineStream *stream = (CineStream *)object;
    // do something
  }
}];

异步获取单个流

[client getStream:@"<SOME STREAM ID>" withCompletionHandler:^(NSError* error, CineStream* stream) {
  // do something
}];

异步创建新流

[client createStream:@{ @"name" : @"my stream" } withCompletionHandler:^(NSError* error, CineStream* stream) {
  // do something
}];

异步更新流

[client updateStream:@{ @"" : "<SOME STREAM ID>", @"name" : @"my stream" } withCompletionHandler:^(NSError* error, CineStream* stream) {
  // do something
}];

异步删除流

[client deleteStream:@"<SOME STREAM ID>" withCompletionHandler:^(NSError* error, NSHTTPURLResponse* response) {
  // do something, like check for response.statusCode == 200
}];

异步获取流的所有录制视频

[client getStreamRecordings:@"<SOME STREAM ID>" withCompletionHandler:^(NSError* error, NSArray* recordings) {
  // do something
}];

异步删除流的录制视频

[client deleteStreamRecording:@"<SOME STREAM ID>"
                     withName:@"foo.mp4"
         andCompletionHandler:^(NSError* error, NSHTTPURLResponse* response) {
  // do something, like check for response.statusCode == 200
}];

播放(使用CinePlayerView

为了使播放尽可能容易,cineio-ios包含了内置的视图控制器,可以处理大部分繁重的任务。使用它很简单。

Storyboard配置

如果你使用XCode的Storyboard来构建用户界面,我们的UI组件应该可以无缝工作。要使用它们,请按照以下步骤操作:

  1. 让你的视图控制器从CinePlayerViewController继承,而不是从UIViewController继承。
  2. 确保你的Storyboard中的视图控制器设置了正确的类名。它应该是你的CinePlayererViewController的子类的名称。

stream属性

继承自CinePlayerViewController的类将有一个可写的stream属性。你需要确保这个属性被设置为一个有效的CineStream对象。

- (void)viewDidLoad
{
    [super viewDidLoad];

    //-- cine.io setup

    // read our cine.io configuration from a plist bundle
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cineio-settings" ofType:@"plist"];
    NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:path];

    // create a new CineClient to fetch our stream information
    CineClient *cine = [[CineClient alloc] initWithSecretKey:settings[@"CINE_IO_SECRET_KEY"]];
    [cine getStream:settings[@"CINE_IO_STREAM_ID"] withCompletionHandler:^(NSError *error, CineStream *stream) {
        if (error) {
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network error"
                                                          message:@"Couldn't get stream settings from cine.io."
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
          [alert show];
        } else {
          self.stream = stream;
        }
    }];

开始播放

要开始播放,只需调用startStreaming。这个方法将首先尝试(异步)验证在给定的HLS URL上是否存在流,如果存在,则开始播放它。否则,将在UIAlert中显示相应的错误消息。

你可能想在方法中禁用空闲定时器。

- (IBAction)playButtonPressed:(id)sender
{
    playButton.enabled = NO;
    playButton.hidden = YES;
    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
    [self startStreaming];
}

清理

当流播放完成后(包括用户退出、流结束或遇到错误等原因),将调用finishStreaming。你应该将任何需要执行的清理代码(例如重新启用空闲定时器)放入此方法。

- (void)finishStreaming
{
    [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
    playButton.hidden = NO;
    playButton.enabled = YES;
}

发布(使用CineBroadcasterViewCineBroadcasterViewController

为了使发布尽可能简单,cineio-ios包含了一些可用的用户界面组件,可以处理大部分繁重的任务。使用它们很简单。

设置

处理设备旋转

为了用户体验的原因,我们的CineBroadcasterView既不使用“自动布局”,也不使用“弹簧和支柱”。相反,整个用户界面都是通过程序构建的。这意味着我们需要监听有关设备方向更改的通知。最佳位置是在AppDelegate didFinishLaunchingWithOptions方法中执行此操作。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    return YES;
}

故事板设置

如果你使用XCode的Storyboard来构建用户界面,我们的UI组件应该可以无缝工作。要使用它们,请按照以下步骤操作:

  1. 让视图控制器继承自CineBroadcasterViewController而不是UIViewController
  2. 确保您在故事板中的视图控制器具有正确的类名设置。它应该设置为您的CineBroadcasterViewController子类
  3. 确保您在故事板中的视图具有正确的类名设置。它应该设置为CineBroadcasterView

初始化属性

您需要初始化这些属性,通常在您的viewDidLoad方法中完成。例如

- (void)viewDidLoad
{
    //-- A/V setup
    self.videoSize = CGSizeMake(1280, 720);
    self.framesPerSecond = 30;
    self.videoBitRate = 1500000;
    self.sampleRateInHz = 44100; // either 44100 or 22050

    // must be called _after_ we set up our properties, as our superclass
    // will use them in its viewDidLoad method
    [super viewDidLoad];

    //-- cine.io setup

    // read our cine.io configuration from a plist bundle
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cineio-settings" ofType:@"plist"];
    NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:path];

    // create a new CineClient to fetch our stream information
    CineClient *cine = [[CineClient alloc] initWithSecretKey:settings[@"CINE_IO_SECRET_KEY"]];
    [self updateStatus:@"Configuring stream using cine.io ..."];
    [cine getStream:settings[@"CINE_IO_STREAM_ID"] withCompletionHandler:^(NSError *error, CineStream *stream) {
        if (error) {
            [self updateStatus:@"ERROR: couldn't get stream information from cine.io"];
        } else {
            self.publishUrl = [stream publishUrl];
            self.publishStreamName = [stream publishStreamName];

            // once we've fully-configured our properties, we can enable the
            // UI controls on our view
            [self enableControls];
        }
    }];
}

流媒体钩子

您可能在开始或结束时进行某些操作。您可以在toggleStreaming方法中这样做。例如,您可能会在这个方法中启用/禁用空闲计时器。

- (void)toggleStreaming:(id)sender
{
    switch(self.streamState) {
        case CineStreamStateNone:
        case CineStreamStatePreviewStarted:
        case CineStreamStateEnded:
        case CineStreamStateError:
            [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
            break;
        default:
            [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
            break;
    }

    // start / stop the actual stream
    [super toggleStreaming:sender];
}

鸣谢

cine.io iOS SDK的大部分基础来自优秀的VideoCore库。

贡献

  1. Fork它
  2. 创建您的功能分支(git checkout -b my-new-feature
  3. 提交您的更改(git commit -am '添加一些功能'
  4. 推送到分支(git push origin my-new-feature
  5. 创建新的Pull Request