测试已测试 | ✓ |
语言语言 | Obj-CObjective C |
许可 | MIT |
发布最后发布 | 2015年5月 |
由 Jeffrey Wescott,Daniel Klein,Norm Brandinger 共同维护。Jeffrey Wescott,Daniel Klein,Norm Brandinger.
依赖 | |
AFNetworking | ~> 2.5 |
VideoCore | ~> 0.3 |
这是cine.io的Broadcast 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-app和cineio-broadcast-ios-swift-example-app仓库中的工作示例,这些示例使用了此 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 内置了一个视图控制器,可以处理大部分繁重的工作。使用它非常直接。
如果您正在使用 XCode 的 Storyboard 构建用户界面,我们的 UI 组件应该无缝工作。要使用它们,请按照以下步骤操作
CinePlayerViewController
的类,而不是从 UIViewController
。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;
}
CineBroadcasterView
和 CineBroadcasterViewController
)为了使发布尽可能简单,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 组件应该无缝工作。要使用它们,请按照以下步骤操作
CineBroadcasterViewController
而不是 UIViewController
。CineBroadcasterViewController
的子类。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 库。