useAudio.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**
  2. * 利用
  3. */
  4. import store from "@/store";
  5. import { computed, onMounted, watch, ref, unref, reactive } from "vue";
  6. import debounce from "lodash-es/debounce";
  7. import mitt from "mitt";
  8. let CLICKFIRST = false;
  9. const currentPlayer = ref(null);
  10. const isInit = ref(false);
  11. const currentAudio = computed(() => store.getters["audio/currentAudio"]);
  12. const currentAudioTemp = ref("");
  13. const isDoneforCover = computed(() => store.getters["scene/isDoneforCover"]);
  14. const currentScene = computed(() => store.getters["scene/currentScene"]);
  15. const metadata = computed(() => store.getters["scene/metadata"]);
  16. const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
  17. const isShowCover = computed(() =>
  18. store.getters["scene/metadata"].workCoverType && "isShowCover" in store.getters["scene/metadata"].workCoverType ? store.getters["scene/metadata"].workCoverType.isShowCover === 1 : false
  19. );
  20. async function initDefaultAudio() {
  21. watch(
  22. [isShowCover, isDoneforCover, currentScene],
  23. ([val1, val2, val3]) => {
  24. //开场完成后才开始初始化audioplayer
  25. console.log("initDefaultAudio", unref(val1), unref(val2));
  26. if (!unref(val1)) {
  27. // 开场封面关闭时
  28. initAudioPlayer();
  29. } else {
  30. // 开场封面开启时并跳转完成时
  31. if (unref(val2)) {
  32. initAudioPlayer();
  33. }
  34. }
  35. watchUpdateCurrentScenEexplanation(unref(val3));
  36. watchResetV4BGM(unref(val3));
  37. watchSWitchNormalBGM(unref(val3));
  38. },
  39. {
  40. deep: true,
  41. immediate: true,
  42. }
  43. );
  44. watch(
  45. [currentAudio, currentScene],
  46. ([val, ns], [_, os]) => {
  47. const oldValue = currentAudioTemp.value;
  48. const isSwitch = os && ns.sceneCode !== os.sceneCode;
  49. const newValue = unref(val).url;
  50. const isSame = newValue === oldValue;
  51. console.log("audio-isSame", isSame);
  52. if (newValue && newValue.length > 0) {
  53. if (unref(currentPlayer)) {
  54. const url = unref(val).url;
  55. const autoplay = unref(val).isAuto;
  56. const loop = unref(val).repeat;
  57. console.log("currentAudio", unref(val).url, autoplay, loop);
  58. if (!isSame) {
  59. unref(currentPlayer).switchUrl(url, autoplay, loop);
  60. currentAudioTemp.value = newValue;
  61. } else {
  62. //相同URL的再次播放
  63. if (unref(currentPlayer).isPlaying) {
  64. console.log("相同URL切换时在播放", isSwitch);
  65. unref(currentPlayer).resume();
  66. } else {
  67. if (!isSwitch) {
  68. console.log("相同URL没切换时toggle", isSwitch);
  69. unref(currentPlayer).resume();
  70. }
  71. }
  72. }
  73. }
  74. } else {
  75. console.log("为空暂时", isSwitch);
  76. if (unref(currentPlayer)) {
  77. unref(currentPlayer).stop();
  78. }
  79. if (isSwitch) {
  80. unref(currentPlayer)._src = "";
  81. }
  82. }
  83. },
  84. {
  85. deep: true,
  86. immediate: true,
  87. }
  88. );
  89. }
  90. function initAudioPlayer() {
  91. if (!unref(isInit)) {
  92. isInit.value = true;
  93. console.log("initAudioPlayer");
  94. const player = createAudioPlayer(unref(currentAudio).url, unref(currentAudio).isAuto, unref(currentAudio).repeat);
  95. currentPlayer.value = player;
  96. player.on("play", () => {
  97. console.log("play--22", player.isPlaying);
  98. store.dispatch("audio/updatePlayerStatus", player.isPlaying);
  99. });
  100. player.on("pause", () => {
  101. console.log("pause--33", player.isPlaying);
  102. store.dispatch("audio/updatePlayerStatus", player.isPlaying);
  103. });
  104. player.on("end", () => {
  105. console.log("end--33", player._loop);
  106. store.dispatch("audio/updatePlayerStatus", player.isPlaying);
  107. });
  108. window.store = store;
  109. }
  110. }
  111. function watchUpdateCurrentScenEexplanation(data) {
  112. // console.error("watchUpdateCurrentScenEexplanation", metadata.value, data);
  113. let currentExplanation = metadata.value.workExplanationList.find((item) => item.navigationId == data.id);
  114. // if ("explanation" in data) {
  115. if (currentExplanation) {
  116. store.dispatch("audio/initExplanationBGM", {
  117. url: currentExplanation.audioUrl,
  118. repeat: currentExplanation.playRepeat,
  119. isAuto: currentExplanation.openByDefault,
  120. });
  121. } else {
  122. console.log("not initExplanationBGM");
  123. store.dispatch("audio/initExplanationBGM", {
  124. url: "",
  125. repeat: false,
  126. isAuto: false,
  127. });
  128. }
  129. }
  130. function watchResetV4BGM(data) {
  131. console.log("data.type", data.type);
  132. if (data.type !== "4dkk") {
  133. store.dispatch("audio/initV4BGM", "");
  134. }
  135. }
  136. function watchSWitchNormalBGM(data) {
  137. if (data.type === "pano") {
  138. console.log("watchSWitchNormalBGM");
  139. if (store.getters["audio/isHasNormalBGM"]) {
  140. store.dispatch("audio/playBGM", 0);
  141. }
  142. }
  143. }
  144. function createAudioPlayer(url, autoplay, loop = true) {
  145. const player = new AudioPlayer({
  146. src: url,
  147. autoplay: autoplay,
  148. loop: loop,
  149. });
  150. return player;
  151. }
  152. class AudioPlayer {
  153. constructor(options) {
  154. this._src = options.src;
  155. this._loop = options.loop;
  156. this._autoplay = options.autoplay || false;
  157. this._isPlaying = false;
  158. this._firstPlay = false;
  159. this._lock = false;
  160. const emitter = mitt();
  161. Object.keys(emitter).forEach((method) => {
  162. this[method] = emitter[method];
  163. });
  164. this.audio = null;
  165. this.switchUrl = debounce(this.switchUrlSource, 80).bind(this);
  166. this.play = debounce(this.toPlay, 80).bind(this);
  167. this.init();
  168. }
  169. get isPlaying() {
  170. return this._isPlaying;
  171. }
  172. switchUrlSource(url, autoplay, loop) {
  173. if ("unload" in this.audio) {
  174. console.log("switchUrlSource-1");
  175. this.audio.unload();
  176. } else {
  177. console.log("switchUrlSource-2");
  178. return;
  179. }
  180. console.log("switchUrlSource", url, autoplay, loop);
  181. this._isPlaying = false;
  182. // this._lock = false;
  183. // this._firstPlay = false;
  184. this._autoplay = autoplay || false;
  185. this._loop = loop || false;
  186. this._src = url;
  187. this.audio = null;
  188. this.createAudio();
  189. this.play();
  190. this.emit("change", this.audio);
  191. }
  192. init() {
  193. this.createAudio();
  194. this.bindElement();
  195. this.play();
  196. this.emit("ready", this.audio);
  197. }
  198. createAudio() {
  199. this.audio = new Howl({
  200. preload: true,
  201. src: [this._src],
  202. loop: this._loop || false,
  203. html5: false,
  204. onplay: () => {
  205. this._isPlaying = true;
  206. this.emit("play");
  207. },
  208. onpause: () => {
  209. this._isPlaying = false;
  210. this.emit("pause");
  211. },
  212. onend: () => {
  213. this._isPlaying = false;
  214. console.log("onend", this._loop);
  215. // if (!this._loop) {
  216. // this.audio.unload();
  217. // }
  218. this.emit("end");
  219. },
  220. });
  221. }
  222. bindElement() {
  223. const $player = document.querySelector(".ui-view-layout");
  224. const onclick = () => {
  225. $player.removeEventListener("click", onclick);
  226. $player.removeEventListener("touchstart", onclick);
  227. //判断是否第一次进入或者是否已点击过 或已自动播放过。
  228. if (CLICKFIRST || this._isPlaying) {
  229. console.log("已点击过或自动播放中");
  230. return;
  231. }
  232. CLICKFIRST = true;
  233. this.play();
  234. };
  235. $player.addEventListener("click", onclick);
  236. $player.addEventListener("touchstart", onclick);
  237. }
  238. async toPlay() {
  239. try {
  240. if (!this._isPlaying && !this._lock) {
  241. // console.log("play---1", this._autoplay, this._lock, this._firstPlay);
  242. console.log(
  243. `playStatus: audioplay->${this._autoplay},
  244. lock:${this._lock},firstPlay:${this._firstPlay},loop:${this._loop}`
  245. );
  246. if (this._autoplay || this._firstPlay) {
  247. await this.audio.play();
  248. } else {
  249. //默认不自动播放重置状态并记录已播放过
  250. if (CLICKFIRST) {
  251. this._firstPlay = true;
  252. }
  253. this._isPlaying = false;
  254. store.dispatch("audio/updatePlayerStatus", false);
  255. }
  256. }
  257. } catch (error) {
  258. console.warn("playError", error);
  259. }
  260. }
  261. pause() {
  262. return this.audio.pause();
  263. }
  264. resume() {
  265. console.log("resume");
  266. if (!this._isPlaying) {
  267. return this.audio.play();
  268. }
  269. }
  270. stop() {
  271. this.pause();
  272. store.dispatch("audio/updatePlayerStatus", false);
  273. }
  274. destroy() {
  275. console.warn("audio-destroy");
  276. if ("unload" in this.audio) {
  277. this.audio.unload();
  278. }
  279. this._isPlaying = false;
  280. this._autoplay = false;
  281. this._loop = false;
  282. this._src = "";
  283. this.audio = null;
  284. store.dispatch("audio/updatePlayerStatus", false);
  285. this.emit("destroy");
  286. }
  287. lock() {
  288. console.log("audio-lock");
  289. this._lock = true;
  290. this.pause();
  291. }
  292. unlock() {
  293. console.log("audio-unlock");
  294. this._lock = false;
  295. }
  296. mute() {
  297. return this.audio.mute(true);
  298. }
  299. unmute() {
  300. return this.audio.mute(false);
  301. }
  302. }
  303. export function useAudio() {
  304. return {
  305. initDefaultAudio,
  306. currentPlayer,
  307. };
  308. }