小程序学习之媒体API(四)视频管理

1.选择视频

使用wx.chooseVideo(OBJECT)拍摄视频或从手机相册中选视频,返回视频的临时文件路径,参数说明如下:

succcess()返回参数说明:

ps:wx.chooseVideo()获得的视频仅能在小程序启动期间临时使用,如需持久保存,需要主动调用wx.saveFile()进行保存,下次启动时才能访问得到。

2.保存视频

wx.saveVideoToPhotosAlbum(OBJECT)保存视频到系统相册,需用户授权scope.writePhotosAlbum,参数说明如下:

3.视频组件控制

使用wx.createVideoContext(video,this)创建并返回视频上下文对象videoContext,其通过videoId和一个video组件绑定,通过它可以操作一个video组件。在自定义组件下,第二个参数传入组件实例this,以操作组件内的<video>组件。其方法说明如下:

综合应用示例:

<!--wxml-->
<view class="title">第6章 媒体API</view>
<view class="demo-box">
 <view class="title">视频管理综合应用</view>
 <button bindtap="chooseVideo">选择视频</button>
 <video id="myVideo" src="{{src}}" enable-danmu danmu-btn controls></video>
 <button type="primary" size="mini" bindtap="play">播放</button>
 <button type="primary" size="mini" bindtap="pause">暂停</button>
 <button type="primary" size="mini" bindtap="saveVideo">保存视频</button>
 <input placeholder="请在此处填写弹幕内容" bindblur="bindInputBlur"></input>
 <button type="primary" size="mini" bindtap="bindSendDanmu">发送弹幕</button>
</view>

/*wxss*/
input{
  border: 1rpx solid lightblue;
  height: 90rpx;
  margin: 10rpx;
}
button{
  margin-left: 50rpx;
}

//js
function getRandomColor(){
  let rgb=[]
  for(let i=0;i<3;i++){
    let color=Math.floor(Math.random()*256).toString(16)
    console.log(color)
    color=color.length==1?'0'+color:color
    rgb.push(color)
  }
  console.log("#" + rgb.join(''))
  return "#" + rgb.join('')
}
Page({
  //选择视频
  chooseVideo:function(){
    var that = this
    wx.chooseVideo({
      sourceType: ['album','camera'],
      maxDuration: 60,
      camera:'back',
      success: function(res) {
        that.setData({
          src:res.tempFilePath
        })
      }
    })
  },
  //开始播放
  play:function(){
    this.videoContext.play()
  },
  //暂停播放
  pause:function(){
    this.videoContext.pause()
  },
  //保存视频
  saveVideo:function(){
    var src = this.data.src
    wx.saveVideoToPhotosAlbum({
      filePath: src,
      success: function(res) {
        wx.showToast({
          title: '保存成功!',
        })
      }
    })
  },
  inputValue:'', //弹幕文本内容
  //更新弹幕文本内容
  bindInputBlur:function(e){
    this.inputValue=e.detail.value
  },
  //发送弹幕
  bindSendDanmu:function(){
    this.videoContext.sendDanmu({
      text:this.inputValue,
      color:getRandomColor() 
    })
  },
  onLoad:function(options){
    this.videoContext = wx.createVideoContext("myVideo")
  }
})

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值