useAudio.js 8.9 KB

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