inner-audio-path.uvue 2.6 KB
Newer Older
Anne_LXM's avatar
Anne_LXM 已提交
1 2 3 4
<template>
  <page-head :title="title"></page-head>
  <view class="uni-padding-wrap uni-common-mt">
    <view class="uni-title">
5
      <text class="uni-title-text">音频路径示例</text>
Anne_LXM's avatar
Anne_LXM 已提交
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 32 33 34 35 36 37 38
    </view>
    <view class="formats" v-for="(item,index) in supportPaths" :key="index">
      <text class="uni-subtitle-text">{{item.description}}</text>
      <image class="icon-play" :src="(isPlaying && playIndex==index)?'/static/pause.png':'/static/play.png'" @click="play(item.src,index)"></image>
    </view>
  </view>
</template>

<script>
  type AudioPath = {
    description : string
    src : string
  }
  export default {
    data() {
      return {
        title: 'audio-path',
        playIndex:0,
        isPlaying: false,
        _audioContext: null as InnerAudioContext | null,
        supportPaths: [
          {
            description: '本地路径:/static方式',
            src: '/static/test-audio/ForElise.mp3'
          },
          {
            description: '本地路径:../static/',
            src: '../../../static/test-audio/ForElise.mp3'
          },
          {
            description: '网络路径',
            src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp3'
          },
39 40 41 42 43 44 45 46
          {
            description: '不存在的音频',
            src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/invalid_url.mp3'
          },
          {
            description: '错误路径',
            src: '../static/test-audio/ForElise.mp3'
          },
Anne_LXM's avatar
Anne_LXM 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        ] as Array<AudioPath>
      }
    },
    onReady() {
      this._audioContext = uni.createInnerAudioContext();
    },
    onUnload() {
    	if (this._audioContext != null) {
        this.pause();
    		this._audioContext!.destroy()
    	}
    },
    methods: {
      pause() {
      	this._audioContext!.pause();
      	this.isPlaying = false;
      },
      play(audioUrl,index){
        // console.log(index,audioUrl);
        if (this.isPlaying && this.playIndex == index) {
        	this.pause();
        	return;
        }
        this.playIndex = index
        this._audioContext!.src = audioUrl;
        this._audioContext!.play();
        this.isPlaying = true;
        this._audioContext!.onPlay(() => {
        	console.log('开始播放');
        });
        this._audioContext!.onEnded(() => {
          console.log('播放结束');
        	this.isPlaying = false;
        });
        this._audioContext!.onError((err) => {
          this.isPlaying = false;
        	console.log('err',err);
        });
      }
    }
  }
</script>

<style>
  .formats{
    align-items: center;
  }
  .icon-play {
  	width: 60px;
  	height: 60px;
    margin: 10px;
  }
</style>