用户
 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
  • TA的每日心情
    慵懒
    2017-1-6 10:46
  • 签到天数: 2 天

    [LV.1]初来乍到

    173

    主题

    181

    帖子

    981

    金钱

    咫尺尊享

    Rank: 5Rank: 5

    积分
    0
    2017-3-3 10:49:18 柿霖不是林 咫尺尊享 楼主 62169
    经过测试,本demo真机可正常使用录音,并播放录音的功能,推荐新手学习

    作者:Cedric-song





    [HTML] 纯文本查看 复制代码
    // page/recorder/index.js
    var util = require('../../util/util.js')
    const PLAY = "播放音频"
    const REPEAT = "重播音频"
    const PAUSE = "暂停播放"
     
    const PLAY_RECORD = "开始录音"
    const STOP_RECORD = "停止录音"
    const PLAY_VOICE = "回放录音"
    const STOP_VOICE = "停止回放"
    const SAVE_VOICE = "保存录音"
     
    var playTimeInterval
    var recordTimeInterval
     
    Page({
      onReady: function(e) {
        // 使用 wx.createAudioContext 获取 audio 上下文 context
        this.audioCtx = wx.createAudioContext('myAudio')
      },
      data: {
        recording: false,
        playing: false,
        hasRecord: false,
        recordTime: 0,
        playTime: 0,
        formatedRecordTime: '00:00:00',
        formatedPlayTime: '00:00:00',
        play: PLAY,
        repeat: REPEAT,
        record: PLAY_RECORD,
        playRecord: PLAY_VOICE,
        stopRecord: STOP_RECORD,
        playVoice: PLAY_VOICE,
        stopVoice: STOP_VOICE,
        saveVoice: SAVE_VOICE,
        audioInfo: {
          poster: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',
          name: '此时此刻',
          author: '许巍',
          controls: false,
          src: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46'
        },
     
      },
      audioPause: function() {
        this.audioCtx.pause()
      },
      audioToggle: function(btnName, defaultName) {
        let otherBtnName = btnName === "play" ? "repeat" : "play"
        if (this.data[btnName] === defaultName) {
          this.audioCtx.play()
          this.setData({
            [btnName]: PAUSE,
            [otherBtnName]: otherBtnName.toUpperCase() === "PLAY" ? PLAY : REPEAT
          });
          return false
        }
        this.setData({
          [btnName]: defaultName
        });
        this.audioPause()
      },
      audioPlay: function() {
        this.audioToggle('play', PLAY)
      },
      audioRepeat: function() {
        this.audioCtx.seek(0)
        this.audioToggle('repeat', REPEAT)
      },
      defaultPause: function() {
        this.setData({
          play: PLAY,
          repeat: REPEAT
        })
      },
      onHide: function() {
        if (this.data.playing) {
          this.stopVoice()
        } else if (this.data.recording) {
          this.stopRecordUnexpectedly()
        }
      },
      startRecord: function() {
        this.setData({
          recording: true
        })
     
        var that = this
        recordTimeInterval = setInterval(function() {
          var recordTime = that.data.recordTime += 1
          that.setData({
            formatedRecordTime: util.formatTime(that.data.recordTime),
            recordTime: recordTime
          })
        }, 1000)
        wx.startRecord({
          success: function(res) {
            that.setData({
              hasRecord: true,
              tempFilePath: res.tempFilePath,
              formatedPlayTime: util.formatTime(that.data.playTime)
            })
          },
          complete: function() {
            that.setData({
              recording: false
            })
            clearInterval(recordTimeInterval)
          }
        })
      },
      stopRecord: function() {
        wx.stopRecord()
      },
      stopRecordUnexpectedly: function() {
        var that = this
        wx.stopRecord({
          success: function() {
            console.log('stop record success')
            clearInterval(recordTimeInterval)
            that.setData({
              recording: false,
              hasRecord: false,
              recordTime: 0,
              formatedRecordTime: util.formatTime(0)
            })
          }
        })
      },
      playVoice: function() {
        var that = this
        playTimeInterval = setInterval(function() {
          var playTime = that.data.playTime + 1
          console.log('update playTime', playTime)
          that.setData({
            playing: true,
            formatedPlayTime: util.formatTime(playTime),
            playTime: playTime
          })
        }, 1000)
        wx.playVoice({
          filePath: this.data.tempFilePath,
          success: function() {
            clearInterval(playTimeInterval)
            var playTime = 0
            console.log('play voice finished')
            that.setData({
              playing: false,
              formatedPlayTime: util.formatTime(playTime),
              playTime: playTime
            })
          }
        })
      },
      pauseVoice: function() {
        clearInterval(playTimeInterval)
        wx.pauseVoice()
        this.setData({
          playing: false
        })
      },
      stopVoice: function() {
        clearInterval(playTimeInterval)
        this.setData({
          playing: false,
          formatedPlayTime: util.formatTime(0),
          playTime: 0
        })
        wx.stopVoice()
      },
      saveVoice: function() {
        wx.saveFile({
          success: function(res) {
            var tempFilePath = res.tempFilePath
            wx.saveFile({
              tempFilePath: tempFilePath,
              success: function(res) {
                var savedFilePath = res.savedFilePath
              }
            })
          }
        })
      },
      clear: function() {
        clearInterval(playTimeInterval)
        wx.stopVoice()
        this.setData({
          playing: false,
          hasRecord: false,
          tempFilePath: '',
          formatedRecordTime: util.formatTime(0),
          recordTime: 0,
          playTime: 0
        })
      }
    })





    游客,如果您要查看本帖隐藏内容请回复

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    使用道具 举报 回复
    谢谢分享,我会好好学习哦
    我哦 偶就曾经啊你想看就参考垃圾来参加
    xuexi ingxuexi ingxuexi ing
    感谢分享
    使用道具 举报 回复
    谢谢分享,我会好好学习哦
    我有流量,你有产品,我们就可以合作,有意的个人开发者朋友可以联系微,信,号j i m 2 0 1 8 0 6 8 8,详聊
    发新帖
    您需要登录后才可以回帖 登录 | 立即注册
    咫尺论坛|小程序开发社区
    X

    扫码添加专属客服即可随时咨询

    还可领取小程序推广攻略