常用的属性:
控制audio组件:通过wx.createAudioContext获取界面中的audio组件
wx.createAudioContext(audioId)
创建audio子目录
<!--pages/audio/audio.wxml-->
<view class="page"><view class="page_hd"><text class="page_title">audio音频</text></view><view class="page_bd"><view class="section section_gap" style="text-align: center;"><audio id="audio1" src="{{current.src}}" poster="{{current.poster}}"name="{{current.name}}" author="{{current.author}}"action="{{audioAction}}" controls ></audio></view><button type="primary" bindtap="audioPlay">播放</button><button type="primary" bindtap="audioPause">暂停</button><button type="primary" bindtap="audio20">播放时间为20秒</button><button type="primary" bindtap="audioStart">回到开头</button></view>
</view>
onReady: function () {//获取audio上下文contextthis.adContr = wx.createAudioContext('audio1');},//播放audioPlay: function() {this.adContr.play()},//暂停audioPause: function() {this.adContr.pause()},//定位audio20: function() {this.adContr.seek(20)},//起始位置audioStart: function() {this.adContr.seek(0)},
1、播放背景音乐wx.playBackgroundAudio:只能有一首处于播放状态
2、暂停播放音乐wx.pauseBackgroundAudio不需要参数
3、停止播放音乐wx.stopBackgroundAudio不需要参数
4、获取音乐播放状态wx.getBackgroundAudioPlayerState
success:回调函数,传入Object参数
5、音乐监听器:
创建audioapi子目录
tapPlay: function() {wx.playBackgroundAudio({dataUrl: '.mp3',title: '青云志主题曲《浮诛》',coverImgUrl: ''})},tapPause: function() {wx.pauseBackgroundAudio();},tapSeek: function() {wx.seekBackgroundAudio({position: 30})},tapStop: function() {wx.stopBackgroundAudio();},tapGetPlayState: function() {wx.getBackgroundAudioPlayerState({success: function(res) {console.log(res)}})},onLoad: function (options) {wx.onBackgroundAudioPlay(function() {console.log('监听音乐播放,开始播放音乐')}) wx.onBackgroundAudioPause(function() {console.log("监听音乐暂停")})wx.onBackgroundAudioStop(function() {console.log('监听音乐停止')})},
<!--pages/audioapi/audioapi.wxml-->
<view class="page"><view class="page_hd"><text class="page_title">audio音频</text></view><view class="page_bd"><view class="btn-area"><button type="primary" bindtap="tapPlay">播放</button><button type="primary" bindtap="tapPause">暂停</button><button type="primary" bindtap="tapSeek">设置播放进度</button><button type="primary" bindtap="tapStop">停止播放</button><button type="primary" bindtap="tapGetPlayState">获取播放状态</button></view></view>
</view>
video组件常用的属性:
获取视频上下文ateVideoContext,主要的方法有play, pause, seek, sendDanmu(发送一个弹幕到屏幕上)
给视频添加弹幕:
// pages/video/video.js
//生成随机颜色
function getRandomColor() {var colorStr = Math.floor(Math.random()* 0xFFFFFF).toString(16);//返回格式化的颜色字符串return "#"+"000000".substring(0, 6-colorStr) + colorStr;
}
Page({inputValue: '', //输入的弹幕值/*** 页面的初始数据*/data: {src: '.mp4',danmuList: [{text: '第2s出现的弹幕',color: '#ff0000',time: 2},{text: '第5s出现的弹幕',color: '#ff00ff',time: 5},],},videoErrorCallback: function(e) {console.log(Msg)},/*** 生命周期函数--监听页面加载*/onLoad: function (options) {},/*** 生命周期函数--监听页面初次渲染完成*/onReady: function () {this.videoContext = wx.createVideoContext('myVideo')},bindInputBlur: function(e) {this.inputValue = e.detail.value},bindSendDanmu: function() {this.videoContext.sendDanmu({text: this.inputValue,color: getRandomColor()})},videoErrorCallback: function(e) {console.log(Msg)},/*** 生命周期函数--监听页面显示*/onShow: function () {},/*** 生命周期函数--监听页面隐藏*/onHide: function () {},/*** 生命周期函数--监听页面卸载*/onUnload: function () {},/*** 页面相关事件处理函数--监听用户下拉动作*/onPullDownRefresh: function () {},/*** 页面上拉触底事件的处理函数*/onReachBottom: function () {},/*** 用户点击右上角分享*/onShareAppMessage: function () {}
})
<!--pages/video/video.wxml-->
<view class="page"><view class="page_hd"><text class="page_title">video视频</text></view><view class="page_bd"><view class="section section_gap" style="text-align: center;"><video id="myVideo" src="{{src}}" binderror="videoErrorCallback"danmu-list="{{danmuList}}" enable-danmu='true' danmu-btn controls ></video></view><view class="btn-area"><input placeholder="输入弹幕内容" bindblur="bindInputBlur"/><button bindtap="bindSendDanmu">发送弹幕</button></view></view>
</view>
本文发布于:2024-01-28 09:57:45,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064070716608.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |