SDK使用方法

初始化

  • 在APPDelegate中初始化SDK。(必须配置)
/**
 使用sdk初始化的相应参数

 @param appid 开放平台注册获取应用Appid
 @param privateKey 开放平台注册获取应用privateKey
 @param isProServer 初始化SDK环境 isProServer为正式环境
*/
- (void)initSDKWithAppId:(nonnull NSString *)appid
              privateKey:(nonnull NSString *)privateKey
               serverPro:(BOOL)isProServer;

//调用示例
AndMuSDKManager * andmuManager = [AndMuSDKManager sharedInstance];
[andmuManager initSDKWithAppId:@"开放平台获取的appId" privateKey:@”开放平台获取的PrivateKey“ serverPro:YES];

在初始化SDK前必须配置下列方法(推荐appdelegate中)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //必须配置
    signal(SIGPIPE, SIG_IGN);
}
  • 设置开启调试模式(可不设置)
/**
 控制台日志输出控制(默认关闭)
 
 @param enable 开关参数  YES 默认为AMLogLevel_default  如需改变日志等级 - (void) setLogLevel:方法
 */
- (void)setDebug:(BOOL)enable;

/**
 @remark 其他详细信息打印的
 
 @param level debug类型
 AMLogLevel_default   = 0,
 AMLogLevel_tcpBuffer = 1 << 0,               //tcpbuffer相关的log
 AMLogLevel_logon     = 1 << 1,                //p2p相关log
 AMLogLevel_player    = 1 << 2,                //player相关log
 AMLogLevel_http      = 1 << 3,                  //http api log
 AMLogLevel_linklog   = 1 << 4,                //全链路日志 log
 AMLogLevel_all       = AMLogLevel_tcpBuffer | AMLogLevel_logon | AMLogLevel_player | AMLogLevel_http | AMLogLevel_linklog
 */
- (void)setLogLevel:(AMLogLevel)level;

  • 设置需要校验的Token(必须配置)

token通过自有平台从和目开放平台提供的api获取

/**
 配置App端token

 @param authToken 鉴权token
*/
- (void )setSDKAuthToken:(NSString *)authToken;

//调用示例
//注入token
[[AndMuSDKManager sharedInstance] setSDKAuthToken:token];

在token无效(调用SDK接口返回的错误码为11503或者11504)、绑定设备及解绑设备后需要重新获取token并调用上面方法。

  • 初始化设备模块(需要使用摄像机相关必须初始化)
/**
 初始化cameraManager

 @param completion 回调
*/
- (void)initAndMuCameraManagerWithCompletion:(AndMuHttpInterFaceCompletionHandler)completion;

设备绑定

通过以下方法可以启动一个专用于绑定的界面,具体的绑定方法可根据界面的提示操作

AndMuBindWebViewController *webController = [[AndMuBindWebViewController alloc]init];

注意:使用绑定前请在Appdelegate中注册UA

//配置UA
[[AndMuSDKManager sharedInstance] setWebViewNavigatorUserAgent];

设备解绑

/// 设备解绑
/// @param deviceId 设备Id  Y
/// @param completion 回调  NSDictiory Y
- (void) unBindDevice:(nonnull NSString *)deviceId
           completion:(AndMuHttpInterFaceCompletionHandler)completion;

获取设备列表

/// 获取设备列表
/// @param page 分页页数  N
/// @param pageSize 分页个数  N
/// @param completion 回调 Y   response<AMCameraInfo>
- (void) getDeviceList:(nullable NSNumber *)page
              pageSize:(nullable NSNumber *)pageSize
            Completion:(AndMuHttpInterFaceCompletionHandler)completion;

获取设备缩略图

/// 获取设备缩略图
/// @param deviceId 设备Id Y
/// @param completion 回调 Y NSString<thumbnailUrl>
- (void) getThumbnail:(nonnull NSString *)deviceId
           completion:(AndMuHttpInterFaceCompletionHandler)completion;

视频直播

播放相关详细逻辑请查看 DEMO

  • 开启会话
//建立连接
[[AndMuCameraManager sharedInstance] connect];
  • 直播

1、播放器配置及播放

AMPlayerConfig *playerConfig = [[AMPlayerConfig alloc]init];
playerConfig.playType = AMPlayerTypeTimeLine;
AMPlayer *player = [[AMPlayer alloc]initWithCameraInfo:self.cameraInfo config:playerConfig playerViewSize:CGSizeMake(kScreenWidth, kScreenWidth / 16 * 9)];
player.delegate = self;


//播放状态监听方法<AMPlayerDelegate>
///播放器状态
- (void)amPlayerStatusChanged:(AMPlayerStatus)playerStatus{
	NSLog(@"AMPlayerStatus = %lu",(unsigned long)playerStatus);
}

//播放
[self.player prepare];
[self.player start];

播放器状态监听回调说明

///播放器状态
typedef NS_ENUM(NSInteger, AMPlayerStatus) {
    AMPlayerStatusError = -1,        //错误
    AMPlayerStatusIdle = 0,          //初始状态
    AMPlayerStatusPreparing = 1,     //准备中
    AMPlayerStatusPrepared = 2,      //准备完成
    AMPlayerStatusBuffering = 3,     //缓冲中
    AMPlayerStatusBuffered = 4,      //缓冲完成
    AMPlayerStatusPlaying = 5,       //正在播放
    AMPlayerStatusPaused = 6,        //暂停
    AMPlayerStatusStoped = 7,        //停止
    AMPlayerStatusCompleted = 8,     //完成
};

2、暂停播放

[self.player pause];

3、恢复播放

[self.player resume];

4、语音通话

//开始通话
[self.player startAudio];
//语音通话监听
[player setOnAudioStateListener:^(AMAudioState state) {
    NSLog(@"当前语音状态 state = %lu",(unsigned long)state);
}];
//结束通话
[self.player stopAudio];

语音状态监听说明

///语音状态
typedef NS_ENUM(NSUInteger, AMAudioState) {
    AMAudioStateAvailable = 0,  //可进行通话
    AMAudioStateUnAvailable = 1 //不可进行通话
};

5、释放播放器

[self.player releasePlayer];

  • 断开会话

在完成对摄像头操作后需要断开会话

[[AndMuCameraManager sharedInstance] disConnect];

视频回放

播放相关详细逻辑请查看 DEMO

::: 直播和回放互相切换无需重新初始化播放器,直接进行 seek **,回放部分初始化与释放阶段与直播类似,这一节只概述回放相关的细节,详细示例参见

首先要通过下面接口获取回放片段用以绘制 :::

/// 获取回放timeLineSecion
/// @param deviceId 设备Id Y
/// @param startTime 过滤开始时间,13位时间戳  N
/// @param endTime 过滤结束时间 ,13位时间戳  N
/// @param shareId 分享id N
/// @param completion 回调 Y  response<NSArray<AMTimeLineSection>>
- (void) getTimelineSectionList:(nonnull NSString *)deviceId
                      startTime:(nullable NSNumber *)startTime
                        endTime:(nullable NSNumber *)endTime
                        shareId:(nullable NSString *)shareId
                     completion:(AndMuHttpInterFaceCompletionHandler)completion; 

在选择了某个时间点后调用如下方法进行播放:

//startTime NSdate 起始时间
//获取想要回放到的时间戳,注意需要有数据的回放片段才能回放
[self.player seek:[startTime timeIntervalSince1970]];
//回到直播
[self.player switchToLive];

视频截图

截图需要在视频播放起来后进行

UIImage * screenShotImg = [self.player screenShot];

告警视频

  • 获取告警视频列表
/// 获取告警事件列表
/// @param deviceId 设备id Y
/// @param startTime 过滤开始时间,13位时间戳  N
/// @param endTime 过滤结束时间 ,13位时间戳  N
/// @param completion 回调 Y response<NSArray<AMTimeLineEvent>>
- (void) getEventList:(nonnull NSString *)deviceId
            startTime:(nullable NSNumber *)startTime
              endTime:(nullable NSNumber *)endTime
           completion:(AndMuHttpInterFaceCompletionHandler)completion;

告警事件类型说明

typedef NS_ENUM(NSUInteger, AMEventType) {
    AMEventTypeMotion = 201,    //运动
    AMEventTypeSound = 202,    //声音
};

播放相关详细逻辑请查看 DEMO

告警播放始化与释放阶段与直播类似,播放代码如下:

AMPlayerConfig *playerConfig = [[AMPlayerConfig alloc]init];
playerConfig.playType = AMPlayerTypeNormal;
AMPlayer *player = [[AMPlayer alloc]initWithCameraInfo:_kCameraInfo config:playerConfig playerViewSize:CGSizeMake(kScreenWidth, kScreenWidth/ 16 * 9)];
player.delegate = self;
[player setEventInfo:_eventInfo];

[self.player prepare];
[self.player start];

设备设置

  • 获取设备配置信息
/// 获取设备配置信息
/// @param deviceId 设备Id  Y
/// @param completion 回调  response<AMTimeLineSection>
- (void) getSettings:(nonnull NSString *)deviceId completion:(AndMuHttpInterFaceCompletionHandler)completion;
  • 设置设备配置
/// 设置设备配置
/// @param deviceId 设备Id  Y
/// @param settings 设置配置项 Y @see AMSettings
/// @param completion 回调  NSDictiory Y
- (void) saveSetting:(nonnull NSString *)deviceId settings:(nonnull AMSettings *)settings completion:(AndMuHttpInterFaceCompletionHandler)completion;

具体配置项说明如下:

///设置项参数
typedef NS_ENUM(NSInteger, AMSettingType) {
    AMSettingTypeTitle,             //设备名称
    AMSettingTypeStatus,            //设备打开、关闭状态
    AMSettingTypeSoundDetection,    //声音告警开关
    AMSettingTypeMotionDetection,   //运动告警开关
    AMSettingTypeSoundSensitivity,  //声音告警灵敏度
    AMSettingTypeMotionSensitivity, //运动告警灵敏度
    AMSettingTypeCameraSound,       //设备声音采集
    AMSettingTypeStatusLight,       //设备状态指示灯
    AMSettingTypeNightVision        //夜视开关
};

云台控制

/// 设置PTZ接口
/// @param deviceId 设备ID
/// @param ptzType 设置PTZ类型,@see AMPTZType
/// @param direction
/// 如果 PTZType == AMPTZTypeTurnOnce || AMPTZTypeTurnContinuos 需要指定此参数,上下左右
/// 如果 PTZType == AMPTZTypeStopTurn || AMPTZTypeResetPosition 需要指定 AMPTZDirectReset
/// 如果 PTZType == AMPTZTypeSetPosition 需要指定 AMPTZDirectUnKnow
/// @param location
/// 如果(PTZType == AMPTZTypeSetPosition)时,需要指定此参数,其它传nil。参数类型如@{@"pan": @(0), @"tilt" :@(-10203)}
/// @param completion 回调  response<NSDictiory> 成功 000000 data@{}

- (void) setPTZPosition:(nonnull NSString *)deviceId ptzType:(AMPTZType)ptzType direction:(AMPTZDirect)direction location:(nullable NSDictionary *)location completion:(AndMuHttpInterFaceCompletionHandler)completion;

AMPTZType && AMPTZDirect类型列举:

///Ptz
///设置PTZ类型枚举
typedef NS_ENUM(NSInteger, AMPTZType) {
    AMPTZTypeUnKnow          = -1,
    AMPTZTypeTurnOnce        = 0,  //设置 一次PTZ,需要direction,上下左右
    AMPTZTypeTurnContinuos   = 1,  //设置 持续PTZ,需要direction,上下左右
    AMPTZTypeStopTurn        = 2,  //设置 停止持续PTZ direction = AMPTZDirectReset
    AMPTZTypeResetPosition   = 3,  //设置 重置PTZ direction = AMPTZDirectReset
    AMPTZTypeSetPosition     = 4   //设置 指定PTZ,需要location
};

///指定PTZ(摄像头摇头)的方向
typedef NS_ENUM(NSInteger, AMPTZDirect) {
    AMPTZDirectUnKnow = -1,
    AMPTZDirectReset  = 0,    //重置ptz\停止摇头
    AMPTZDirectLeft   = 1,    //AMPTZDirectLeft: 左
    AMPTZDirectRight  = 2,    //AMPTZDirectRight: 右
    AMPTZDirectUp     = 3,    //AMPTZDirectUp: 上
    AMPTZDirectDown   = 4,    //AMPTZDirectDown: 下
};

设备状态监听

  • 注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cameraConnectionDidChanged:) name:AMCameraConnectionDidMessageNotification object:nil];

监听通知类型
//MESSAGE_DEVICE_BIND 设备绑定消息
//MESSAGE_DEVICE_UNBIND 设备解绑消息
//MESSAGE_DEVICE_ONLINE 设备上线消息
//MESSAGE_DEVICE_OFFLINE 设备下线消息
if ([note.userInfo isKindOfClass:[NSDictionary class]]) {
    NSDictionary *noteDic = note.userInfo;
    if ([noteDic[@"msgType"] isEqualToString:@"MESSAGE_DEVICE_BIND"]) {
        [self showTipText:@"设备绑定消息"];
    }else if ([noteDic[@"msgType"] isEqualToString:@"MESSAGE_DEVICE_UNBIND"]) {
        [self showTipText:@"设备绑定消息"];
    }else if ([noteDic[@"msgType"] isEqualToString:@"MESSAGE_DEVICE_ONLINE"]) {
        [self showTipText:@"设备上线消息"];
    }else if ([noteDic[@"msgType"] isEqualToString:@"MESSAGE_DEVICE_OFFLINE"]) {
        [self showTipText:@"设备下线消息"];
    }
}

  • 移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:AMCameraConnectionDidMessageNotification object:nil];

释放AndMuSDK

//清楚AndMuSDK数据
[[AndMuSDKManager sharedInstance] unInit];