浏览代码

语音播放bug

jwyu 2 年之前
父节点
当前提交
82560cd19b
共有 2 个文件被更改,包括 36 次插入43 次删除
  1. 12 14
      pages-voice/addVoice.vue
  2. 24 29
      pages/voice/voice.vue

+ 12 - 14
pages-voice/addVoice.vue

@@ -286,17 +286,15 @@ export default {
 
 
         //点击播放\暂停音频
         //点击播放\暂停音频
         handlePlayAudio(){
         handlePlayAudio(){
-            //没有初始化时
-            if(!innerAudioContext.src||innerAudioContext.src!=this.temAudio.url){
+            if(innerAudioContext.url==this.temAudio.url){
+                if(innerAudioContext.paused){
+                    innerAudioContext.play()
+                }else{
+                    innerAudioContext.pause()
+                }
+            }else{
                 innerAudioContext.src=this.temAudio.url 
                 innerAudioContext.src=this.temAudio.url 
                 innerAudioContext.play()
                 innerAudioContext.play()
-                return
-            }
-
-            if(innerAudioContext.paused){
-                innerAudioContext.play()
-            }else{
-                innerAudioContext.pause()
             }
             }
         },
         },
 
 
@@ -310,11 +308,11 @@ export default {
                 console.log('录音播放暂停');
                 console.log('录音播放暂停');
                 this.temAudio.paused=true
                 this.temAudio.paused=true
             })
             })
-            innerAudioContext.onStop(()=>{
-                console.log('录音播放停止');
-                this.temAudio.paused=true
-                innerAudioContext.src=''
-            })
+            // innerAudioContext.onStop(()=>{
+            //     console.log('录音播放停止');
+            //     this.temAudio.paused=true
+            //     innerAudioContext.src=''
+            // })
             innerAudioContext.onEnded(()=>{
             innerAudioContext.onEnded(()=>{
                 console.log('录音播放自然结束');
                 console.log('录音播放自然结束');
                 this.temAudio.curTime=this.temAudio.duration
                 this.temAudio.curTime=this.temAudio.duration

+ 24 - 29
pages/voice/voice.vue

@@ -74,7 +74,7 @@ import {apiVoiceList,apiVoiceSectionList,apiVoicePlayRecord,apiVoiceDel} from '@
 import {apiGetSceneToParams} from '@/api/common'
 import {apiGetSceneToParams} from '@/api/common'
 import noAuth from './components/noAuth.vue'
 import noAuth from './components/noAuth.vue'
 const moment=require('@/utils/moment-with-locales.min')
 const moment=require('@/utils/moment-with-locales.min')
-// let innerAudioContext = uni.createInnerAudioContext();//播放音频实例
+let innerAudioContext = uni.createInnerAudioContext();//播放音频实例
 export default {
 export default {
     components:{
     components:{
         noAuth
         noAuth
@@ -91,7 +91,7 @@ export default {
     },
     },
     data() {
     data() {
         return {
         return {
-            innerAudioContext:uni.createInnerAudioContext(),//播放音频实例
+            // innerAudioContext:null,//播放音频实例
             list:[],
             list:[],
             page:1,
             page:1,
             pageSize:20,
             pageSize:20,
@@ -120,7 +120,6 @@ export default {
     onLoad(options){
     onLoad(options){
         this.init(options)
         this.init(options)
         this.getOptionsList()
         this.getOptionsList()
-        this.listenAudio()
         this.addListenVoiceSuccess()
         this.addListenVoiceSuccess()
     },
     },
     onShow(){
     onShow(){
@@ -128,6 +127,8 @@ export default {
         if(!this.isAuth){
         if(!this.isAuth){
             this.getVoiceList()
             this.getVoiceList()
         }
         }
+        // this.innerAudioContext=uni.createInnerAudioContext()
+        this.listenAudio()
     },
     },
     onUnload(){
     onUnload(){
 		uni.$off('addVoiceSuccess')
 		uni.$off('addVoiceSuccess')
@@ -293,30 +294,27 @@ export default {
         },
         },
 
 
         handlePlay(item){
         handlePlay(item){
-            const innerAudioContext=this.innerAudioContext
-            //没有初始化时
-            if(this.temAudio.id==0||this.temAudio.id!=item.BroadcastId){
-                this.listenAudio()
+            if(this.temAudio.id==item.BroadcastId){
+                if(innerAudioContext.paused){
+                    innerAudioContext.play()
+                }else{
+                    innerAudioContext.pause()
+                }
+            }else{
+                if(!innerAudioContext.paused){
+                    innerAudioContext.stop()
+                }
                 this.temAudio.id=item.BroadcastId
                 this.temAudio.id=item.BroadcastId
                 this.temAudio.duration=item.VoicePlaySeconds
                 this.temAudio.duration=item.VoicePlaySeconds
                 this.temAudio.curTime=item.VoicePlaySeconds
                 this.temAudio.curTime=item.VoicePlaySeconds
                 innerAudioContext.src=item.VoiceUrl 
                 innerAudioContext.src=item.VoiceUrl 
                 innerAudioContext.play()
                 innerAudioContext.play()
-                this.temAudio.paused=false
                 this.handleVoicePlayRecord(item)
                 this.handleVoicePlayRecord(item)
-                return
-            }
-
-            if(innerAudioContext.paused){
-                innerAudioContext.play()
-            }else{
-                innerAudioContext.pause()
             }
             }
         },
         },
 
 
         //音频播放事件
         //音频播放事件
         listenAudio(){
         listenAudio(){
-            const innerAudioContext=this.innerAudioContext
             innerAudioContext.onPlay(()=>{
             innerAudioContext.onPlay(()=>{
                 console.log('开始播放录音');
                 console.log('开始播放录音');
                 this.temAudio.paused=false
                 this.temAudio.paused=false
@@ -325,21 +323,18 @@ export default {
                 console.log('录音播放暂停');
                 console.log('录音播放暂停');
                 this.temAudio.paused=true
                 this.temAudio.paused=true
             })
             })
-            innerAudioContext.onStop(()=>{
-                console.log('录音播放停止');
-                this.temAudio.paused=true
-                setTimeout(() => {
-                    this.temAudio.id=0
-                }, 100);
-                innerAudioContext.src=''
-            })
+            // innerAudioContext.onStop(()=>{
+            //     console.log('录音播放停止');
+            //     this.temAudio.paused=true
+            //     // setTimeout(() => {
+            //         this.temAudio.id=0
+            //     // }, 100);
+            // })
             innerAudioContext.onEnded(()=>{
             innerAudioContext.onEnded(()=>{
                 console.log('录音播放自然结束');
                 console.log('录音播放自然结束');
-                setTimeout(() => {
-                    this.temAudio.paused=true
-                    this.temAudio.id=0
-                    innerAudioContext.src=''
-                }, 100);
+                this.temAudio.paused=true
+                this.temAudio.id=0
+
             })
             })
             innerAudioContext.onTimeUpdate(()=>{
             innerAudioContext.onTimeUpdate(()=>{
                 // console.log('时间更新');
                 // console.log('时间更新');