微信小程序之基础组件篇——媒体组件(十一)

微信小程序之入门篇(一)
微信小程序之注册篇(二)
微信小程序之开发初体验(三)——开发工具使用和目录结构
微信小程序之生命周期(四)
微信小程序之数据绑定(五)
微信小程序之触控事件(六)
微信小程序之基础组件篇——视图容器(七)
微信小程序之基础组件篇——基础内容(八)
微信小程序之基础组件篇——表单组件(九)
微信小程序之基础组件篇——导航组件(十)
微信小程序之基础组件篇——媒体组件(十一)
微信小程序之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效果图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,024评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,268评论 4 61
  • 我有一个关系很好的朋友和我一起在魔都工作。工作压力大、生活节奏快暂且不提,既然选择了离开家乡来到上海,自然是明白自...
    铁血路人麻麻桑阅读 500评论 1 0
  • 原文 英国首先转型为现代国家后,别的民族别无选择了,必须跟着现代化,要沿着这个轨迹向前发展,在历史长河中,像中国这...
    鸭梨山大哎阅读 2,923评论 1 1