cinel-broadcast-ios 0.7.2

cinel-broadcast-ios 0.7.2

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最后发布2015年5月

由 Jeffrey Wescott,Daniel Klein,Norm Brandinger 共同维护。Jeffrey WescottDaniel KleinNorm Brandinger.



 
依赖
AFNetworking~> 2.5
VideoCore~> 0.3
 

  • Jeffrey Wescott

cineio-broadcast-ios - cine.io Broadcast iOS SDK

这是cine.ioBroadcast iOS SDK。这个库允许您从 iOS 设备实时直播视频到支持 RTMP 或 HLS 流媒体传输(iOS、Android、web)的任何其他设备。

目录

安装

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

platform :ios, '7.0'

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

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

pod install

然后您可以通过打开 <project>.xcworkspace 文件来打开项目

open <project>.xcworkspace

示例应用

查看cineio-broadcast-ios-example-appcineio-broadcast-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;
}

Storyboard 设置

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

  1. 使你的视图控制器继承自 CineBroadcasterViewController 而不是 UIViewController
  2. 确保你在Storyboard中的视图控制器有正确的类名设置。它应该设置为 你的 CineBroadcasterViewController 的子类。
  3. 确保你在Storyboard中的视图有正确的类名设置。它应该设置为 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. 分叉它
  2. 创建你的特性分支(《git checkout -b my-new-feature》)
  3. 提交你的更改(《git commit -am '添加一些功能'》)
  4. 推送到分支(《git push origin my-new-feature》)
  5. 创建新的 Pull Request