快速入门

优质
小牛编辑
128浏览
2023-12-01

目录

  1. 环境需求

  2. 添加SDK

  3. 初始化

  4. 加入房间

  5. 离开房间

  6. Demo下载

环境需求

  • iOS 9.0以上真机
  • 支持架构 armv7s、arm64、arm64e

添加SDK

  • 安装CocoaPods,在终端中输入如下命令:

    brew install cocoapods
    
  • 在项目根目录下运行中端命令:

    pod init
    
  • profile文件中加入如下代码:

    platform :ios, '9.0'
    
    target 'Your App' do
      pod 'MRTC_Interactive'
    end
    
  • 在项目根目录下运行中端命令:

    pod install
    
  • Build Settings中将Build Options下的Enable Bitcode设为NO

  • Capabilities中将Background Modes打开就可以在后台保持推拉流状态。

初始化

使用目睹云的互动直播主机地址、推流配置和渲染的view来创建MRTCInteractiveClient实例

#import <MRTC_Interactive/MRTC_Interactive.h>
...

- (void)initClient {
    MRTCSetting *defaultSetting = [[MRTCSetting alloc]init];
    self.mrtcClient = [[MRTCInteractiveClient alloc]initWithHost:@"interactive.myun.tv" setting:defaultSetting renderer:self.rendererView];
}

加入频道

在初始化MRTCInteractiveClient实例之后,调用joinWithChannel: role: password: delegate:方法加入频道

   //channel:频道id
   //role:角色 主播为main,嘉宾为1起始的数字字符
   //password:密码 主播和嘉宾的密码是不同的
   //delegate:代理
   - (void)joinRoom {
      [self.mrtcClient joinWithChannel:@"channelId" role:@"main" password:@"xxxxxxxx" delegate:self];
   }

   //加入房间的代理方法MRTCInteractiveClientRoomDelegate
   - (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client joinSuccess:(NSArray<NSString *> *)roleArray{
      //加入成功
      //roleArray为房间中当前在线的其他角色,可以调用playRoleWithRole:来进行播放
   }

  - (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client joinFailed:(MRTCError *)error{
     //加入失败
  }

  - (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client onJoinRoles:(NSArray<NSString *> *)roleArray{
     //新加入房间的角色数组,可以调用playRoleWithRole:来进行播放
  }

  - (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client onLeaveRoles:(NSArray<NSString *> *)roleArray{
     //离开房间的角色数组
  }

  - (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client playRoleSuccess:(NSString *)role{
     //播放角色成功
  }

  - (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client playRole:(NSString *)role failure:(MRTCError *)error{
     //播放角色失败
  }

  - (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client stateChanged:(MRTCInteractiveClientState)state{
     //连接状态改变回调
  }

  - (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client onError:(MRTCError *)error{
     //其他错误回调
  }

离开频道

调用leave方法离开频道,调用该方法会把直播相关的资源释放

- (void)leaveChanel {
   [self.mrtcClient leave];
}

iOS demo 下载