微信小程序之入门篇(一)
微信小程序之注册篇(二)
微信小程序之开发初体验(三)——开发工具使用和目录结构
微信小程序之生命周期(四)
微信小程序之数据绑定(五)
微信小程序之触控事件(六)
微信小程序之基础组件篇——视图容器(七)
微信小程序之基础组件篇——基础内容(八)
微信小程序之基础组件篇——表单组件(九)
微信小程序之基础组件篇——导航组件(十)
微信小程序之基础组件篇——媒体组件(十一)
微信小程序之API篇——豆瓣图书搜索(十二)
微信小程序之拓展篇——weui-wxss(十三)
本篇文章将介绍小程序的基础组件——媒体组件。
媒体组件分为三大组件:
- audio
- image
- video
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()
},
audio14: function () {
this.audioCtx.seek(14)
},
audioStart: function () {
this.audioCtx.seek(0)
}
})
audio效果图
image
image组件也是一个程序不可缺少的,可以这样说一个app中image组件随处可以看到,一般 image有两种加载方式第一种是网络图片第二种是本地图片资源,都用src属性去指定。
-
重点属性
-
三种缩放模式
-
九种剪切方式
<view class="page">
<view class="page__hd">
<text class="page__title">image</text>
<text class="page__desc">图片</text>
</view>
<view class="page__bd">
<view class="section section_gap" wx:for="{{array}}" wx:for-item="item">
<view class="section__title">{{item.text}}</view>
<view class="section__ctn">
<image style="width: 200px; height: 200px; background-color: #eeeeee;" mode="{{item.mode}}" src="{{src}}"></image>
</view>
</view>
</view>
</view>
Page({
data: {
array: [{
mode: 'scaleToFill',
text: 'scaleToFill:不保持纵横比缩放图片,使图片完全适应'
}, {
mode: 'aspectFit',
text: 'aspectFit:保持纵横比缩放图片,使图片的长边能完全显示出来'
}, {
mode: 'aspectFill',
text: 'aspectFill:保持纵横比缩放图片,只保证图片的短边能完全显示出来'
}, {
mode: 'top',
text: 'top:不缩放图片,只显示图片的顶部区域'
}, {
mode: 'bottom',
text: 'bottom:不缩放图片,只显示图片的底部区域'
}, {
mode: 'center',
text: 'center:不缩放图片,只显示图片的中间区域'
}, {
mode: 'left',
text: 'left:不缩放图片,只显示图片的左边区域'
}, {
mode: 'right',
text: 'right:不缩放图片,只显示图片的右边边区域'
}, {
mode: 'top left',
text: 'top left:不缩放图片,只显示图片的左上边区域'
}, {
mode: 'top right',
text: 'top right:不缩放图片,只显示图片的右上边区域'
}, {
mode: 'bottom left',
text: 'bottom left:不缩放图片,只显示图片的左下边区域'
}, {
mode: 'bottom right',
text: 'bottom right:不缩放图片,只显示图片的右下边区域'
}],
src: '../../resources/cat.jpg'
},
imageError: function(e) {
console.log('image3发生error事件,携带值为', e.detail.errMsg)
}
})
- 原图
[图片上传失败...(image-6c5f81-1606306299418)] - scaleToFill
不保持纵横比缩放图片,使图片完全适应
[图片上传失败...(image-214e1a-1606306299419)] - aspectFit
保持纵横比缩放图片,使图片的长边能完全显示出来
[图片上传失败...(image-2f0c90-1606306299419)] - aspectFill
保持纵横比缩放图片,只保证图片的短边能完全显示出来
[图片上传失败...(image-17ad41-1606306299419)] - top
不缩放图片,只显示图片的顶部区域
[图片上传失败...(image-14251e-1606306299419)] - bottom
不缩放图片,只显示图片的底部区域
[图片上传失败...(image-a94e8e-1606306299419)] - center
不缩放图片,只显示图片的中间区域
[图片上传失败...(image-fde650-1606306299419)] - left
不缩放图片,只显示图片的左边区域
[图片上传失败...(image-656f8d-1606306299419)] - right
不缩放图片,只显示图片的右边边区域
[图片上传失败...(image-44fe9a-1606306299419)] - top left
不缩放图片,只显示图片的左上边区域
[图片上传失败...(image-a4ddd4-1606306299419)] - top right
不缩放图片,只显示图片的右上边区域
[图片上传失败...(image-80b4c8-1606306299419)] - bottom left
不缩放图片,只显示图片的左下边区域
[图片上传失败...(image-a10860-1606306299420)] - bottom right
不缩放图片,只显示图片的右下边区域
[图片上传失败...(image-c1042c-1606306299420)]
video
<!--index.wxml-->
<view class="index">
<label class="label" type="primary">东京食尸鬼第{{chapter}}集</label>
<video id="myVideo" class="video" src="http://119.29.74.46/Dj/Dj_{{chapter}}.webm" objectFit="contain" danmu-list="{{danmuList}}" enable-danmu danmu-btn controls></video>
<input bindblur="bindInputBlur" placeholder="来这儿输入你的弹幕!倾泻弹药吧!"/>
<button type="primary" bindtap="bindSendDanmu">发送弹幕</button>
<button type="primary" bindtap="Last">上一集</button>
<button type="primary" bindtap="Next">下一集</button>
<!--index.js-->
function getRandomColor () {
let rgb = []
for (let i = 0 ; i < 3; ++i){
let color = Math.floor(Math.random() * 256).toString(16)
color = color.length == 1 ? '0' + color : color
rgb.push(color)
}
return '#' + rgb.join('')
}
Page({
onReady: function () {
this.videoContext = wx.createVideoContext('myVideo')
this.animation = wx.createAnimation({
transformOrigin: "50% 50%",
duration: 1000,
timingFunction: "ease",
delay: 0
})
},
inputValue: '',
data:{
// Chapters:[1,2,3,4,5,6,7,8,9,10,11,12],
chapter:0,
src: '',
danmuList: [
{
text: '请问柏超是帅哥吗?',
color: '#ff0000',
time: 1
},
{
text: '柏超一个人写弹幕太苦逼了',
color: '#eeeeaa',
time: 2
},
{
text: '你们要的弹幕君来了,还差柏超君',
color: '#e0aaa0',
time: 3
},
{
text: '柏超在不,能看到吗??',
color: '#eeeeee',
time: 4
},
{
text: '柏超是哪个??',
color: '#faaaa0',
time: 5
},
{
text: '请问柏超是谁?',
color: '#ff00ff',
time: 6
}]
},
Next:function(){
this.setData({
chapter:this.data.chapter + 1
})
},
Last:function(){
this.setData({
chapter:this.data.chapter - 1
})
},
bindInputBlur: function(e) {
this.inputValue = e.detail.value
},
bindSendDanmu: function () {
this.videoContext.sendDanmu({
text: this.inputValue,
color: getRandomColor()
})
}
})
<!--index.wxss-->
.index{
background-color: #Eeefaf;
font-family: -apple-system-font,Helvetica Neue,Helvetica,sans-serif;
flex: 1;
min-height: 100%;
font-size: 32rpx;
}
.label{
width: 750rpx;
position:center;
padding-left: 250rpx
}
.video{
width: 750rpx;
position: center;
}
video效果图