live.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * @description WebSDK 实例化对象
  3. */
  4. let rtc;
  5. /**
  6. * @method initSDK
  7. * @description 初始化SDK方法
  8. * @param { object } data 初始化SDK所需参数
  9. * @returns { object }
  10. */
  11. export function initSDK(data) {
  12. // eslint-disable-next-line no-undef
  13. rtc = new Rtc(data);
  14. return rtc;
  15. }
  16. /**
  17. * @method initListener
  18. * @description 初始化监听事件
  19. */
  20. export function initListener() {
  21. rtc.on('login_success', data => {
  22. console.log('登录成功', data);
  23. alert('登录成功:' + JSON.stringify(data));
  24. });
  25. rtc.on('login_failed', data => {
  26. console.log('登录失败', data);
  27. alert('登录失败:' + JSON.stringify(data));
  28. });
  29. rtc.on('conference_join', function () {
  30. // 有监听就是加入房间成功
  31. console.log('加入房间成功');
  32. });
  33. rtc.on('conference_join_failed', function (err) {
  34. // 加入房间失败 err为错误原因
  35. console.log('加入房间失败', err);
  36. });
  37. rtc.on('switch_user_settings', function (settingData) {
  38. // 单个用户配置监听
  39. console.log(settingData);
  40. });
  41. rtc.on('allow_sub', stream => {
  42. alert('监听到有流');
  43. rtc.trySubscribeStream({
  44. tryStream: stream,
  45. success: function success(stream) {
  46. stream.show('other');
  47. },
  48. fail: function fail(err) {
  49. console.error(err);
  50. }
  51. });
  52. });
  53. }
  54. /**
  55. * @method startLive
  56. * @description 开启直播
  57. */
  58. export function startLive() {
  59. rtc.startLive({
  60. success(data) {
  61. alert('开启直播成功:' + JSON.stringify(data));
  62. },
  63. fail(data) {
  64. alert('开启直播失败:' + JSON.stringify(data));
  65. }
  66. });
  67. }
  68. /**
  69. * @method endLive
  70. * @description 结束直播
  71. */
  72. export function endLive() {
  73. rtc.endLive({
  74. success(data) {
  75. alert('结束直播成功:' + JSON.stringify(data));
  76. },
  77. fail(data) {
  78. alert('结束直播失败:' + JSON.stringify(data));
  79. }
  80. });
  81. }
  82. /**
  83. * @method createLocalStram
  84. * @description 创建本地流
  85. */
  86. export function createLocalStream() {
  87. const createData = { video: true, audio: true };
  88. rtc.createLocalStream({
  89. streamName: 'main',
  90. createData,
  91. success: function (stream) {
  92. // 创建本地流成功,将流展示到id为 live 的dom元素盒子中
  93. stream.show('live');
  94. // publish(stream); // 如果需要立即推流,执行publish方法
  95. alert('创建本地流成功');
  96. },
  97. fail: function (data) {
  98. // 创建本地流失败,应用层处理
  99. console.log(data);
  100. alert('创建本地流失败');
  101. }
  102. });
  103. }
  104. /**
  105. * @method publishStream
  106. * @description 推流
  107. */
  108. export function publishStream() {
  109. rtc.publish({
  110. streamName: 'main',
  111. // 推流成功,更新上麦结果
  112. success: function (stream) {
  113. rtc.updateMcResult({
  114. pid: 1,
  115. stid: stream.id(),
  116. success: function (data) {
  117. alert('更新上麦结果请求成功,此处可处理应用层逻辑' + JSON.stringify(data));
  118. },
  119. fail: function (data) {
  120. alert('更新上麦结果请求失败,此处可处理应用层逻辑' + JSON.stringify(data));
  121. }
  122. });
  123. },
  124. fail: function (stream) {
  125. // 推流失败,更新上麦结果
  126. rtc.updateMcResult({
  127. pid: 0,
  128. stid: stream.id(),
  129. success: function (data) {
  130. console.log('更新上麦结果请求成功,此处可处理应用层逻辑', data);
  131. },
  132. fail: function (data) {
  133. console.log('更新上麦结果请求失败,此处可处理应用层逻辑', data);
  134. }
  135. });
  136. alert('推流失败,此处可处理应用层逻辑');
  137. }
  138. });
  139. }
  140. /**
  141. * @method handsUp
  142. * @description 申请连麦
  143. */
  144. export function handsUp(data) {
  145. rtc.handsUp(data);
  146. }