bgm.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import Vue from 'vue'
  2. var bgm = {
  3. show : false,
  4. playing : false,
  5. manager : false,
  6. control : false,
  7. init : function(source, music, title, signer, pic, autoplay) {
  8. if (this.manager) {
  9. return;
  10. }
  11. Vue.bgm = {
  12. show : false,
  13. playing : false,
  14. };
  15. if (music) {
  16. this.show = true;
  17. Vue.bgm.show = true;
  18. if (source == 'h5') {
  19. this.manager = uni.createInnerAudioContext()
  20. this.manager.src = music;
  21. this.manager.obeyMuteSwitch = false;
  22. this.manager.loop = true;
  23. } else {
  24. this.manager = uni.getBackgroundAudioManager()
  25. this.manager.title = title;
  26. this.manager.singer = signer;
  27. this.manager.coverImgUrl = pic;
  28. this.manager.src = music;
  29. this.manager.loop = true;
  30. }
  31. if (autoplay == 1) {
  32. if (source == 'h5') {
  33. this.manager.autoplay = true;
  34. this.start();
  35. } else {
  36. this.start();
  37. }
  38. } else {
  39. this.stop();
  40. }
  41. } else {
  42. this.show = false;
  43. Vue.bgm.show = false;
  44. }
  45. },
  46. control : function() {
  47. if (this.playing) {
  48. this.stop();
  49. } else {
  50. this.start();
  51. }
  52. },
  53. start : function() {
  54. if (this.manager) {
  55. if (Vue.control) {
  56. var i = 0;
  57. for (i in Vue.control) {
  58. if (Vue.control[i].load) {
  59. Vue.control[i].stop();
  60. }
  61. }
  62. }
  63. this.manager.play();
  64. this.playing = true;
  65. Vue.bgm.playing = true;
  66. }
  67. },
  68. stop : function() {
  69. if (this.manager) {
  70. this.manager.pause();
  71. this.playing = false;
  72. Vue.bgm.playing = false;
  73. }
  74. }
  75. };
  76. export default bgm;