微信小步伐mp3音频播放组件,仅需传入url即可

打印 上一主题 下一主题

主题 970|帖子 970|积分 2910

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

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

x
  1. // index.js
  2. // packageChat/components/audio-player/index.js
  3. Component({
  4.   /**
  5.    * 组件的属性列表
  6.    */
  7.   properties: {
  8.     /**
  9.      * MP3 文件的 URL
  10.      */
  11.     src: {
  12.       type: String,
  13.       value: '',
  14.       observer(newVal, oldVal) {
  15.         if (newVal !== oldVal && newVal) {
  16.           // 如果 InnerAudioContext 已存在,先停止并销毁它以避免多个实例
  17.           if (this.innerAudioContext) {
  18.             this.innerAudioContext.stop()
  19.             this.innerAudioContext.destroy()
  20.             this.innerAudioContext = null
  21.           }
  22.           // 初始化音频并获取时长
  23.           this.initializeAudio()
  24.         }
  25.       }
  26.     }
  27.   },
  28.   /**
  29.    * 组件的初始数据
  30.    */
  31.   data: {
  32.     playStatus: 0, // 0: 未播放, 1: 正在播放
  33.     duration: 0, // 音频总时长(秒)
  34.     remain: 0 // 剩余播放时间(秒)
  35.   },
  36.   /**
  37.    * 组件生命周期
  38.    */
  39.   lifetimes: {
  40.     attached() {
  41.       if (this.data.src) {
  42.         this.initializeAudio()
  43.       }
  44.     },
  45.     detached() {
  46.       // 组件卸载时,停止并销毁 InnerAudioContext
  47.       if (this.innerAudioContext) {
  48.         this.innerAudioContext.stop()
  49.         this.innerAudioContext.destroy()
  50.         this.innerAudioContext = null
  51.       }
  52.     }
  53.   },
  54.   /**
  55.    * 组件的方法列表
  56.    */
  57.   methods: {
  58.     /**
  59.      * 播放按钮点击事件
  60.      */
  61.     playBtn() {
  62.       const { src, playStatus } = this.data
  63.       if (!src) {
  64.         wx.showToast({
  65.           title: '没有录音文件',
  66.           icon: 'none'
  67.         })
  68.         return
  69.       }
  70.       // 如果 InnerAudioContext 尚未创建,初始化它
  71.       if (!this.innerAudioContext) {
  72.         this.initializeAudio()
  73.       }
  74.       // 如果当前正在播放,停止播放并重置状态
  75.       if (playStatus === 1) {
  76.         this.innerAudioContext.stop()
  77.         this.setData({
  78.           playStatus: 0,
  79.           remain: this.data.duration
  80.         })
  81.         return
  82.       }
  83.       // 每次点击都重新开始播放
  84.       this.innerAudioContext.src = src
  85.       this.innerAudioContext.play()
  86.       // 更新播放状态
  87.       this.setData({
  88.         playStatus: 1,
  89.         remain: this.data.duration
  90.       })
  91.     },
  92.     /**
  93.      * 初始化 InnerAudioContext 并绑定事件
  94.      */
  95.     initializeAudio() {
  96.       this.innerAudioContext = wx.createInnerAudioContext()
  97.       this.innerAudioContext.obeyMuteSwitch = false
  98.       this.innerAudioContext.src = this.data.src
  99.       // 监听音频能够播放时触发
  100.       this.innerAudioContext.onCanplay(() => {
  101.         console.log('音频可以播放')
  102.         const totalDuration = Math.floor(this.innerAudioContext.duration)
  103.         if (totalDuration > 0) {
  104.           this.setData({
  105.             duration: totalDuration,
  106.             remain: totalDuration
  107.           })
  108.         } else {
  109.           console.warn('无法获取音频时长')
  110.         }
  111.       })
  112.       // 监听播放开始
  113.       this.innerAudioContext.onPlay(() => {
  114.         console.log('音频开始播放')
  115.       })
  116.       // 监听播放结束
  117.       this.innerAudioContext.onEnded(() => {
  118.         console.log('音频播放结束')
  119.         this.setData({
  120.           playStatus: 0,
  121.           remain: this.data.duration
  122.         })
  123.         // 触发自定义事件(如果需要)
  124.         this.triggerEvent('playComplete')
  125.       })
  126.       // 监听播放错误
  127.       this.innerAudioContext.onError(err => {
  128.         console.error('播放错误:', err)
  129.         wx.showToast({
  130.           title: '播放失败,请重试',
  131.           icon: 'none'
  132.         })
  133.         this.setData({
  134.           playStatus: 0,
  135.           remain: this.data.duration
  136.         })
  137.       })
  138.       // 监听播放进度更新
  139.       this.innerAudioContext.onTimeUpdate(() => {
  140.         const current = Math.floor(this.innerAudioContext.currentTime)
  141.         const remain = Math.floor(this.innerAudioContext.duration) - current
  142.         this.setData({
  143.           remain: remain > 0 ? remain : 0
  144.         })
  145.       })
  146.     }
  147.   }
  148. })
复制代码
  1. <view class="voice-msg" bindtap="playBtn">
  2.   <image
  3.     src="{{ playStatus === 0 ? '/sendingaudio.png' : '/voice.gif' }}"
  4.     mode="aspectFill"
  5.     class="voice-icon"
  6.   />
  7.   <text class="voice-msg-text"> {{ playStatus === 1 ? (remain + "''") : (duration + "''") }} </text>
  8. </view>
复制代码
  1. /* packageChat/components/audio-player/index.wxss */
  2. .voice-msg {
  3.   display: flex;
  4.   align-items: center;
  5.   min-width: 200rpx;
  6.   padding: 0 20rpx;
  7.   height: 60rpx;
  8.   background-color: rgba(149, 236, 105, 0.72);
  9.   border-radius: 10rpx;
  10.   box-shadow:0 3rpx 6rpx rgba(0, 0, 0, 0.13);
  11.   .voice-icon {
  12.     transform: rotate(180deg);
  13.     width: 22rpx;
  14.     height: 32rpx;
  15.   }
  16.   .voice-msg-text {
  17.     margin-left: 10rpx;
  18.     color:#000000 !important;
  19.     font-size:30rpx !important;
  20.   }
  21. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

曹旭辉

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表