inner-audio.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <view class="uni-padding-wrap">
  3. <page-head title="audio"></page-head>
  4. <view class="uni-common-mt">
  5. <slider :value="position" :min="0" :max="duration" @changing="onchanging" @change="onchange"></slider>
  6. </view>
  7. <!-- <view class="uni-common-mt play-time-area">
  8. <text class="current-time">{{currentTime}}</text>
  9. <text class="duration">{{duration}}</text>
  10. </view> -->
  11. <view class="play-button-area">
  12. <image class="icon-play" :src="playImage" @click="play"></image>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. const audioUrl = 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-hello-uniapp/2cc220e0-c27a-11ea-9dfb-6da8e309e0d8.mp3'
  18. export default {
  19. data() {
  20. return {
  21. title: "innerAudioContext",
  22. isPlaying: false,
  23. isPlayEnd: false,
  24. currentTime: 0,
  25. duration: 100
  26. }
  27. },
  28. computed: {
  29. position() {
  30. return this.isPlayEnd ? 0 : this.currentTime;
  31. },
  32. playImage() {
  33. return this.isPlaying ? "/static/pause.png" : "/static/play.png"
  34. }
  35. },
  36. onLoad() {
  37. this._isChanging = false;
  38. this._audioContext = null;
  39. this.createAudio();
  40. },
  41. onUnload() {
  42. if (this._audioContext != null && this.isPlaying) {
  43. this.stop();
  44. }
  45. },
  46. methods: {
  47. createAudio() {
  48. var innerAudioContext = this._audioContext = uni.createInnerAudioContext();
  49. innerAudioContext.autoplay = false;
  50. innerAudioContext.src = audioUrl;
  51. innerAudioContext.onPlay(() => {
  52. console.log('开始播放');
  53. });
  54. innerAudioContext.onTimeUpdate((e) => {
  55. if (this._isChanging === true) {
  56. return;
  57. }
  58. this.currentTime = innerAudioContext.currentTime || 0;
  59. this.duration = innerAudioContext.duration || 0;
  60. });
  61. innerAudioContext.onEnded(() => {
  62. this.currentTime = 0;
  63. this.isPlaying = false;
  64. this.isPlayEnd = true;
  65. });
  66. innerAudioContext.onError((res) => {
  67. this.isPlaying = false;
  68. console.log(res.errMsg);
  69. console.log(res.errCode);
  70. });
  71. return innerAudioContext;
  72. },
  73. onchanging() {
  74. this._isChanging = true;
  75. },
  76. onchange(e) {
  77. console.log(e.detail.value);
  78. console.log(typeof e.detail.value);
  79. this._audioContext.seek(e.detail.value);
  80. this._isChanging = false;
  81. },
  82. play() {
  83. if (this.isPlaying) {
  84. this.pause();
  85. return;
  86. }
  87. this.isPlaying = true;
  88. this._audioContext.play();
  89. this.isPlayEnd = false;
  90. },
  91. pause() {
  92. this._audioContext.pause();
  93. this.isPlaying = false;
  94. },
  95. stop() {
  96. this._audioContext.stop();
  97. this.isPlaying = false;
  98. }
  99. }
  100. }
  101. </script>
  102. <style>
  103. .play-time-area {
  104. display: flex;
  105. flex-direction: row;
  106. margin-top: 20px;
  107. }
  108. .duration {
  109. margin-left: auto;
  110. }
  111. .play-button-area {
  112. display: flex;
  113. flex-direction: row;
  114. justify-content: center;
  115. margin-top: 50px;
  116. }
  117. .icon-play {
  118. width: 60px;
  119. height: 60px;
  120. }
  121. </style>