Live.nvue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <view>
  3. <!-- <view class="account-form">
  4. <view class="uni-form-item">
  5. <view class="uni-input-wrapper">
  6. <view class="uni-label uni-label-must">直播间标题</view>
  7. <input class="uni-input" placeholder="请输入直播间标题" :value="form.liveroomTitle" @input="changeInput($event,'liveroomTitle')"/>
  8. </view>
  9. </view>
  10. </view> -->
  11. <button type="primary" class="cu-btn" v-if="!startState" @click="saveForm">开始直播</button>
  12. <button type="primary" class="cu-btn" v-if="startState" @click="downcast">关闭直播</button>
  13. <button type="primary" class="cu-btn" @click="switchCamera">切换摄像头</button>
  14. <live-pusher
  15. id="livePusher"
  16. ref="livePusher"
  17. :style="pusherCalss"
  18. class="livePusher"
  19. :url="tl_url"
  20. mode="FHD"
  21. :muted="false"
  22. :enable-camera="true"
  23. :auto-focus="true"
  24. :beauty="1"
  25. whiteness="2"
  26. aspect="9:16"
  27. @statechange="statechange"
  28. @netstatus="netstatus"
  29. @error="error"
  30. ></live-pusher>
  31. <!-- // 官方给的一些按钮,具体调用在下面 -->
  32. <!-- <button class="btn" @click="start">开始推流</button>
  33. <button class="btn" @click="pause">暂停推流</button>
  34. <button class="btn" @click="resume">resume</button>
  35. <button class="btn" @click="stop">停止推流</button>
  36. <button class="btn" @click="snapshot">快照</button>
  37. <button class="btn" @click="startPreview">开启摄像头预览</button>
  38. <button class="btn" @click="stopPreview">关闭摄像头预览</button>
  39. <button class="btn" @click="switchCamera">切换摄像头</button> -->
  40. </view>
  41. </template>
  42. <script>
  43. import http from '@/utils/http.js'
  44. export default {
  45. data() {
  46. return {
  47. // 视频宽高
  48. pusherCalss: {
  49. width: '200px',
  50. height: '300px'
  51. },
  52. // form本人测试请求的参数,不发请求可以不加
  53. form: {
  54. liveroomTitle: null, // 标题
  55. equipmentType: 1 // 设备类型(1.手机 2.电脑)
  56. },
  57. // 控制开启,关闭直播按钮的显示
  58. startState: false,// 直播状态(false 关闭)
  59. chapter_id:0,
  60. tl_url:''
  61. };
  62. },
  63. onReady() {
  64. // 注意:需要在onReady中 或 onLoad 延时
  65. this.context = uni.createLivePusherContext('livePusher', this);
  66. },
  67. onLoad(e) {
  68. this.chapter_id = e.chapter_id
  69. this.getInfo()
  70. // 获取可视区域高度,减去固定高度
  71. this.pusherCalss.width = uni.getSystemInfoSync().windowWidth;
  72. this.pusherCalss.height = uni.getSystemInfoSync().windowHeight;
  73. },
  74. mounted() {
  75. // 一进页面,先调用摄像头,保证摄像头是打开状态,不加也可以,手动开启,参考上面官方给出的那些按钮
  76. // this.startPreview();
  77. },
  78. methods: {
  79. getInfo(){
  80. http.post({
  81. data:{
  82. do:'LiveInfo',
  83. data:{
  84. user_id:uni.getStorageSync('id'),
  85. chapter_id:this.chapter_id
  86. }
  87. }
  88. }).then(res=>{
  89. //
  90. this.tl_url = res.data.live.complete_url
  91. // console.log(res.data.live.tl_url,'++++++')
  92. })
  93. },
  94. // 下面的这些方法,可以参考官网,具体查看每个方法的意义
  95. statechange(e) {
  96. console.log('statechange:' + JSON.stringify(e));
  97. },
  98. netstatus(e) {
  99. console.log('netstatus:' + JSON.stringify(e));
  100. },
  101. error(e) {
  102. console.log('error:' + JSON.stringify(e));
  103. },
  104. start: function() {
  105. this.context.start({
  106. success: a => {
  107. this.startState = true;
  108. console.log('livePusher.start:' + JSON.stringify(a));
  109. },
  110. fail:err=>{
  111. console.log(err,'失败了,失败了')
  112. }
  113. });
  114. },
  115. close: function() {
  116. this.context.close({
  117. success: a => {
  118. console.log('livePusher.close:' + JSON.stringify(a));
  119. }
  120. });
  121. },
  122. snapshot: function() {
  123. this.context.snapshot({
  124. success: e => {
  125. console.log(JSON.stringify(e));
  126. }
  127. });
  128. },
  129. resume: function() {
  130. this.context.resume({
  131. success: a => {
  132. console.log('livePusher.resume:' + JSON.stringify(a));
  133. }
  134. });
  135. },
  136. pause: function() {
  137. this.context.pause({
  138. success: a => {
  139. console.log('livePusher.pause:' + JSON.stringify(a));
  140. }
  141. });
  142. },
  143. stop: function() {
  144. this.context.stop({
  145. success: a => {
  146. this.startState = false;
  147. console.log(JSON.stringify(a));
  148. }
  149. });
  150. },
  151. switchCamera: function() {
  152. this.context.switchCamera({
  153. success: a => {
  154. console.log('livePusher.switchCamera:' + JSON.stringify(a));
  155. }
  156. });
  157. },
  158. startPreview: function() {
  159. this.context.startPreview({
  160. success: a => {
  161. console.log('livePusher.startPreview:' + JSON.stringify(a));
  162. }
  163. });
  164. },
  165. stopPreview: function() {
  166. this.context.stopPreview({
  167. success: a => {
  168. console.log('livePusher.stopPreview:' + JSON.stringify(a));
  169. }
  170. });
  171. },
  172. // 输入框改变(没有接口可以忽略,这里就是发送请求时起一个直播间的名字)
  173. changeInput: function(e, name) {
  174. this.form[name] = e.detail.value;
  175. },
  176. // 开始直播
  177. saveForm() {
  178. uni.showModal({
  179. title:'提示',
  180. content:'确定要开始直播吗?',
  181. success: (res) => {
  182. if(res.confirm){
  183. this.start()
  184. }
  185. }
  186. })
  187. // if(this.form.liveroomTitle == null || this.form.liveroomTitle == ""){
  188. // return;
  189. // }
  190. // // 这里是我自己的测试请求,因为需要和vue数据同步,做一个开播关播的数据交互,大家如果用不到的话可以直接调用 this.start();
  191. // getApp().$util.requestUrl(`/test/live/createLiveRecord?anchorId=2&liveroomTitle=${this.form.liveroomTitle}&equipmentType=${this.form.equipmentType}`).then(res => {
  192. // console.log(res)
  193. // if (res.status == 200) {
  194. // this.start();
  195. // }
  196. // });
  197. },
  198. // 关闭直播
  199. downcast(){
  200. uni.showModal({
  201. title:'提示',
  202. content:'确定要关闭直播吗?',
  203. success: (res) => {
  204. if(res.confirm){
  205. this.stopPreview()
  206. this.stop();
  207. uni.showToast({
  208. title:'直播已关闭',
  209. mask:true
  210. })
  211. setTimeout(()=>{
  212. uni.navigateBack()
  213. },1500)
  214. }
  215. }
  216. })
  217. // 同样,用不到接口只简单测试,调用this.stop();
  218. // vue的工具类等调用是this.$util;
  219. // uniapp使用getApp().$util
  220. // getApp().$util.requestUrl(`/test/live/downcast?anchorId=2`).then(res => {
  221. // console.log(res)
  222. // if (res.status == 200) {
  223. // this.stop();
  224. // }
  225. // });
  226. }
  227. }
  228. };
  229. </script>
  230. <style>
  231. .toLive{
  232. height: 45rpx;
  233. display: flex;
  234. justify-content: center;
  235. align-items: center;
  236. color: #405375;
  237. padding: 10rpx 20rpx;
  238. border: 1rpx solid #405375;
  239. border-radius: 25rpx;
  240. }
  241. .f-btns{
  242. width: 750rpx;
  243. height: 150rpx;
  244. }
  245. </style>