微信小程序学习之路——媒体组件(一)

image

一个应用中图片是必不可少的,小程序的<iamge/>除了可以显示图片外,还提供了图片的裁剪、缩放模式属性,<image/>默认宽度为300px,默认高度为225p,其属性如下:

属性名 类型 默认值 说明 最低版本
src String   图片资源地址,支持云文件ID(2.2.3起)  
mode String 'scaleToFill' 图片裁剪、缩放的模式  
lazy-load Boolean false 图片懒加载,在即将进入当前屏幕可视区域时才开始加载 1.5.0
binderror HandleEvent   当错误发生时,发布到 AppService 的事件名,事件对象event.detail = {errMsg: 'something wrong'}  
bindload HandleEvent   当图片载入完毕时,发布到 AppService 的事件名,事件对象event.detail = {height:'图片高度px', width:'图片宽度px'}  
aria-label String   无障碍访问,(属性)元素的额外描述 2.5.0

mode 有效值:

mode 有 13 种模式,其中 4 种是缩放模式,9 种是裁剪模式。

模式 说明
缩放 scaleToFill 不保持纵横比缩放图片,使图片的宽高完全拉伸至填满 image 元素
缩放 aspectFit 保持纵横比缩放图片,使图片的长边能完全显示出来。也就是说,可以完整地将图片显示出来。
缩放 aspectFill 保持纵横比缩放图片,只保证图片的短边能完全显示出来。也就是说,图片通常只在水平或垂直方向是完整的,另一个方向将会发生截取。
缩放 widthFix 宽度不变,高度自动变化,保持原图宽高比不变
裁剪 top 不缩放图片,只显示图片的顶部区域
裁剪 bottom 不缩放图片,只显示图片的底部区域
裁剪 center 不缩放图片,只显示图片的中间区域
裁剪 left 不缩放图片,只显示图片的左边区域
裁剪 right 不缩放图片,只显示图片的右边区域
裁剪 top left 不缩放图片,只显示图片的左上边区域
裁剪 top right 不缩放图片,只显示图片的右上边区域
裁剪 bottom left 不缩放图片,只显示图片的左下边区域
裁剪 bottom right 不缩放图片,只显示图片的右下边区域

示例代码如下:

<view class='page'>
  <view class='page_hd'>
    <text class='page_title'>image</text>
    <text class='oage_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_btn'>
    <image style='width:200px;height200;background-color:#eeeeee;' mode='{{item.mode}}' src='{{src}}'></image>
    </view>
    </view>
  </view>
</view>
Page({
  data:{
    array:[{
      mode:'scaleToFill',
      text:'sacleToFill:不保持纵横缩放图片,使图片完全适应'
    },{
      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:'../../images/12.jpg'
},
imageError(e){
  console.log('image3发生error事件,携带的值为:',e.detail.errMsg)
}
})

执行结果如下:

audio

小程序运行在页面中直接嵌入的音频组件,官方系统为其提供了一套默认的组件样式,我们也可以通过修改属性或调用API封装自己的音频UI组件,<audio/>属性如下:

属性名 类型 默认值 说明
id String   audio 组件的唯一标识符
src String   要播放音频的资源地址
loop Boolean false 是否循环播放
controls Boolean false 是否显示默认控件
poster String   默认控件上的音频封面的图片资源地址,如果 controls 属性值为 false 则设置 poster 无效
name String 未知音频 默认控件上的音频名字,如果 controls 属性值为 false 则设置 name 无效
author String 未知作者 默认控件上的作者名字,如果 controls 属性值为 false 则设置 author 无效
binderror EventHandle   当发生错误时触发 error 事件,detail = {errMsg: MediaError.code}
bindplay EventHandle   当开始/继续播放时触发play事件
bindpause EventHandle   当暂停播放时触发 pause 事件
bindtimeupdate EventHandle   当播放进度改变时触发 timeupdate 事件,detail = {currentTime, duration}
bindended EventHandle   当播放到末尾时触发 ended 事件

MediaError.code

返回错误码 描述
1 获取资源被用户禁止
2 网络错误
3 解码错误
4 不合适资源
<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='promary' bindtap='audio14'>设置当前播放时间为14秒</button>
  <button type='primary' bindtap='audioStart'>回到开头</button>
Page({
  onReady(e){
    this.audioCtx = wx.createAudioContext('myAudio')
  },
  data:{
    poster: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',
    name: '此时此刻',
    author: '许巍',src:"http://112.17.14.90/cache/ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46&ich_args2=1296-01213410008443_b82fdf0dc355988fc1caa7f33fdfb7d2_10004305_9c896128d6c1f4d7903e518939a83798_6966fec5fe15ec416787211efd47712f"
  },
  audioPlay(){
    this.audioCtx.play()
  },
  audioPause(){
    this.audioCtx.pause()
  },
  audio14(){
    this.audioCtx.seek(14)
  },
  audioStart(){
    this.audioCtx.seek(0)
  }
})

执行结果如下:

video

小程序中允许我们嵌入视频,默认宽度为300px,高度为225px,属性如下:

属性名 类型 默认值 说明 最低版本
src String   要播放视频的资源地址,支持云文件ID(2.2.3起)  
duration Number   指定视频时长 1.1.0
controls Boolean true 是否显示默认播放控件(播放/暂停按钮、播放进度、时间)  
danmu-list Object Array   弹幕列表  
danmu-btn Boolean false 是否显示弹幕按钮,只在初始化时有效,不能动态变更  
enable-danmu Boolean false 是否展示弹幕,只在初始化时有效,不能动态变更  
autoplay Boolean false 是否自动播放  
loop Boolean false 是否循环播放 1.4.0
muted Boolean false 是否静音播放 1.4.0
initial-time Number   指定视频初始播放位置 1.6.0
page-gesture Boolean false 在非全屏模式下,是否开启亮度与音量调节手势(废弃,见 vslide-gesture) 1.6.0
direction Number   设置全屏时视频的方向,不指定则根据宽高比自动判断。有效值为 0(正常竖向), 90(屏幕逆时针90度), -90(屏幕顺时针90度) 1.7.0
show-progress Boolean true 若不设置,宽度大于240时才会显示 1.9.0
show-fullscreen-btn Boolean true 是否显示全屏按钮 1.9.0
show-play-btn Boolean true 是否显示视频底部控制栏的播放按钮 1.9.0
show-center-play-btn Boolean true 是否显示视频中间的播放按钮 1.9.0
enable-progress-gesture Boolean true 是否开启控制进度的手势 1.9.0
object-fit String contain 当视频大小与 video 容器大小不一致时,视频的表现形式。contain:包含,fill:填充,cover:覆盖  
poster String   视频封面的图片网络资源地址或云文件ID(2.2.3起支持)。若 controls 属性值为 false 则设置 poster 无效  
show-mute-btn Boolean false 是否显示静音按钮 2.4.0
title String   视频的标题,全屏时在顶部展示 2.4.0
play-btn-position String bottom 播放按钮的位置,有效值为:bottom(controls bar 上)、center(视频中间) 2.4.0
enable-play-gesture Boolean false 是否开启播放手势,即双击切换播放/暂停 2.4.0
auto-pause-if-navigate Boolean true 当跳转到其它小程序页面时,是否自动暂停本页面的视频 2.5.0
auto-pause-if-open-native Boolean true 当跳转到其它微信原生页面时,是否自动暂停本页面的视频 2.5.0
vslide-gesture Boolean false 在非全屏模式下,是否开启亮度与音量调节手势(同 page-gesture) 2.6.2
vslide-gesture-in-fullscreen Boolean true 在全屏模式下,是否开启亮度与音量调节手势 2.6.2
bindplay EventHandle   当开始/继续播放时触发play事件  
bindpause EventHandle   当暂停播放时触发 pause 事件  
bindended EventHandle   当播放到末尾时触发 ended 事件  
bindtimeupdate EventHandle   播放进度变化时触发,event.detail = {currentTime, duration} 。触发频率 250ms 一次  
bindfullscreenchange EventHandle   视频进入和退出全屏时触发,event.detail = {fullScreen, direction},direction 有效值为 vertical 或 horizontal 1.4.0
bindwaiting EventHandle   视频出现缓冲时触发 1.7.0
binderror EventHandle   视频播放出错时触发 1.7.0
bindprogress EventHandle   加载进度变化时触发,只支持一段加载。event.detail = {buffered},百分比 2.4.0

支持的格式

格式 iOS Android
mp4
mov x
m4v x
3gp
avi x
m3u8
webm x

支持的编码格式

格式 iOS Android
H.264
HEVC
MPEG-4
VP9 x

示例代码如下:

<view class="page-body">
  <view class='section tc'>
    <video id="myVideo" src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400" binderror="videoErrorCallback" danmu-list="{{danmuList}}" enable-danmu danmu-btn controls></video>
  <view class='cells'>
   <view class='label'>弹幕内容:</view>
   <input bindblur='bindInputBlur' class='input' type='text' placeholder='在此输入弹幕内容' ></input>
  </view>
   <view class='btn-area'>
    <button bindtap='bindButtonTap' class='page-body-button' type='primary'>获取视频</button>
    <button bindtap='bindSendDanmu' class='page-body-button' type='primary' form-type='submit'>发送弹幕</button>
    <button bindtap="bindPlay" class="page-body-button" type="primary">播放</button>
      <button bindtap="bindPause" class="page-body-button" type="primary">暂停</button>
    </view>
  </view>
</view>
function getRandomColor () {
  const 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(res){
    this.videoContext = wx.createVideoContext("myVideo")
  },
  inputValue:"",
  data:{
    src:"",
    danmuList:[
      {
        text:'第一秒出现的弹幕',
        color:getRandomColor(),
        time:1
      },
      {
        text: '第 3s 出现的弹幕',
        color: getRandomColor(),
        time: 3
        }]
  },
  bindInputBlur: function (e) {
    this.inputValue = e.detail.value
  },
  bindSendDanmu: function () {
    this.videoContext.sendDanmu({
      text: this.inputValue,
      color: getRandomColor()
    })
  },
  bindButtonTap(){
    const that =this
    wx.chooseVideo({
      sourceType:['album','camera'],
      maxDuration:60,
      camera:['front','back'],
      success(res){
        that.setData({
          src:res.tempFilePath
        })
      }
    })
  },

  bindPlay:function(){
    this.videoContext.play()
  },
  bindPause:function(){
    this.videoContext.pause()
  },
  videoErrorCallback:function(e){
    console.log('视频信息错误')
    console.log(e.detai.errMsg)
  }
 
})
.page-body-button {
  margin-bottom: 30rpx;
}
.label{  width: 5em;}

.input{ height: 2.58823529em;
  min-height: 2.58823529em;
  line-height: 2.58823529em;
  }

.cells{ margin-top: 80rpx;
  text-align: left;
  }

除此之外还不够噢,需要在app.wxss中修改样式,使之居中

page {
  background-color: #F8F8F8;
  height: 100%;
  font-size: 32rpx;
  line-height: 1.6;
}

.page-body {
  padding: 20rpx 0;
}

.btn-area{
  margin-top: 60rpx;
  box-sizing: border-box;
  width: 100%;
  padding: 0 30rpx;
}

.tc{
  text-align: center;
}

执行结果如下:

猜你喜欢

转载自blog.csdn.net/CSDN_XUWENHAO/article/details/88956075