记录微信小程序音频及视频播放。
1.audio
<!-- audio.wxml --> <audio poster="{{poster}}" name="{{name}}" author="{{author}}" src="{{src}}" id="myAudio" controls loop></audio> <button type="primary" bindtap="audioPlay">播放</button> <button type="primary" bindtap="audioPause">暂停</button> <button type="primary" bindtap="audio14">设置当前播放时间为14秒</button> <button type="primary" bindtap="audioStart">回到开头</button>
// audio.js Page({ onReady: function (e) { // 使用 wx.createAudioContext 获取 audio 上下文 context this.audioCtx = wx.createAudioContext(‘myAudio‘) }, data: { poster: ‘http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000‘, name: ‘此时此刻‘, author: ‘许巍‘, src: ‘http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46‘, },//播放 audioPlay: function () { this.audioCtx.play() },//暂停 audioPause: function () { this.audioCtx.pause() },//设置当前播放时间在14秒 audio14: function () { this.audioCtx.seek(14) },//回到开头 audioStart: function () { this.audioCtx.seek(0) } })
2.video
默认宽度300px、高度225px
<view class="section tc"> <video id="myVideo" src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400" danmu-list="{{danmuList}}" enable-danmu danmu-btn controls></video> <view class="btn-area"> <input bindblur="bindInputBlur" placeholder="输入文字"/> <button bindtap="bindSendDanmu">发送弹幕</button> </view> </view>
function getRandomColor () { let rgb = [] for (let i = 0 ; i < 3; ++i){ let color = Math.floor(Math.random() * 256).toString(16)// 0.0 ~ 1.0 之间的一个伪随机数。Math.random()。floor() 方法执行的是向下取整计算,它返回的是小于或等于函数参数,并且与之最接近的整数。toString() 方法可把一个逻辑值转换为字符串,并返回结果。 color = color.length == 1 ? ‘0‘ + color : color rgb.push(color) }// join() 方法用于把数组中的所有元素放入一个字符串。 return ‘#‘ + rgb.join(‘‘) } Page({ onReady: function (res) { this.videoContext = wx.createVideoContext(‘myVideo‘) }, inputValue: ‘‘, data: { src: ‘‘, danmuList: [ { text: ‘第 1s 出现的弹幕‘, color: ‘#ff0000‘, time: 1 }, { text: ‘第 3s 出现的弹幕‘, color: ‘#ff00ff‘, time: 3 }] }, bindInputBlur: function(e) { this.inputValue = e.detail.value },//发送弹幕文字 bindSendDanmu: function () { this.videoContext.sendDanmu({ text: this.inputValue, color: getRandomColor() }) } })
原文地址:https://www.cnblogs.com/juewuzhe/p/8205579.html
时间: 2024-10-10 08:48:23