Blame view

pages/API/inner-audio/inner-audio.vue 2.79 KB
4b045f7c   刘淇   江阴初始化项目
1
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
32
33
34
35
36
37
38
39
40
41
42
43
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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  <template>
  	<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-common-mt play-time-area">
  			<text class="current-time">{{currentTime}}</text>
  			<text class="duration">{{duration}}</text>
  		</view> -->
  		<view class="play-button-area">
  			<image class="icon-play" :src="playImage" @click="play"></image>
  		</view>
  	</view>
  </template>
  <script>
  	const audioUrl = 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-hello-uniapp/2cc220e0-c27a-11ea-9dfb-6da8e309e0d8.mp3'
  	export default {
  		data() {
  			return {
  				title: "innerAudioContext",
  				isPlaying: false,
  				isPlayEnd: false,
  				currentTime: 0,
  				duration: 100
  			}
  		},
  		computed: {
  			position() {
  				return this.isPlayEnd ? 0 : this.currentTime;
  			},
  			playImage() {
  				return this.isPlaying ? "/static/pause.png" : "/static/play.png"
  			}
  		},
  		onLoad() {
  			this._isChanging = false;
  			this._audioContext = null;
  			this.createAudio();
  		},
  		onUnload() {
  			if (this._audioContext != null && this.isPlaying) {
  				this.stop();
  			}
  		},
  		methods: {
  			createAudio() {
  				var innerAudioContext = this._audioContext = uni.createInnerAudioContext();
  				innerAudioContext.autoplay = false;
  				innerAudioContext.src = audioUrl;
  				innerAudioContext.onPlay(() => {
  					console.log('开始播放');
  				});
  				innerAudioContext.onTimeUpdate((e) => {
  					if (this._isChanging === true) {
  						return;
  					}
  					this.currentTime = innerAudioContext.currentTime || 0;
  					this.duration = innerAudioContext.duration || 0;
  				});
  				innerAudioContext.onEnded(() => {
  					this.currentTime = 0;
  					this.isPlaying = false;
  					this.isPlayEnd = true;
  				});
  				innerAudioContext.onError((res) => {
  					this.isPlaying = false;
  					console.log(res.errMsg);
  					console.log(res.errCode);
  				});
  				return innerAudioContext;
  			},
  			onchanging() {
  				this._isChanging = true;
  			},
  			onchange(e) {
  				console.log(e.detail.value);
  				console.log(typeof e.detail.value);
  				this._audioContext.seek(e.detail.value);
  				this._isChanging = false;
  			},
  			play() {
  				if (this.isPlaying) {
  					this.pause();
  					return;
  				}
  				this.isPlaying = true;
  				this._audioContext.play();
  				this.isPlayEnd = false;
  			},
  			pause() {
  				this._audioContext.pause();
  				this.isPlaying = false;
  			},
  			stop() {
  				this._audioContext.stop();
  				this.isPlaying = false;
  			}
  		}
  	}
  </script>
  <style>
  	.play-time-area {
  		display: flex;
  		flex-direction: row;
  		margin-top: 20px;
  	}
  
  	.duration {
  		margin-left: auto;
  	}
  
  	.play-button-area {
  		display: flex;
  		flex-direction: row;
  		justify-content: center;
  		margin-top: 50px;
  	}
  
  	.icon-play {
  		width: 60px;
  		height: 60px;
  	}
  </style>