inner-audio.uvue 7.8 KB
Newer Older
Anne_LXM's avatar
Anne_LXM 已提交
1
<template>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  <view class="uni-padding-wrap">
    <page-head title="audio"></page-head>
    <view class="uni-common-mt">
      <slider :value="position" :min="0" :max="duration" @changing="onchanging" @change="onchange"></slider>
    </view>
    <view class="uni-title">
      <text class="uni-title-text">属性示例</text>
    </View>
    <text class="uni-text-box uni-common-mt">当前音频播放位置(保留小数点后 6 位):{{currentTime}} s</text>
    <text class="uni-text-box">音频的长度(单位:s):{{duration}} s</text>
    <text class="uni-text-box">当前是否停止状态:{{isPaused}}</text>
    <text class="uni-text-box">音频缓冲的时间点:{{buffered}}</text>
    <text class="uni-text-box">当前音量:{{volume}}</text>
    <!-- 设置音量无效 -->
    <!-- <button plain :disabled="volume == 1" @click="increaseVolume">增加音量</button>
    <button plain :disabled="volume == 0" @click="decreaseVolume">减少音量</button> -->

    <text class="uni-subtitle-text uni-title">开始播放的位置(单位:s)</text>
    <input :value="startTime" type="number" placeholder="开始播放的位置(单位:s)" class="uni-input"
      @input="startTimeInput"></input>
    <boolean-data :defaultValue="false" title="是否自动开始播放" @change="setAutoplay"></boolean-data>
    <boolean-data :defaultValue="false" title="是否循环播放" @change="setLoop"></boolean-data>
    <view class="uni-title">
      <text class="uni-title-text">方法示例</text>
    </View>
    <button :disabled="isPlaying" type="primary" @click="play" class="uni-btn">播放</button>
    <button :disabled="!isPlaying" type="primary" @click="pause" class="uni-btn">暂停</button>
    <button :disabled="!isPlaying && !isPaused" type="primary" @click="stop" class="uni-btn">停止</button>
    <button type="primary" @click="onchange(20)" class="uni-btn">跳转到指定位置20</button>

Anne_LXM's avatar
Anne_LXM 已提交
32 33 34 35 36 37
    <view class="uni-title">
      <text class="uni-title-text">格式/路径示例</text>
    </View>
    <navigator url="/pages/API/inner-audio/inner-audio-format" class="uni-btn">
      <button type="primary" @click="pause">音频格式示例</button>
    </navigator>
38
    <navigator url="/pages/API/inner-audio/inner-audio-path" class="uni-btn uni-common-mb">
Anne_LXM's avatar
Anne_LXM 已提交
39 40
      <button type="primary" @click="pause">音频路径示例</button>
    </navigator>
41
  </view>
Anne_LXM's avatar
Anne_LXM 已提交
42 43
</template>
<script lang="uts">
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  const audioUrl = 'https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3'
  export default {
    data() {
      return {
        title: "innerAudioContext",
        currentTime: 0,
        duration: 100,
        startTime: 0,
        buffered: 0,
        volume: 0.5,
        isCanplay: false,
        isPlaying: false,
        isPaused: true,
        isPlayEnd: false,
        _isChanging: false,
        _audioContext: null as InnerAudioContext | null,
        // 自动化测试
        onSeekingTest:false,
        onSeekedTest:false,
        onWaitingTest:false
      }
    },
    computed: {
      position() {
        return this.isPlayEnd ? 0 : this.currentTime;
      },
    },
Anne_LXM's avatar
Anne_LXM 已提交
71 72 73
    onReady() {
      this._audioContext = uni.createInnerAudioContext();
      this._audioContext!.src = audioUrl;
74
      this.volume = this._audioContext!.volume;
Anne_LXM's avatar
Anne_LXM 已提交
75 76
      this.onCanplay()
    },
77 78 79
    onUnload() {
      if (this._audioContext != null && this.isPlaying) {
        this.stop();
Anne_LXM's avatar
Anne_LXM 已提交
80
        this._audioContext!.destroy()
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
      }
    },
    methods: {
      onCanplay() {
        this._audioContext!.onCanplay(() => {
          console.log('音频进入可以播放状态事件');
          this.isCanplay = true;
          // 当音频可以播放时,获取缓冲信息
          this.buffered = this._audioContext!.buffered;
          this.duration = this._audioContext!.duration || 0;
        });
      },
      onchanging() {
        this._isChanging = true;
      },
      onchange(e) {
        console.log(e, 'e');
        let pos = typeof e === "number" ? e : e.detail.value;
        this._audioContext!.seek(pos);
        this.onSeeking()
        this.onSeeked()
        this._isChanging = false;
      },
      startTimeInput(e : InputEvent) {
        let startTimeValue = Number(e.detail.value)
        this._audioContext!.startTime = startTimeValue;
        this.onchange(startTimeValue)
      },
      setAutoplay() {
Anne_LXM's avatar
Anne_LXM 已提交
110
        this._audioContext!.autoplay = !this._audioContext!.autoplay;
111
        console.log(this._audioContext!.autoplay, 'autoplay');
Anne_LXM's avatar
Anne_LXM 已提交
112
      },
113
      setLoop() {
Anne_LXM's avatar
Anne_LXM 已提交
114
        this._audioContext!.loop = !this._audioContext!.loop;
115
        console.log(this._audioContext!.loop, 'loop');
Anne_LXM's avatar
Anne_LXM 已提交
116
      },
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
      play() {
        if (!this.isCanplay) {
          uni.showToast({
            title: '音频未进入可以播放状态,请稍后再试'
          });
          return;
        }
        this.isPlaying = true;
        this._audioContext!.play();
        this.isPlayEnd = false;
        if (this._audioContext!.startTime > 0) {
          this.onchange(this._audioContext!.startTime)
        }
        this._audioContext!.onPlay(() => {
          this.isPaused = false;
          console.log('开始播放',this.isPaused);
        });
        this.onTimeUpdate()
        this.onWaiting()
        this.onError()
        this.onEnded()
Anne_LXM's avatar
Anne_LXM 已提交
138
      },
139
      onSeeking() {
Anne_LXM's avatar
Anne_LXM 已提交
140 141
        this._audioContext!.onSeeking(() => {
          console.log('音频进行 seek 操作事件');
142
          this.onSeekingTest = true
Anne_LXM's avatar
Anne_LXM 已提交
143 144
        });
      },
145
      onSeeked() {
Anne_LXM's avatar
Anne_LXM 已提交
146 147
        this._audioContext!.onSeeked(() => {
          console.log('音频完成 seek 操作事件');
148
          this.onSeekedTest = true
Anne_LXM's avatar
Anne_LXM 已提交
149 150
        });
      },
151 152 153 154
      onWaiting() {
        this._audioContext!.onWaiting(() => {
          console.log('音频加载中事件');
          this.onWaitingTest = true
Anne_LXM's avatar
Anne_LXM 已提交
155 156
        });
      },
157 158 159 160 161 162 163 164
      onTimeUpdate() {
        this._audioContext!.onTimeUpdate(() => {
          // console.log('onTimeUpdate:音频播放进度更新事件,currentTime',this._audioContext!.currentTime);
          if (this._isChanging === true) { return; }
          this.currentTime = this._audioContext!.currentTime || 0;
          if (this.currentTime > this.buffered) {
            console.log('缓冲不足');
          }
Anne_LXM's avatar
Anne_LXM 已提交
165 166
        });
      },
167 168 169 170 171 172 173 174 175
      increaseVolume() {
        this.volume = Math.min(this.volume + 0.1, 1);
        this.volume = parseFloat(this.volume.toFixed(1));
        console.log('增加音量', this.volume);
      },
      decreaseVolume() {
        this.volume = Math.max(this.volume - 0.1, 0);
        this.volume = parseFloat(this.volume.toFixed(1));
        console.log('减少音量', this.volume);
Anne_LXM's avatar
Anne_LXM 已提交
176
      },
177
      onEnded() {
Anne_LXM's avatar
Anne_LXM 已提交
178 179
        this._audioContext!.onEnded(() => {
          console.log('播放结束');
180 181 182 183 184
          this.currentTime = 0;
          this.startTime = 0
          this.isPlaying = false;
          this.isPaused = true;
          this.isPlayEnd = true;
Anne_LXM's avatar
Anne_LXM 已提交
185 186
        });
      },
187
      onError() {
Anne_LXM's avatar
Anne_LXM 已提交
188
        this._audioContext!.onError((err) => {
189 190 191
          console.log('err', err);
          this.isPlaying = false;
          this.isPaused = true;
Anne_LXM's avatar
Anne_LXM 已提交
192 193
        });
      },
194 195
      pause() {
        this._audioContext!.pause();
Anne_LXM's avatar
Anne_LXM 已提交
196 197
        this._audioContext!.onPause(() => {
          console.log('音频暂停事件');
198
          this.isPaused = true;
Anne_LXM's avatar
Anne_LXM 已提交
199
        });
200
        this.isPlaying = false;
Anne_LXM's avatar
Anne_LXM 已提交
201
      },
202 203 204
      stop() {
        console.log('stop');
        this._audioContext!.stop();
Anne_LXM's avatar
Anne_LXM 已提交
205
        this._audioContext!.onStop(() => {
206 207
          // 第一次点停止时,不触发
          this.isPaused = true;
Anne_LXM's avatar
Anne_LXM 已提交
208 209
          console.log('音频停止事件');
        });
210 211 212 213 214
        this.isPlaying = false;
        console.log('stop',this.isPaused);
      }
    }
  }
Anne_LXM's avatar
Anne_LXM 已提交
215 216
</script>
<style>
217 218 219 220 221
  .play-time-area {
    display: flex;
    flex-direction: row;
    margin-top: 20px;
  }
Anne_LXM's avatar
Anne_LXM 已提交
222

223 224 225
  .duration {
    margin-left: auto;
  }
Anne_LXM's avatar
Anne_LXM 已提交
226

227 228 229 230 231 232
  .play-button-area {
    display: flex;
    flex-direction: row;
    justify-content: center;
    margin: 50px 0;
  }
Anne_LXM's avatar
Anne_LXM 已提交
233

234 235 236 237
  .icon-play {
    width: 60px;
    height: 60px;
  }
Anne_LXM's avatar
Anne_LXM 已提交
238
</style>