use-play.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import '@/assets/css/videojs.scss';
  2. import { getRandomString } from 'billd-utils';
  3. import md5 from 'crypto-js/md5';
  4. import mpegts from 'mpegts.js';
  5. import videoJs from 'video.js';
  6. import Player from 'video.js/dist/types/player';
  7. import { onMounted, onUnmounted, ref, watch } from 'vue';
  8. import { useAppStore } from '@/store/app';
  9. import { usePiniaCacheStore } from '@/store/cache';
  10. import { useUserStore } from '@/store/user';
  11. import { createVideo } from '@/utils';
  12. export * as flvJs from 'flv.js';
  13. export function useFlvPlay() {
  14. // const flvPlayer = ref<flvJs.Player>();
  15. const flvPlayer = ref<mpegts.Player>();
  16. const flvVideoEl = ref<HTMLVideoElement>();
  17. const cacheStore = usePiniaCacheStore();
  18. const appStore = useAppStore();
  19. const retryMax = ref(120);
  20. const retry = ref(0);
  21. const retrying = ref(false);
  22. onMounted(() => {});
  23. onUnmounted(() => {
  24. destroyFlv();
  25. });
  26. function destroyFlv() {
  27. if (flvPlayer.value) {
  28. flvPlayer.value.destroy();
  29. flvPlayer.value = undefined;
  30. }
  31. flvVideoEl.value?.remove();
  32. }
  33. function setMuted(val) {
  34. if (flvVideoEl.value) {
  35. flvVideoEl.value.muted = val;
  36. }
  37. if (flvPlayer.value) {
  38. flvPlayer.value.muted = val;
  39. }
  40. }
  41. function setVolume(val: number) {
  42. if (flvVideoEl.value) {
  43. flvVideoEl.value.volume = val / 100;
  44. }
  45. if (flvPlayer.value) {
  46. flvPlayer.value.volume = val / 100;
  47. }
  48. }
  49. function setPlay(val: boolean) {
  50. if (val) {
  51. flvVideoEl.value?.play();
  52. flvPlayer.value?.play();
  53. } else {
  54. flvVideoEl.value?.pause();
  55. flvPlayer.value?.pause();
  56. }
  57. }
  58. watch(
  59. () => cacheStore.muted,
  60. (newVal) => {
  61. setMuted(newVal);
  62. }
  63. );
  64. watch(
  65. () => cacheStore.volume,
  66. (newVal) => {
  67. setVolume(newVal);
  68. }
  69. );
  70. watch(
  71. () => appStore.play,
  72. (newVal) => {
  73. setPlay(newVal);
  74. }
  75. );
  76. function startFlvPlay(data: { flvurl: string }) {
  77. console.log('startFlvPlay', data.flvurl);
  78. return new Promise((resolve) => {
  79. function main() {
  80. destroyFlv();
  81. if (mpegts.getFeatureList().mseLivePlayback && mpegts.isSupported()) {
  82. flvPlayer.value = mpegts.createPlayer({
  83. type: 'flv', // could also be mpegts, m2ts, flv
  84. isLive: true,
  85. url: data.flvurl,
  86. });
  87. const videoEl = createVideo({});
  88. videoEl.addEventListener('play', () => {
  89. console.log('flv-play');
  90. });
  91. videoEl.addEventListener('playing', () => {
  92. console.log('flv-playing');
  93. retry.value = 0;
  94. setMuted(cacheStore.muted);
  95. setVolume(cacheStore.volume);
  96. flvVideoEl.value = videoEl;
  97. resolve('');
  98. });
  99. videoEl.addEventListener('loadedmetadata', () => {
  100. console.log('flv-loadedmetadata');
  101. });
  102. flvPlayer.value.attachMediaElement(videoEl);
  103. flvPlayer.value.load();
  104. flvPlayer.value.on(mpegts.Events.ERROR, () => {
  105. console.error('mpegts消息:mpegts.Events.ERROR');
  106. if (retry.value < retryMax.value && !retrying.value) {
  107. retrying.value = true;
  108. destroyFlv();
  109. setTimeout(() => {
  110. console.error(
  111. '播放flv错误,重新加载,剩余次数:',
  112. retryMax.value - retry.value
  113. );
  114. retry.value += 1;
  115. retrying.value = false;
  116. main();
  117. }, 1000);
  118. }
  119. });
  120. flvPlayer.value.on(mpegts.Events.MEDIA_INFO, () => {
  121. console.log('mpegts消息:mpegts.Events.MEDIA_INFO');
  122. });
  123. try {
  124. console.log(`开始播放flv,muted:${cacheStore.muted}`);
  125. flvPlayer.value.play();
  126. } catch (err) {
  127. console.error('flv播放失败');
  128. console.log(err);
  129. }
  130. } else {
  131. console.error('不支持flv');
  132. }
  133. }
  134. main();
  135. });
  136. }
  137. return { flvPlayer, flvVideoEl, startFlvPlay, destroyFlv };
  138. }
  139. export function useHlsPlay() {
  140. const hlsPlayer = ref<Player>();
  141. const hlsVideoEl = ref<HTMLVideoElement>();
  142. const cacheStore = usePiniaCacheStore();
  143. const appStore = useAppStore();
  144. const userStore = useUserStore();
  145. const retryMax = ref(120);
  146. const retry = ref(0);
  147. const retrying = ref(false);
  148. onMounted(() => {});
  149. onUnmounted(() => {
  150. destroyHls();
  151. });
  152. function destroyHls() {
  153. if (hlsPlayer.value) {
  154. hlsPlayer.value.dispose();
  155. hlsPlayer.value = undefined;
  156. }
  157. hlsVideoEl.value?.remove();
  158. }
  159. function setMuted(val: boolean) {
  160. if (hlsVideoEl.value) {
  161. hlsVideoEl.value.muted = val;
  162. }
  163. if (hlsPlayer.value) {
  164. hlsPlayer.value.muted(val);
  165. }
  166. }
  167. function setVolume(val: number) {
  168. if (hlsVideoEl.value) {
  169. hlsVideoEl.value.volume = val / 100;
  170. }
  171. if (hlsPlayer.value) {
  172. hlsPlayer.value.volume(val / 100);
  173. }
  174. }
  175. function setPlay(val: boolean) {
  176. if (val) {
  177. hlsVideoEl.value?.play();
  178. hlsPlayer.value?.play();
  179. } else {
  180. hlsVideoEl.value?.pause();
  181. hlsPlayer.value?.pause();
  182. }
  183. }
  184. watch(
  185. () => cacheStore.muted,
  186. (newVal) => {
  187. setMuted(newVal);
  188. }
  189. );
  190. watch(
  191. () => cacheStore.volume,
  192. (newVal) => {
  193. setVolume(newVal);
  194. }
  195. );
  196. watch(
  197. () => appStore.play,
  198. (newVal) => {
  199. setPlay(newVal);
  200. }
  201. );
  202. function startHlsPlay(data: { hlsurl: string }) {
  203. return new Promise((resolve) => {
  204. function main() {
  205. console.log('startHlsPlay', data.hlsurl);
  206. destroyHls();
  207. const videoEl = createVideo({
  208. muted: cacheStore.muted,
  209. autoplay: true,
  210. });
  211. const userInfo = userStore.userInfo;
  212. const userToken = md5(userStore.token) as string;
  213. console.log('userdddd', userInfo);
  214. hlsPlayer.value = videoJs(
  215. videoEl,
  216. {
  217. sources: [
  218. {
  219. src: !userInfo
  220. ? data.hlsurl
  221. : `${
  222. data.hlsurl
  223. }?usertoken=${userToken}&userid=${userInfo.id!}&randomid=${getRandomString(
  224. 8
  225. )}`,
  226. type: 'application/x-mpegURL',
  227. },
  228. ],
  229. },
  230. function () {
  231. try {
  232. // console.log(`开始播放hls,muted:${cacheStore.muted}`);
  233. hlsPlayer.value?.play();
  234. } catch (err) {
  235. console.error('hls播放失败');
  236. console.log(err);
  237. }
  238. }
  239. );
  240. hlsPlayer.value?.on('error', () => {
  241. console.log('hls-error');
  242. if (retry.value < retryMax.value && !retrying.value) {
  243. retrying.value = true;
  244. setTimeout(() => {
  245. console.error(
  246. '播放hls错误,重新加载,剩余次数:',
  247. retryMax.value - retry.value
  248. );
  249. retry.value += 1;
  250. retrying.value = false;
  251. main();
  252. }, 1000);
  253. }
  254. });
  255. hlsPlayer.value?.on('play', () => {
  256. console.log('hls-play');
  257. // console.log(hlsPlayer.value?.videoHeight()); // 获取到的是0!
  258. });
  259. hlsPlayer.value?.on('playing', () => {
  260. console.log('hls-playing');
  261. setMuted(cacheStore.muted);
  262. setVolume(cacheStore.volume);
  263. retry.value = 0;
  264. // console.log(hlsPlayer.value?.videoHeight()); // 获取到的是正确的!
  265. const childNodes = hlsPlayer.value?.el().childNodes;
  266. if (childNodes) {
  267. childNodes.forEach((item) => {
  268. if (item.nodeName.toLowerCase() === 'video') {
  269. // @ts-ignore
  270. hlsVideoEl.value = item;
  271. }
  272. });
  273. }
  274. resolve('');
  275. });
  276. hlsPlayer.value?.on('loadedmetadata', () => {
  277. console.log('hls-loadedmetadata');
  278. });
  279. }
  280. main();
  281. });
  282. }
  283. return { hlsPlayer, hlsVideoEl, startHlsPlay, destroyHls };
  284. }