LiveVideo.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <script>
  2. import { createWatchCore, PlayerEvents, ChatEvents } from '@polyv/live-watch-sdk';
  3. export default {
  4. name: "LiveVideo",
  5. data() {
  6. return {
  7. watchCore: null
  8. }
  9. },
  10. mounted() {
  11. this.getWatchCore();
  12. },
  13. methods: {
  14. async getWatchCore() {
  15. this.watchCore = createWatchCore({
  16. channelId: "4247961",
  17. userInfo: {
  18. userId: this.$route.query.id || "e6soizn34h"
  19. }
  20. })
  21. try {
  22. await this.watchAuth();
  23. await this.watchCore.setup();
  24. // 连接
  25. await this.watchCore.connect();
  26. await this.createPlayer();
  27. this.getChatContent();
  28. } catch (err) {
  29. console.error(err);
  30. }
  31. },
  32. // 观看授权
  33. watchAuth() {
  34. return new Promise( (resolve, reject) => {
  35. this.watchCore.auth.verifyNoneAuth().then(result => {
  36. if (result.success) {
  37. resolve(result)
  38. } else {
  39. reject(result)
  40. }
  41. });
  42. })
  43. },
  44. // 创建播放器
  45. async createPlayer() {
  46. this.watchCore.player.eventEmitter.on(PlayerEvents.PlayerInfoChange, (data) => {
  47. const playerInfo = data.playerInfo;
  48. console.log('播放状态', playerInfo.playStatus);
  49. });
  50. await this.watchCore.player.setupPlayer({
  51. container: document.getElementById('liveVideoContainer'),
  52. barrageEnabled: false,
  53. autoplay: true,
  54. barrageShow: false,
  55. showController: false,
  56. // lowLatency: true, // 使用无延迟播放
  57. });
  58. },
  59. // 获取聊天内容
  60. async getChatContent() {
  61. console.log("来获取聊天内容啦")
  62. const list = await this.watchCore.chat.getChatHistory();
  63. console.log("聊天内容", list);
  64. this.listenChat();
  65. },
  66. // 监听聊天信息
  67. async listenChat() {
  68. this.watchCore.chat.eventEmitter.on(ChatEvents.ChatMessage, (data) => {
  69. console.log("聊天信息", data);
  70. });
  71. }
  72. }
  73. }
  74. </script>
  75. <template>
  76. <div class="live-video" id="liveVideoContainer"></div>
  77. </template>
  78. <style scoped>
  79. .live-video {
  80. width: 100%;
  81. //height: 56.25vw;
  82. }
  83. /deep/ .plwrap {
  84. background: transparent;
  85. }
  86. </style>