这篇文章介绍了如何用Wowza Gocoder SDK iOS版开发一个基本功能的直播APP。
一、创建一个新的开发项目
首先你需要在Xcode中创建一个APP开发项目
二、添加一个直播按钮
在这一步,你需要在APP的界面上添加一个直播按钮.
三、定义APP的参数
添加一个 #import 申明,指向GoCoder SDK的API header,然后在 ViewController.m 定义的 ViewController 类中添加goCoder参数。
#import
@interface ViewController ()
// The top level GoCoder API interface
@property (nonatomic, strong) WowzaGoCoder*goCoder;
// Referencing outlet for the broadcastbutton
@property (weak, nonatomic) IBOutletUIButton *broadcastButton;
@end
四、注册和初始化SDK
在这一步,你需要在 ViewController.m 的 ViewController Class的 viewDidLoad 方法中添加下面注册和初始化GoCoder SDK的代码。
注意,请用你的License Key替换 GOSK-XXXX-XXXX-XXXX-XXXX-XXXX
// Register the GoCoder SDK license key
NSError *goCoderLicensingError =[WowzaGoCoder registerLicenseKey:@"GOSK-XXXX-XXXX-XXXX-XXXX-XXXX"];
if (goCoderLicensingError != nil) {
// Log license key registration failure
NSLog(@"%@", [goCoderLicensingError localizedDescription]);
} else {
// Initialize the GoCoder SDK
self.goCoder = [WowzaGoCoder sharedInstance];
}
五、检查APP的权限
对于iOS 10及以上版本,你必须定义APP访问摄像头和麦克风时给用户的授权提示信息。
字符串名字:Privacy - Camera Usage Description,对应的值 捕捉摄像头视频用于直播
字符串名字:Privacy - Microphone Usage Description,对应的值 捕捉麦克风音频用于直播
六、开启摄像头预览
在 ViewController.m 文件定义的 ViewController Class的 viewDidLoad 方法中加入下面的代码在界面中开启摄像头预览。
if (self.goCoder != nil) {
// Associate the U/I view with the SDK camera preview
self.goCoder.cameraView = self.view;
// Start the camera preview
[self.goCoder.cameraPreview startPreview];
}
七、配置直播相关参数
在 ViewController.m 文件定义的 ViewController Class的 viewDidLoad 方法中加入对直播的相关参数,包括服务器地址(hostAddress)、端口(portNumber)、 应用名(applicationName)和流名(streamName)。这个服务器不限于Wowza Streaming Engine或Wowza Streaming Cloud。
如果你要往Wowza Streaming Cloud 云平台推流,那么你可以在Wowza Streaming Cloud的Web界面的live streaming详情的 Overview Tab页上找到 Connection Code
// Get a copy of the active config
WowzaConfig *goCoderBroadcastConfig =self.goCoder.config;
// Set the defaults for 720p video
[goCoderBroadcastConfigloadPreset:WZFrameSizePreset1280x720];
// Set the connection properties for thetarget Wowza Streaming Engine server or Wowza Cloud account
goCoderBroadcastConfig.hostAddress =@"live.someserver.net";
goCoderBroadcastConfig.portNumber =1935;
goCoderBroadcastConfig.applicationName =@"live";
goCoderBroadcastConfig.streamName =@"myStream";
// Update the active config
self.goCoder.config =goCoderBroadcastConfig;
八、添加流传输的状态回调
在这一步,我们添加对流传输状态监控的回调。
1、我们首先要在 ViewController Class的中包含WZStatusCallback协议, 用来对直播流的传输做状态监控:
// Implements the WZStatusCallbackprotocol
@interface ViewController ()
2、在 ViewController Class中添加WZStatusCallback协议中定义的方法:
- (void) onWZStatus:(WZStatus *)goCoderStatus {
// A successful status transition has been reported by the GoCoder SDK
NSString *statusMessage = nil;
switch (goCoderStatus.state) {
case WZStateIdle:
statusMessage = @"Thebroadcast is stopped";
break;
case WZStateStarting:
statusMessage = @"Broadcastinitialization";
break;
case WZStateRunning:
statusMessage = @"Streaming isactive";
break;
case WZStateStopping:
statusMessage = @"Broadcastshutting down";
break;
}
if (statusMessage != nil)
NSLog(@"Broadcast status: %@", statusMessage);
}
- (void) onWZError:(WZStatus *)goCoderStatus {
// If an error is reported by the GoCoder SDK, display an alert dialog
// containing the error details using the U/I thread
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertDialog =
[[UIAlertView alloc]initWithTitle:@"Streaming Error"
message:goCoderStatus.description
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertDialog show];
});
}
九、开始直播
在开始直播之前,您还需要为直播按钮加上启动和停止直播的代码:
在 ViewController Class中添加一个开启和停止直播的代码,当直播按钮被按下时需要调用这个方法。
- (IBAction)broadcastButtonTapped:(UIButton *)button
{
// Ensure the minimum set of configuration settings have been specifiednecessary to
// initiate a broadcast streaming session
NSError *configValidationError = [self.goCoder.configvalidateForBroadcast];
if (configValidationError != nil) {
UIAlertView *alertDialog =
[[UIAlertView alloc]initWithTitle:@"Incomplete Streaming Settings"
message:self.goCoder.status.description
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertDialog show];
} else if (self.goCoder.status.state != WZStateRunning) {
// Start streaming
[self.goCoder startStreaming:self];
}
else {
// Stop the broadcast that is currently running
[self.goCoder endStreaming:self];
}
}
接下来,我们需要添加下面的代码,将上述处理代码与直播按钮的按下动作关联起来:
[self.broadcastButton addTarget:selfaction:@selector(broadcastButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
十、构建和运行你的APP
点击 Product 按钮,选择 Run 。
十一、ViewController的例子程序
下面是一个完整的 ViewController Class的例子,代码包括上面讲到的所有内容:
#import "ViewController.h"
#import
@interface ViewController ()
// The top level GoCoder API interface
@property (nonatomic, strong)WowzaGoCoder *goCoder;
// Referencing outlet for the broadcastbutton
@property (weak, nonatomic) IBOutletUIButton *broadcastButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Register the GoCoder SDK license key
NSError *goCoderLicensingError = [WowzaGoCoderregisterLicenseKey:@"GOSK-XXXX-XXXX-XXXX-XXXX-XXXX"];
if (goCoderLicensingError != nil) {
// Log license key registration failure
NSLog(@"%@", [goCoderLicensingError localizedDescription]);
} else {
// Initialize the GoCoder SDK
self.goCoder = [WowzaGoCoder sharedInstance];
}
if (self.goCoder != nil) {
// Associate the U/I view with the SDK camera preview
self.goCoder.cameraView = self.view;
// Start the camera preview
[self.goCoder.cameraPreview startPreview];
}
// Get a copy of the active config
WowzaConfig *goCoderBroadcastConfig = self.goCoder.config;
// Set the defaults for 720p video
[goCoderBroadcastConfig loadPreset:WZFrameSizePreset1280x720];
// Set the connection properties for the target Wowza Streaming Engineserver or Wowza Cloud account
goCoderBroadcastConfig.hostAddress = @"live.someserver.net";
goCoderBroadcastConfig.portNumber = 1935;
goCoderBroadcastConfig.applicationName = @"live";
goCoderBroadcastConfig.streamName = @"myStream";
// Update the active config
self.goCoder.config = goCoderBroadcastConfig;
[self.broadcastButton addTarget:selfaction:@selector(broadcastButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) onWZStatus:(WZStatus *)goCoderStatus {
// A successful status transition has been reported by the GoCoder SDK
NSString *statusMessage = nil;
switch (goCoderStatus.state) {
case WZStateIdle:
statusMessage = @"The broadcast is stopped";
break;
case WZStateStarting:
statusMessage = @"Broadcast initialization";
break;
case WZStateRunning:
statusMessage = @"Streaming is active";
break;
case WZStateStopping:
statusMessage = @"Broadcast shutting down";
break;
}
if (statusMessage != nil)
NSLog(@"Broadcast status: %@", statusMessage);
}
- (void) onWZError:(WZStatus *)goCoderStatus {
// If an error is reported by the GoCoder SDK, display an alert dialog
// containing the error details using the U/I thread
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertDialog =
[[UIAlertView alloc] initWithTitle:@"Streaming Error"
message:goCoderStatus.description
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertDialog show];
});
}
-(IBAction)broadcastButtonTapped:(UIButton *)button
{
// Ensure the minimum set of configuration settings have been specifiednecessary to
// initiate a broadcast streaming session
NSError *configValidationError = [self.goCoder.configvalidateForBroadcast];
if (configValidationError != nil) {
UIAlertView *alertDialog =
[[UIAlertView alloc] initWithTitle:@"Incomplete StreamingSettings"
message:self.goCoder.status.description
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertDialog show];
} else if (self.goCoder.status.state != WZStateRunning) {
// Start streaming
[self.goCoder startStreaming:self];
}
else {
// Stop the broadcast that is currently running
[self.goCoder endStreaming:self];
}
}
@end
Wowza Streaming Engine 4是业界功能强大、API接口丰富的流媒体Server产品,采用它作为流媒体服务器产品的案例很多,直播、在线教育、IPTV都有它的用武之地。
京ICP备09015132号-996 | 网络文化经营许可证京网文[2017]4225-497号 | 违法和不良信息举报电话:4006561155
© Copyright 2000-2023 北京哲想软件有限公司版权所有 | 地址:北京市海淀区西三环北路50号豪柏大厦C2座11层1105室