测试已测试 | ✓ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布最后一次发布 | 2015年1月 |
由Jeffrey Wescott,Daniel Klein维护。
依赖关系 | |
AFNetworking | ~> 2.4.1 |
VideoCore | ~> 0.2.1.2 |
这是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-app 和 cineio-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库。
git checkout -b my-new-feature
)git commit -am '添加一些功能'
)git push origin my-new-feature
)