im ok
This commit is contained in:
63
components/GoEasyAudioPlayer/goEasyAudioPlayer.js
Normal file
63
components/GoEasyAudioPlayer/goEasyAudioPlayer.js
Normal file
@@ -0,0 +1,63 @@
|
||||
Component({
|
||||
options: {
|
||||
addGlobalClass: true,
|
||||
},
|
||||
properties: {
|
||||
src: {
|
||||
type: String,
|
||||
value: ""
|
||||
},
|
||||
duration: {
|
||||
type: Number,
|
||||
value: 0
|
||||
}
|
||||
},
|
||||
data: {
|
||||
width: "",
|
||||
play: false,
|
||||
finalDuration: "",
|
||||
audioContext: null
|
||||
},
|
||||
methods: {
|
||||
playAudio() {
|
||||
// 播放时才创建audioContext,播放完毕销毁
|
||||
var self = this;
|
||||
this.setData({
|
||||
audioContext: wx.createInnerAudioContext()
|
||||
});
|
||||
this.data.audioContext.src = this.data.src;
|
||||
this.switchAudioState();
|
||||
setTimeout(() => {
|
||||
self.switchAudioState();
|
||||
self.data.audioContext.destroy();
|
||||
}, self.data.finalDuration*1000);
|
||||
|
||||
this.data.audioContext.play();
|
||||
this.data.audioContext.onPlay(()=>{
|
||||
console.log("正在播放......");
|
||||
});
|
||||
this.data.audioContext.onError((res) => {
|
||||
console.log("audio error:",res)
|
||||
});
|
||||
},
|
||||
switchAudioState(){
|
||||
this.setData({
|
||||
play: !this.data.play
|
||||
});
|
||||
},
|
||||
},
|
||||
attached: function() {
|
||||
// 在组件实例进入页面节点树时执行
|
||||
this.setData({
|
||||
width: Math.ceil(this.data.duration)*7+80,
|
||||
finalDuration: Math.ceil(this.data.duration),
|
||||
});
|
||||
},
|
||||
detached: function() {
|
||||
// 在组件实例被从页面节点树移除时执行
|
||||
// 语音还在播放时退出该界面时audioContext还没有被销毁,因此调用该方法清空audioContext
|
||||
if(this.data.audioContext != null){
|
||||
this.data.audioContext.destroy();
|
||||
}
|
||||
},
|
||||
})
|
||||
4
components/GoEasyAudioPlayer/goEasyAudioPlayer.json
Normal file
4
components/GoEasyAudioPlayer/goEasyAudioPlayer.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"component": true,
|
||||
"usingComponents": {}
|
||||
}
|
||||
7
components/GoEasyAudioPlayer/goEasyAudioPlayer.wxml
Normal file
7
components/GoEasyAudioPlayer/goEasyAudioPlayer.wxml
Normal file
@@ -0,0 +1,7 @@
|
||||
<view class="goeasy-audio-player" bindtap="playAudio">
|
||||
<view class="audio-facade" style="width:{{width}}rpx">
|
||||
<image wx:if="{{!play}}" class="audio-facade-bg" src="/static/images/audioImage/voice.png"></image>
|
||||
<image wx:else class="audio-facade-bg audio-play-icon" src="/static/images/audioImage/play.gif"></image>
|
||||
<view class="record-second">{{finalDuration}}</view>
|
||||
</view>
|
||||
</view>
|
||||
34
components/GoEasyAudioPlayer/goEasyAudioPlayer.wxss
Normal file
34
components/GoEasyAudioPlayer/goEasyAudioPlayer.wxss
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
.goeasy-audio-player {
|
||||
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
.audio-facade {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
min-width: 80rpx;
|
||||
max-width: 300rpx;
|
||||
height: 60rpx;
|
||||
padding: 6rpx 10rpx;
|
||||
border-radius: 14rpx;
|
||||
line-height: 30rpx;
|
||||
background: #D02129;
|
||||
font-size: 24rpx;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
.audio-facade .audio-play-icon {
|
||||
-moz-transform: rotate(180deg);
|
||||
-webkit-transform: rotate(180deg);
|
||||
-o-transform: rotate(180deg);
|
||||
transform: rotate(180deg);
|
||||
}
|
||||
|
||||
.audio-facade-bg {
|
||||
width: 40rpx;
|
||||
height: 35rpx;
|
||||
}
|
||||
|
||||
.record-second {
|
||||
padding-left: 14rpx;
|
||||
}
|
||||
Reference in New Issue
Block a user