首先你需要在Xcode中创建一个APP开发项目
二、添加一个直播按钮
在这一步,你需要在APP的界面上添加一个直播按钮.
三、定义APP的参数
添加一个#import申明,指向GoCoderSDK的APIheader,然后在ViewController.m定义的ViewController类中添加goCoder参数。
#import
//ThetoplevelGoCoderAPIinterface
//Referencingoutletforthebroadcastbutton
四、注册和初始化SDK
在这一步,你需要在ViewController.m的ViewControllerClass的viewDidLoad方法中添加下面注册和初始化GoCoderSDK的代码。注意,请用你的LicenseKey替换GOSK-XXXX-XXXX-XXXX-XXXX-XXXX
//RegistertheGoCoderSDKlicensekey
NSError*goCoderLicensingError=[WowzaGoCoderregisterLicenseKey:@"GOSK-XXXX-XXXX-XXXX-XXXX-XXXX"];
if(goCoderLicensingError!=nil){
//Loglicensekeyregistrationfailure
NSLog(@"%@",[goCoderLicensingErrorlocalizedDescription]);
}else{
//InitializetheGoCoderSDK
self.goCoder=[WowzaGoCodersharedInstance];
}
五、检查APP的权限
字符串名字:Privacy-CameraUsageDescription,对应的值捕捉摄像头视频用于直播字符串名字:Privacy-MicrophoneUsageDescription,对应的值捕捉麦克风音频用于直播
六、开启摄像头预览
在ViewController.m文件定义的ViewControllerClass的viewDidLoad方法中加入下面的代码在界面中开启摄像头预览。
if(self.goCoder!=nil){
//AssociatetheU/IviewwiththeSDKcamerapreview
self.goCoder.cameraView=self.view;
//Startthecamerapreview
[self.goCoder.cameraPreviewstartPreview];
//Getacopyoftheactiveconfig
WowzaConfig*goCoderBroadcastConfig=self.goCoder.config;
//Setthedefaultsfor720pvideo
[goCoderBroadcastConfigloadPreset:WZFrameSizePreset1280x720];
//SettheconnectionpropertiesforthetargetWowzaStreamingEngineserverorWowzaCloudaccount
goCoderBroadcastConfig.hostAddress=@"live.someserver.net";
goCoderBroadcastConfig.portNumber=1935;
goCoderBroadcastConfig.applicationName=@"live";
goCoderBroadcastConfig.streamName=@"myStream";
//Updatetheactiveconfig
self.goCoder.config=goCoderBroadcastConfig;
八、添加流传输的状态回调
在这一步,我们添加对流传输状态监控的回调。1、我们首先要在ViewControllerClass的中包含WZStatusCallback协议,用来对直播流的传输做状态监控:
//ImplementstheWZStatusCallbackprotocol
2、在ViewControllerClass中添加WZStatusCallback协议中定义的方法:
-(void)onWZStatus:(WZStatus*)goCoderStatus{
//AsuccessfulstatustransitionhasbeenreportedbytheGoCoderSDK
NSString*statusMessage=nil;
switch(goCoderStatus.state){
caseWZStateIdle:
statusMessage=@"Thebroadcastisstopped";
break;
caseWZStateStarting:
statusMessage=@"Broadcastinitialization";
caseWZStateRunning:
statusMessage=@"Streamingisactive";
caseWZStateStopping:
statusMessage=@"Broadcastshuttingdown";
if(statusMessage!=nil)
NSLog(@"Broadcaststatus:%@",statusMessage);
-(void)onWZError:(WZStatus*)goCoderStatus{
//IfanerrorisreportedbytheGoCoderSDK,displayanalertdialog
//containingtheerrordetailsusingtheU/Ithread
dispatch_async(dispatch_get_main_queue(),^{
UIAlertView*alertDialog=
[[UIAlertViewalloc]initWithTitle:@"StreamingError"
message:goCoderStatus.description
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertDialogshow];
});
九、开始直播
在开始直播之前,您还需要为直播按钮加上启动和停止直播的代码:在ViewControllerClass中添加一个开启和停止直播的代码,当直播按钮被按下时需要调用这个方法。
-(IBAction)broadcastButtonTapped:(UIButton*)button
{
//Ensuretheminimumsetofconfigurationsettingshavebeenspecifiednecessaryto
//initiateabroadcaststreamingsession
NSError*configValidationError=[self.goCoder.configvalidateForBroadcast];
if(configValidationError!=nil){
[[UIAlertViewalloc]initWithTitle:@"IncompleteStreamingSettings"
message:self.goCoder.status.description
}elseif(self.goCoder.status.state!=WZStateRunning){
//Startstreaming
[self.goCoderstartStreaming:self];
else{
//Stopthebroadcastthatiscurrentlyrunning
[self.goCoderendStreaming:self];
接下来,我们需要添加下面的代码,将上述处理代码与直播按钮的按下动作关联起来:
forControlEvents:UIControlEventTouchUpInside];
十、构建和运行你的APP
点击Product按钮,选择Run。
十一、ViewController的例子程序
下面是一个完整的ViewControllerClass的例子,代码包括上面讲到的所有内容:
#import"ViewController.h"
-(void)viewDidLoad{
[superviewDidLoad];
//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.
-(void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
//Disposeofanyresourcesthatcanberecreated.
WowzaStreamingEngine4是业界功能强大、API接口丰富的流媒体Server产品,采用它作为流媒体服务器产品的案例很多,直播、在线教育、IPTV都有它的用武之地。