123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <template>
- <view class="task-form-item form-item-media">
- <view class="form-lable">
- <text><text class="form-must" v-if="rule && rule.required">* </text>录音:</text>
- <view :class="['form-lable-content' , soundRecording.length > 0 ? 'lable-content-right' : '']"
- v-if="!soundRecording || soundRecording.length < num">
- <nb-voice-record v-if="!checkPage" @startRecord="start" @endRecord="end" @cancelRecord="cancel">
- <view class="lable-content-btn" @click.stop="onRecording()">
- <text class="iconfont_yige"></text>
- <text>长按录音</text>
- </view>
- </nb-voice-record>
- </view>
- <text class="form-media-num">({{soundRecording.length}}/{{num}})</text>
- </view>
- <view class="form-img-video">
- <view class="audio-item" :key="index" v-for="(item,index) in soundRecording"
- @click.stop="onPlayAudio(item , index)">
- <view :class="['audio-item-val' , play_index === index ? 'play-audio' : '']"><text
- class="iconfont_yige"></text>
- <text class="audio-time">{{item.duration}}</text> <text
- class="iconfont_yige"></text>
- </view>
- <view class="del-img-video iconfont_yige" @click.stop="delAudio(index)" v-if="!checkPage">
- </view>
- </view>
- </view>
- </view>
- </template>
- <script>
- import {
- SingleFileUpload
- } from "@/utils/tool.js"
- let innerAudioContext = uni.createInnerAudioContext();
- export default {
- name: 'submitAudio',
- model: {
- prop: ['soundRecording'],
- event: ['onChange']
- },
- props: {
- checkPage: {
- type: Boolean,
- default: false
- },
- num: {
- type: Number,
- default: 3
- },
- rule: {
- type: Object,
- default: null
- },
- soundRecording: {
- type: Array,
- default: () => [],
- require: true
- },
- },
- data() {
- return {
- play_index: -1,
- audio_s: 0,
- audioInterval: null,
- }
- },
- methods: {
- start() {
- // 开始录音
- try {
- clearInterval(this.audioInterval)
- } catch (e) {}
- this.audioInterval = null;
- this.audio_s = 0;
- this.audioInterval = setInterval(() => {
- this.audio_s += 1;
- }, 1000)
- },
- end(event) {
- try {
- clearInterval(this.audioInterval)
- } catch (e) {}
- this.audioInterval = null;
- const file_ = event ? event.tempFilePath : ''
- SingleFileUpload(file_, true).then(res => {
- this.soundRecording.push({
- filePath: res.url,
- duration: this.audio_s
- })
- })
- // 结束录音并处理得到的录音文件
- // event中,app端仅有tempFilePath字段,微信小程序还有duration和fileSize两个字段
- },
- cancel() {
- // 用户取消录音
- },
- delAudio(index) {
- if (this.play_index === index) {
- // 当前存在播放,先关闭播放
- try {
- innerAudioContext.pause();
- innerAudioContext.destroy()
- innerAudioContext = null
- } catch (e) {
- //TODO handle the exception
- }
- }
- this.soundRecording.splice(index, 1);
- },
- onPlayAudio(item, index) {
- console.log('item ==1111= ', innerAudioContext)
- if (innerAudioContext) {
- try {
- innerAudioContext.pause();
- innerAudioContext.destroy()
- innerAudioContext = null
- } catch (e) {
- //TODO handle the exception
- }
- }
- // 点击同一个播放的时候,阻止继续播放
- if (this.play_index === index) {
- this.play_index = -1;
- return
- }
- if (item) {
- try {
- innerAudioContext = uni.createInnerAudioContext();
- innerAudioContext.src = item.filePath;
- innerAudioContext.play();
- innerAudioContext.onPlay(() => {
- this.play_index = index;
- console.log('开始播放');
- });
- innerAudioContext.onError((res) => {
- this.play_index = -1;
- innerAudioContext.destroy()
- innerAudioContext = null
- // uni.showToast({
- // title: '播放失败',
- // icon: 'none'
- // })
- });
- innerAudioContext.onEnded((res) => {
- this.play_index = -1;
- innerAudioContext.destroy()
- innerAudioContext = null
- // uni.showToast({
- // title: '播放结束事件',
- // icon: 'none'
- // })
- });
- innerAudioContext.onPause((res) => {
- this.play_index = -1;
- innerAudioContext.destroy()
- innerAudioContext = null
- // uni.showToast({
- // title: '播放暂停事件',
- // icon: 'none'
- // })
- });
- } catch (e) {
- //TODO handle the exception
- console.log('play = Audio = ', e)
- }
- }
- },
- onRecording() {
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- @import '~./submint.scss';
- .form-img-video {
- flex-direction: column !important;
- }
- .audio-item {
- width: 100%;
- display: flex;
- flex-direction: row;
- justify-content: space-between;
- align-items: center;
- margin-bottom: 20rpx;
- &:last-child {
- margin-bottom: 0rpx;
- }
- .audio-item-val {
- flex: 1;
- margin-right: 20rpx;
- height: 70rpx;
- background-color: #E6E6E6;
- border-radius: 14rpx;
- display: flex;
- align-items: center;
- padding: 0 20rpx;
- .audio-time {
- padding-left: 20rpx;
- }
- .iconfont_yige {
- font-size: 40rpx;
- }
- }
- }
- .play-audio {
- overflow: hidden;
- /* 超出部分隐藏起来 */
- animation: changecolor 1.5s linear infinite;
- /* 定义动画效果 */
- }
- @keyframes changecolor {
- 0% {
- background-color: rgba(60, 183, 210, 0.4);
- }
- 35% {
- background-color: rgba(60, 183, 210, 0.6);
- }
- 75% {
- background-color: rgba(60, 183, 210, 0.8);
- }
- 100% {
- background-color: rgba(60, 183, 210, 1);
- }
- }
- </style>
|