跳到主要内容

音视频播放

更新时间:2025-09-24 11:38:13

专属版设备播放接口与通用版基本相同,请先阅读【通用版-音视频播放】相关接口,只是在构建RTCXPlayInfo播放对象时,需要设置RTCXPlayInfo.deviceType = 1,即私有化设备

// 导入媒体播放SDK头文件
#import <RTCXMedia/RTCXMedia.h>

播放器初始化

@interface RTCXPlayInfo : NSObject
// 设备型号 如:xLeWcoelZbyOePbJludl 必选
@property (nonatomic, copy) NSString *productKey;
// 设备序列号SN 如:TEST20240820002 必选
@property (nonatomic, copy) NSString *deviceName;
// 平台为设备颁发的ID,设备的唯一标识符 必选
@property (nonatomic, copy) NSString *iotId;
// 是否需要播放预连接 YES: 预连接, NO: 不预连接; 默认NO 可选
@property (nonatomic, assign) BOOL preConnectEnable;
// 设备镜头ID 可选
@property (nonatomic, strong) NSNumber *lensId;
// 码流类型,0:主码流 1:辅码流,默认为0
@property (nonatomic, assign) int streamType;
// 设备属性值字典 必选
@property (nonatomic, strong) NSDictionary *propertyMap;
// 设备套餐信息 必选
@property (nonatomic, strong) NSDictionary *cloudServicesDic;
// 设备类型 0:普通设备;1:私有化设备;2:AP设备;默认0,可选
@property (nonatomic, assign) NSInteger deviceType;
@end

@interface RTCXPlayerView : UIView
/**
player相关接口调用
*/
@property (nonatomic, strong) RTCXPlayerController *playerController;

/**
初始化View
@param frame 需要展示视频的frame
@param playInfo 播放设备信息
*/
- (id)initWithFrame:(CGRect)frame withPlayInfo:(RTCXPlayInfo *)playInfo;
@end

调用示例如下:

RTCXPlayInfo *playInfo = [[RTCXPlayInfo alloc] init];
playInfo.iotId = self.curDevice.iotId;
playInfo.productKey = self.curDevice.productKey;
playInfo.deviceName = self.curDevice.deviceName;
playInfo.lensId = self.lensCount == 2?@(self.lensType):nil;
playInfo.propertyMap = self.curDevice.propertyMap;
playInfo.cloudServicesDic = [self.curDevice.cloudServices iotyy_modelToJSONObject];
playInfo.deviceType = 1;// 区分私有化设备
// 初始化播放器,传入frame和设备model
self.playerView = [[RTCXPlayerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * 9.0 / 16.0) withDevice:playInfo];
self.playerView.playerController.delegate = self;// 创建player控制类并设置代理监听player状态,playeview.playerController会懒加载创建控制类
self.playerView.playerController.dataSource = self;// 设置playeview.playerController的dataSource
// 初始化player播放的视图,可以把该playerView加到任意view上
[self.view addSubview:self.playerView];

设备在线状态

// 导入物模型SDK头文件
#import <RTCXThingCapability/RTCXThingCapability.h>

通过物对象模块(可参见【通用版-物模型功能】相关接口)可获取设备在线状态,包括监听设备上下线通知等,只是在创建物对象时,需要调用- (RTCXThing*)buildThing:(NSString *)productKey deviceName:(NSString *)deviceName iotId:(NSString *)iotId deviceType:(NSInteger)deviceType接口,设置deviceType = 1,即私有化设备

  • 物对象初始化
@interface RTCXThingManager : NSObject
/**
返回单例

@return 单例
*/
+ (instancetype)sharedManager;
/**
销毁 Thing 对象,在 Thing 不再需要时调用此方法
可以将其从 管理者维护的物的队列中删除
@param thing Thing 的对象
*/
- (void)destroyThing:(RTCXThing *)thing;
/**
创建 Thing 对象,该对象会被加入管理者维护的物的队列中
私有化的设备,在创建与获取物对象时必须使用该方法,建议使用该方法代替 - (RTCXThing*)buildThing:deviceName:iotId: 方法

@param productKey 物在iot平台注册的产品型号
@param deviceName 物的deviceName
@param iotId 物的iotId
@param deviceType 设备类型 0:普通设备;1:私有化设备;2:AP设备;默认0
@return 返回 Thing 对象
*/
- (RTCXThing*)buildThing:(NSString *)productKey
deviceName:(NSString *)deviceName
iotId:(NSString *)iotId
deviceType:(NSInteger)deviceType;
@end

使用 RTCXThingManager 宏定义调用示例如下:

// 初始化物模型
RTCXThing *things = [kRTCXThingManager buildThing:self.curDevice.productKey deviceName:self.curDevice.deviceName iotId:self.curDevice.iotId deviceType:1];
self.things = things;
// 注册监听
[self.things registerThingObserver:self];
  • 获取设备在线状态
/**
获取物的生命周期
说明:status表示设备生命周期,目前有以下几个状态,0:未激活;1:上线;2:休眠;3:离线;8:禁用;
@param handler 结果回调函数
*/
- (void)getStatus:(RTCXThingActionsResponseHandler)handler;

调用示例如下:

[[[kRTCXThingManager buildThing:self.curDevice.productKey deviceName:self.curDevice.deviceName iotId:self.curDevice.iotId deviceType:1] getThingActions] getStatus:^(RTCXThingActionsResponse * _Nullable response) {

}];
  • 状态回调代理方法如下:
/**
物的状态变更的观察者类
*/
@protocol RTCXThingObserver <NSObject>

@optional
/**
物的状态变更回调, 0:离线;1:在线;2:休眠;3:休眠离线;
@param iotId 物的 iotId
@param params 描述具体状态
*/
- (void)onStatusChange:(NSString *)iotId params:(NSDictionary *)params;
@end
  • 监听获取上下线消息
#pragma mark -- RTCXThingObserver --
- (void)onStatusChange:(NSString *)iotId params:(NSDictionary *)params {
COMMON_PRINTF(@"[deviceList]:deviceStatusChanged-iotId: %@, params: %@",iotId,params);
int status = [params[@"status"] intValue];
switch (status) {
case 0:
// 离线
break;
case 1:
// 在线
break;
case 2:
// 休眠
break;
case 3:
// 休眠离线
break;
default:
break;
}
[self loadNewData];
}

其它功能调用同【通用版-音视频播放】即可