Pārlūkot izejas kodu

Signed-off-by: yukangzhe <382985154@qq.com>

yukangzhe 3 gadi atpakaļ
vecāks
revīzija
4a9910c4bc

+ 0 - 1
pages/doTask/doTask.js

@@ -132,7 +132,6 @@ Page({
         })
       }
     } 
-    
   },
 
   delAudio:function(e){

+ 28 - 23
pages/taskDetail/taskDetail.js

@@ -2,6 +2,9 @@
 const app = getApp()
 const util = require("../../utils/util")
 const urlDef = require("../../utils/urls")
+
+// const recordManager = wx.getRecorderManager() //录音对象
+const audioContext = wx.createInnerAudioContext() //音频播放对象
 Page({
 
   /**
@@ -15,7 +18,7 @@ Page({
     videoContext: '',
     curAudio: '',
     curAudioIndex: -1,
-    audioPause: 0,
+    audioPause: false,
     doTaskFlag: false,
     entity: null,
     teacherFace: '/images/head.png',
@@ -72,31 +75,33 @@ Page({
       urls: list
     })
   },
-  audioShow: function (e) {
-    if (this.data.audioPause == 0 && this.data.curAudioIndex != e.currentTarget.dataset.index) {//播放语音,切换
-      this.setData({
-        curAudio: ""
-      })
-      this.setData({
-        curAudioIndex: e.currentTarget.dataset.index,
-        curAudio: e.currentTarget.dataset.url,
 
-      })
-      this.audioContext.play()
+  showAudio:function(e){
+    const index = e.currentTarget.dataset.index
+    if(this.data.curAudioIndex != index){
       this.setData({
-        audioPause: 0
+        curAudioIndex: index,
       })
-    } else if (this.data.audioPause == 1 && this.data.curAudioIndex == e.currentTarget.dataset.index) {//暂停后恢复播放
-      this.audioContext.play()
-      this.setData({
-        audioPause: 0
-      })
-    } else if (this.data.audioPause == 0 && this.data.curAudioIndex == e.currentTarget.dataset.index) {//暂停播放
-      this.audioContext.pause()
-      this.setData({
-        audioPause: 1
+      audioContext.src = this.data.audioList[index]
+      audioContext.play()
+      audioContext.onEnded((res)=>{
+        this.setData({
+          curAudioIndex: '-1',
+        })
       })
-    }
+    }else{
+      if(this.data.audioPause){
+        audioContext.play()
+        this.setData({
+          audioPause: false
+        })
+      } else {
+        audioContext.pause()
+        this.setData({
+          audioPause: true
+        })
+      }
+    } 
   },
 
   bindended: function () {
@@ -119,7 +124,7 @@ Page({
   onLoad: function (options) {
     let id = options.id;
     this.setData({ homeworkId: id });
-    this.audioContext = wx.createAudioContext('audio-play')
+    // this.audioContext = wx.createAudioContext('audio-play')
     this.videoContext = wx.createVideoContext('play-video');// 	创建 video 上下文 VideoContext 对象。
     const type = options.type
     if (type == 0) {

+ 1 - 1
pages/taskDetail/taskDetail.wxml

@@ -97,7 +97,7 @@
 
     <view class="title">语音</view>
     <view class="audio-list">
-      <image bindtap="audioShow" src="{{index == curAudioIndex ? '/images/cur-audio.png' : '/images/audio.png'}}" class="{{index == curAudioIndex ? 'curAudioStyle' : ''}}" wx:for="{{entity.mark.records}}"  wx:key="index" data-index="{{index}}" data-url="{{item.url}}"></image>
+      <image bindtap="showAudio" src="{{index == curAudioIndex ? '/images/cur-audio.png' : '/images/audio.png'}}" class="{{index == curAudioIndex ? 'curAudioStyle' : ''}}" wx:for="{{entity.mark.records}}"  wx:key="index" data-index="{{index}}" data-url="{{item.url}}"></image>
     </view>
     <audio id="audio-play" bindpause="bindpause" bindended="bindended" src="{{curAudio}}"></audio>