wangzhibo
6 天以前 da0230346106bc810664488a2b152bc24f853a44
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!-- components/ezuikit-player.vue -->
<script lang="ts" setup>
defineOptions({
  name: 'ezuikit-player'
});
import { EZUIKitPlayer } from "ezuikit-js";
import { onMounted, onBeforeUnmount, ref, watch } from "vue";
 
interface IPlayer {
  play: Function;
  stop: Function;
  getOSDTime: Function;
  capturePicture: Function;
  openSound: Function;
  closeSound: Function;
  startSave: Function;
  stopSave: Function;
  startTalk: Function;
  stopTalk: Function;
  fullscreen: Function;
  destroy: Function;
  on: any
}
 
const props = defineProps<{
  containerId: string; // 【重要】外部传入唯一DOM ID
  accessToken: string;
  url: string;
  staticPath?: string;
}>();
 
let player: IPlayer | null = null;
 
// 方法暴露给父组件
const play = () => {
  if (player) player.play()
};
 
const stop = () => {
  if (player) player.stop();
};
 
const getOSDTime = () => {
  if (player)
    player.getOSDTime().then((data: any) => {
      console.log("getOSDTime 获取 数据", data);
    });
};
 
const capturePicture = () => {
  if (player)
    player.capturePicture(`${new Date().getTime()}`).then((data: any) => {
      console.log("capturePicture 获取 数据", data);
    });
};
 
const openSound = () => {
  if (player) player.openSound();
};
 
const closeSound = () => {
  if (player) player.closeSound();
};
 
const startSave = () => {
  if (player)
    player.startSave(`${new Date().getTime()}`).then((data: any) => {
      console.log("startSave 获取 数据", data);
    });
};
 
const stopSave = () => {
  if (player)
    player.stopSave().then((data: any) => {
      console.log("stopSave 获取 数据", data);
    });
};
 
const startTalk = () => {
  if (player) player.startTalk();
};
 
const stopTalk = () => {
  if (player) player.stopTalk();
};
 
const fullscreen = () => {
  if (player) player.fullscreen();
};
 
const destroy = async () => {
  if (player) {
    try {
      await player.destroy();
    } catch (e) {
      console.warn("播放器销毁异常", e);
    }
    player = null;
  }
};
 
let playedOnce = false;
const init = () => {
  if (player) {
    destroy();
  }
  if (!props.containerId || !props.url || !props.accessToken) return;
 
  player = new EZUIKitPlayer({
    id: props.containerId,
    accessToken: props.accessToken,
    url: props.url,
    autoplay: true,
    template: "pcLive",
    height: 360,
    scaleMode: 1,
    staticPath: props.staticPath || "",
    env: {
      domain: "https://open.ys7.com",
    },
    loggerOptions: {
      level: "INFO",
      name: "ezuikit",
      showTime: true,
    },
    streamInfoCBType: 1,
    handleError: (err: any) => {
      console.error("handleError", err);
    }
  });
 
  player.on(EZUIKitPlayer.EVENTS.firstFrameDisplay, () => {
    if (playedOnce) return;
    playedOnce = true;
    setTimeout(() => {
      player?.pause();
    }, 1000);
  });  
 
  // 事件监听
  player.on(EZUIKitPlayer.EVENTS.videoInfo, (info: any) => {
    console.warn("videoInfo", info);
  });
  player.on(EZUIKitPlayer.EVENTS.audioInfo, (info: any) => {
    console.warn("audioInfo", info);
  });
  player.on(EZUIKitPlayer.EVENTS.firstFrameDisplay, () => {
    console.warn("firstFrameDisplay ");
  });
  player.on(EZUIKitPlayer.EVENTS.streamInfoCB, (info: any) => {
    console.warn("streamInfoCB ", info);
  });
};
 
// 监听播放地址/token变化,重新初始化
watch([() => props.url, () => props.accessToken], () => {
  init();
});
 
onMounted(() => {
  init();
});
 
// 组件销毁释放播放器资源
onBeforeUnmount(() => {
  destroy();
});
 
// 对外暴露实例方法
defineExpose({
  play,
  stop,
  openSound,
  closeSound,
  capturePicture,
  startSave,
  stopSave,
  startTalk,
  stopTalk,
  fullscreen,
  getOSDTime,
  destroy
});
</script>
 
<template>
  <div class="ez-player-card">
    <!-- ID由父组件传入,保证全局唯一 -->
    <div :id="containerId" style="height: 400px;width:100%"></div>
    <!-- 
    <div class="player-buttons" style="margin-top:8px;display:flex;gap:6px;flex-wrap:wrap">
      <button @click="init">init</button>
      <button @click="stop">stop</button>
      <button @click="play">play</button>
      <button @click="openSound">开声音</button>
      <button @click="closeSound">关声音</button>
      <button @click="capturePicture">截图</button>
      <button @click="fullscreen">全屏</button>
    </div>
    -->
  </div>
</template>
 
<style scoped>
.ez-player-card {
  border: 0px solid #e4e7ed;
  border-radius: 6px;
  padding: 0px;
  background: #000;
}
</style>