找回密码
 立即注册

扫一扫,访问微社区

查看: 5963|回复: 6

微信小程序组件学习demo:录音机,RECORD组件真机可用

  [复制链接]
发表于 2017-3-3 10:49:18 | 显示全部楼层 |阅读模式
经过测试,本demo真机可正常使用录音,并播放录音的功能,推荐新手学习

作者:Cedric-song





[mw_shl_code=html,true]// 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
    })
  }
})[/mw_shl_code]




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

本帖子中包含更多资源

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

×

0

主题

3

回帖

39

金钱

新人求带

积分
0
发表于 2018-10-12 12:10:18 | 显示全部楼层
谢谢分享,我会好好学习哦

0

主题

7

回帖

18

金钱

新人求带

积分
0
发表于 2018-11-14 20:31:36 | 显示全部楼层
我哦 偶就曾经啊你想看就参考垃圾来参加

0

主题

3

回帖

22

金钱

新人求带

积分
0
发表于 2018-12-12 11:11:00 | 显示全部楼层
xuexi ingxuexi ingxuexi ing

0

主题

11

回帖

32

金钱

新人求带

积分
0
发表于 2019-3-20 18:36:39 | 显示全部楼层
感谢分享
回复

使用道具 举报

0

主题

5

回帖

48

金钱

新人求带

积分
0
发表于 2019-3-25 10:56:06 | 显示全部楼层
谢谢分享,我会好好学习哦

0

主题

408

回帖

600

金钱

新人求带

积分
0
发表于 2020-4-4 16:48:43 | 显示全部楼层
我有流量,你有产品,我们就可以合作,有意的个人开发者朋友可以联系微,信,号j i m 2 0 1 8 0 6 8 8,详聊
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|微信小程序开发|教程|文档|资源汇总_即速论坛 ( 粤ICP备14097199号-1  

GMT+8, 2024-4-26 05:12 , Processed in 0.130038 second(s), 31 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表