uni-im-sound.vue 5.5 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
2 3 4
	<view class="sound-buttom-box">
		<view @touchmove="touchmove" @touchstart="recordStart" @touchend="recordEnd" @touchcancel="recordEnd"
			class="sound-buttom" :class="{recordState}">
DCloud_JSON's avatar
DCloud_JSON 已提交
5
			<view v-if="soundProgress" class="sound-progress" :style="{'width':soundProgress}"></view>
6 7
			<text class="sound-text">{{recordState?'录音中('+time+'s)':'按住 说话'}}</text>
			<view class="sound-tip" v-if="recordState">
DCloud_JSON's avatar
DCloud_JSON 已提交
8 9
				<text class="sound-tip-text" :style="{color:cancel?'#f70000':'#FFFFFF'}">{{cancel?'松手取消':'松手发送,上划取消'}}</text>
				<view class="closeIcon" :style="{'background-color':cancel?'#f70000':'#EEEEEE'}">
10
					<uni-im-icons class="icon" code="e61a" size="10px" color="#FFFFFF"></uni-im-icons>
DCloud_JSON's avatar
DCloud_JSON 已提交
11 12 13
				</view>
			</view>
		</view>
14
    <view v-if="recordState" :style="{bottom:markBottom}" class="mark"></view>
DCloud_JSON's avatar
DCloud_JSON 已提交
15 16 17 18 19 20 21 22 23 24 25
	</view>
</template>

<script>
	// #ifndef H5
	const recorderManager = uni.getRecorderManager();
	// #endif

	import uniIm from '@/uni_modules/uni-im/sdk/index.js';
	let soundInterval,soundPath,startTime;
	export default {
26
		emits: ['sendSoundMsg'],
DCloud_JSON's avatar
DCloud_JSON 已提交
27 28
		data() {
			return {
29
				recordState:0,
DCloud_JSON's avatar
DCloud_JSON 已提交
30 31 32 33 34 35 36 37 38
				soundProgress:0,
				cancel:false,
				time:0,
				phoneBH:0
			}
		},
		computed:{
      ...uniIm.mapState(['systemInfo']),
			markBottom(){
39
				let markBottom = 67;
DCloud_JSON's avatar
DCloud_JSON 已提交
40
				// #ifndef H5
41
				markBottom += this.systemInfo.safeAreaInsets.bottom/2
DCloud_JSON's avatar
DCloud_JSON 已提交
42 43 44 45 46 47 48 49
				// #endif
        console.log('markBottom',markBottom);
				return markBottom  + 'px'
			},
		},
		created() {
			// #ifndef H5
			recorderManager.onStop((res)=> {
50
				console.log('recorderManager.onStop',{res});
DCloud_JSON's avatar
DCloud_JSON 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63
				if(!this.cancel){
					if(this.time < 2){
						return uni.showToast({
							title: '语音时间过短',
							icon: 'none'
						});
					}
					uni.showLoading({
						title: '上传中',
						mask: false
					});
					uniCloud.uploadFile({
						filePath:res.tempFilePath,
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
64
						cloudPath:'uni-im/' + uniIm.currentUser._id + '/sound/' + Date.now() + '.mp3',
DCloud_JSON's avatar
DCloud_JSON 已提交
65 66
						// fileType:"audio",
						success: (e) => {
67
							// console.log('uniCloud.uploadFile-sendSoundMsg',e,'sendSoundMsg',{"url":e.fileID,time:this.time});
DCloud_JSON's avatar
DCloud_JSON 已提交
68
							try{
69
								this.$emit('sendSoundMsg',{"url":e.fileID,time:this.time})
DCloud_JSON's avatar
DCloud_JSON 已提交
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
							}catch(e){
								console.error(e);
							}
							uni.hideLoading()
						},
						fail: (e) =>{
							console.log(e);
							uni.showModal({
								content: JSON.stringify(e),
								showCancel: false,
								confirmText: '知道了'
							});
						},
						complete: (e) =>{
							console.log('complete',e);
							uni.hideLoading()
						}
					})
				}else{
					console.log('用户取消了录音功能','this.time:'+this.time);
				}
			});
			
			recorderManager.onStart(e=>{
				// console.log(e);
			})
			recorderManager.onPause(e=>{
				// console.log(e);
			})
			recorderManager.onError(e=>{
				console.error(e);
			})
			// #endif
		},
		methods: {
			touchmove(e){
106 107 108 109 110
        let touchY = e.touches[0].clientY + this.systemInfo.statusBarHeight + this.systemInfo.safeArea.top
        // #ifdef H5
        touchY += 44
        // #endif
        this.cancel = this.systemInfo.safeArea.bottom - touchY > 66
DCloud_JSON's avatar
DCloud_JSON 已提交
111
			},
112
			recordStart(e){
DCloud_JSON's avatar
DCloud_JSON 已提交
113 114 115 116 117 118 119 120 121 122
				// 关闭正在播放的sound
				uniIm.audioContext.stop()
				this.time = 0
				
				// #ifdef H5
				// 防止H5端 调试出现鼠标右键菜单
				window.oncontextmenu = function () { return false; }
				// #endif

				// #ifdef H5
123 124 125 126
				// return uni.showToast({
				// 	title: 'h5端不支持语音功能',
				// 	icon: 'none'
				// });
DCloud_JSON's avatar
DCloud_JSON 已提交
127 128 129 130 131 132 133 134 135 136 137 138
				// #endif

				// #ifndef H5
				recorderManager.start({
					sampleRate:16000,
					numberOfChannels:2,
					format:"mp3"
				});
				// #endif

				startTime = Date.now()

139
				console.log('recordStart');
DCloud_JSON's avatar
DCloud_JSON 已提交
140 141

				//进度条
142
				this.recordState = 1
DCloud_JSON's avatar
DCloud_JSON 已提交
143 144 145 146 147 148 149
				soundInterval = setInterval(()=>{
					this.soundProgress = parseInt(this.soundProgress) + uni.upx2px(450/60) +'px'
					// console.log('this.soundProgress',this.soundProgress);
					this.time = parseInt((Date.now() - startTime) / 1000)
				},1000)
				// e.preventDefault();
			},
150
			recordEnd(){
DCloud_JSON's avatar
DCloud_JSON 已提交
151 152 153
				// #ifndef H5
				recorderManager.stop();
				// #endif
154
				console.log('recordEnd');
DCloud_JSON's avatar
DCloud_JSON 已提交
155 156
				clearInterval(soundInterval)
				setTimeout(()=> {
157
					this.recordState = 0
DCloud_JSON's avatar
DCloud_JSON 已提交
158 159 160 161 162 163 164 165 166
					this.soundProgress = 0
					this.cancel = false
				}, 300);
			}
		}
	}
</script>

<style lang="scss">
167 168 169
  .sound-buttom-box {
    height: 100%;
  }
DCloud_JSON's avatar
DCloud_JSON 已提交
170 171 172
	.sound-buttom {
		background-color: #ffffff;
		padding: 10px;
173 174
		width: 100%;
    flex: 1;
DCloud_JSON's avatar
DCloud_JSON 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
		// border-radius: 10px;
		font-size: 16px;
		align-items: flex-start;
		justify-content: center;
		overflow:hidden;
	}
	.sound-text{
		position: relative;
		left: -20rpx;
		width: 450rpx;
		font-size: 14px;
		text-align: center;
	}
	.sound-tip{
		position: fixed;
		left: 0;
		bottom: 110px;
192
    z-index: 9;
DCloud_JSON's avatar
DCloud_JSON 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
		width: 750rpx;
		text-align: center;
		justify-content: center;
		align-items: center;
	}
	.sound-tip-text{
		margin-bottom: 10px;
		color: #999999;
		font-size: 14px;
	}
	.closeIcon{
		width: 30px;
		height: 30px;
		background-color: #DDDDDD;
		border-radius: 100px;
		justify-content: center;
		align-items: center;
	}
211 212 213 214 215 216 217
  /* #ifdef MP-WEIXIN */
  // 纠正微信小程序的icon位置
  .closeIcon .icon{
    position: relative;
    top: -2px;
  }
  /* #endif */
DCloud_JSON's avatar
DCloud_JSON 已提交
218 219 220 221 222 223 224 225 226 227 228 229
	.sound-progress {
		// border-radius: 10px;
		height: 44px;
		position: absolute;
		left: 0;
		top: 0;
		padding: 0;
		transition: width 0.2s linear;
		background-color: #2faf4c;
		opacity: 0.3;
	}
	
230
	.recordState{
DCloud_JSON's avatar
DCloud_JSON 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244
		background-color: #efefef;
	}
	
	.mark{
		width: 750rpx;
		position: fixed;
		top: 0;
		left: 0;
		bottom: 57px;
		right: 0;
		background:rgba(0,0,0,0.7);
		flex: 1;
	}
</style>