Skip to content

Commit b706f3c

Browse files
committed
update: add inner-audio 示例
1 parent 22d0e11 commit b706f3c

File tree

4 files changed

+135
-1
lines changed

4 files changed

+135
-1
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ root = true
55
# all files
66
[*]
77
charset = utf-8
8-
indent_style = space
8+
indent_style = tab
99
indent_size = 4
1010
end_of_line = lf
1111
insert_final_newline = true

pages.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,12 @@
542542
"style": {
543543
"navigationBarTitleText": "录音"
544544
}
545+
},
546+
{
547+
"path": "inner-audio/inner-audio",
548+
"style": {
549+
"navigationBarTitleText": "音频"
550+
}
545551
},
546552
{
547553
"path": "background-audio/background-audio",

pages/API/inner-audio/inner-audio.vue

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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://img-cdn-qiniu.dcloud.net.cn/uniapp/audio/music.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+
109+
.duration {
110+
margin-left: auto;
111+
}
112+
113+
.play-button-area {
114+
display: flex;
115+
flex-direction: row;
116+
justify-content: center;
117+
margin-top: 50px;
118+
}
119+
120+
.icon-play {
121+
width: 60px;
122+
height: 60px;
123+
}
124+
</style>

pages/tabBar/API/API.nvue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
name: '图片',
4242
url: 'image'
4343
},
44+
{
45+
name: '音频',
46+
url: 'inner-audio'
47+
},
4448
// #ifdef APP-PLUS || MP-WEIXIN || MP-BAIDU || MP-QQ
4549
{
4650
name: '录音',

0 commit comments

Comments
 (0)