本文介绍了微信小程序 的开发,主要包括 图片 、录音、音频播放、音乐播放、视频、文件 ,具体如下:
图片 :
wx.chooseImage(OBJECT)
从本地相册 选择图片 或使用相机拍照 。
OBJECT参数说明:
注:文件 的临时路径,在小程序 本次启动期间可以正常使用,如需持久保存,需在主动调用 wx.saveFile,在小程序 下次启动时才能访问得到。
示例代码 :
rush:js;">
wx.chooseImage({
count: 1,//
默 认9
sizeType: ['original','com
pressed '],// 可以指定是原图还是压缩图,
默 认二者都有
sourceType: ['album','camera'],// 可以指定来源是相册还是相机,
默 认二者都有
success: function (res) {
// 返回选定照片的本地
文件 路径列表,tempFilePath可以作为
img标签 的src
属性 显示 图片
var tempFilePaths = res.tempFilePaths
}
})
wx.previewImage(OBJECT)
预览图片 。
OBJECT参数说明:
示例代码 :
rush:js;">
wx.previewImage({
current: '',// 当前
显示 图片 的http
链接
urls: [] // 需要预览的
图片 http
链接 列表
})
wx.getimage Info(OBJECT)
获取 图片 信息
OBJECT参数说明:
success返回参数说明:
示例代码 :
rush:js;">
wx.ge
timage Info({
src: 'images/a.jpg',success: function (res) {
console.log(res.width)
console.log(res.height)
}
})
wx.chooseImage({
success: function (res) {
wx.getimage Info({
src: res.tempFilePaths[0],success: function (res) {
console.log(res.width)
console.log(res.height)
}
})
}
})
录音:
wx.startRecord(OBJECT)
开始录音。当主动调用 wx.stopRecord,或者录音超过1分钟时自动 结束录音,返回录音文件 的临时文件 路径。
OBJECT参数说明:
注:文件 的临时路径,在小程序 本次启动期间可以正常使用,如需持久保存,需在主动调用 wx.saveFile,在小程序 下次启动时才能访问得到。
wx.stopRecord()
主动调用 停止录音。
示例代码 :
rush:js;">
wx.startRecord({
success: function(res) {
var tempFilePath = res.tempFilePath
},fail: function(res) {
//录音失败
}
})
setTimeout(function() {
//结束录音
wx.stopRecord()
},10000)
音频播放控制:
wx.playVoice(OBJECT)
开始播放语音,同时只允许一个 语音文件 正在播放,如果前一个 语音文件 还没播放完,将中断前一个 语音播放。
OBJECT参数说明:
示例代码 :
rush:js;">
wx.startRecord({
success: function(res) {
var tempFilePath = res.tempFilePath
wx.playVoice({
filePath: tempFilePath,complete: function(){
}
})
}
})
wx.pauseVoice()
暂停正在播放的语音。再次调用 wx.playVoice播放同一个 文件 时,会从暂停处开始播放。如果想从头开始播放,需要先调用 wx.stopVoice。
示例代码 :
rush:js;">
wx.startRecord({
success: function(res) {
var tempFilePath = res.tempFilePath
wx.playVoice({
filePath: tempFilePath
})
setTimeout(function() {
//暂停播放
wx.pauseVoice()
},5000)
}
})
wx.stopVoice()
结束播放语音。
示例代码 :
rush:js;">
wx.startRecord({
success: function(res) {
var tempFilePath = res.tempFilePath
wx.playVoice({
filePath:tempFilePath
})
setTimeout(function(){
wx.stopVoice()
},5000)
}
})
音乐播放控制:
wx.getBackgroundAudio PlayerState(OBJECT)
获取 音乐播放状态。
OBJECT参数说明:
success返回参数说明:
示例代码 :
rush:js;">
wx.getBackgroundAu
dio PlayerState({
success: function(res) {
var status = res.status
var dataUrl = res.dataUrl
var currentPosition = res.currentPosition
var duration = res.duration
var downloadPercent = res.downloadPercent
}
})
wx.playBackgroundAudio (OBJECT)
播放音乐,同时只能有一首音乐正在播放。
OBJECT参数说明
示例代码
rush:js;">
wx.playBackgroundAu
dio ({
dataUrl: '',title: '',cover
imgur l: ''
})
wx.pauseBackgroundAudio ()
暂停播放音乐。
示例代码
rush:js;">
wx.pauseBackgroundAu
dio ()
wx.seekBackgroundAudio (OBJECT)
控制音乐播放进度。
OBJECT参数说明
示例代码
rush:js;">
wx.seekBackgroundAu
dio ({
position: 30
})
wx.stopBackgroundAudio ()
停止播放音乐。
示例代码
rush:js;">
wx.stopBackgroundAu
dio ()
wx.onBackgroundAudio Play(CALLBACK)
监听音乐播放。
wx.onBackgroundAudio Pause(CALLBACK)
监听音乐暂停。
wx.onBackgroundAudio Stop(CALLBACK)
监听音乐停止。
文件 :
wx.saveFile(OBJECT)
保存文件 到本地。
OBJECT参数说明:
示例代码 :
rush:js;">
wx.startRecord({
success: function(res) {
var tempFilePath = res.tempFilePath
wx.saveFile({
tempFilePath: tempFilePath,success: function(res) {
var savedFilePath = res.savedFilePath
}
})
}
})
wx.getSavedFileList(OBJECT)
获取 本地已保存的文件 列表
OBJECT参数说明:
success返回参数说明:
fileList中的项目说明:
示例代码 :
rush:js;">
wx.getSavedFileList({
success: function(res) {
console.log(res.fileList)
}
})
wx.getSavedFileInfo(OBJECT)
获取 本地文件 的文件 信息
OBJECT参数说明:
success返回参数说明:
示例代码 :
rush:js;">
wx.getSavedFileInfo({
filePath: 'wxfile://somefile',//仅做示例用,非真正的
文件 路径
success: function(res) {
console.log(res.size)
console.log(res.createTime)
}
})
wx.removeSavedFile(OBJECT)
删除 本地存储的文件
OBJECT参数说明:
示例代码 :
0){
wx.removeSavedFile({
filePath: res.fileList[0].filePath,complete: function(res) {
console.log(res)
}
})
}
}
})
新开页面 打开文档,支持 格式:doc,xls,ppt,pdf,docx,xlsx,pptx
OBJECT参数说明:
示例代码
rush:js;">
wx.downloadFile({
url: 'http://example.com/somefile.pdf',success: function (res) {
var filePath = res.tempFilePath
wx.
opendocument ({
filePath: filePath,success: function (res) {
console.log('打开文档成功')
}
})
}
})
视频:
wx.chooseVideo(OBJECT)
拍摄视频或从手机相册中选视频,返回视频的临时文件 路径。
OBJECT参数说明:
返回参数说明:
注:文件 的临时路径,在小程序 本次启动期间可以正常使用,如需持久保存,需在主动调用 wx.saveFile,在小程序 下次启动时才能访问得到。
示例代码 :
rush:xml;">
获取视频
rush:js;">
Page({
bindButtonTap: function() {
var that = this
wx.chooseVideo({
sourceType: ['album',maxDuration: 60,camera: ['front','back'],success: function(res) {
that.setData({
src: res.tempFilePath
})
}
})
}
})
音频组件控制:
wx.createAudio Context(audio Id)
创建并返回 audio 上下文 audio Context 对象
audio Context
audio Context 通过 audio Id 跟一个 audio 组件绑定,通过它可以操作一个 audio 组件。
audio Context对象的方法 列表:
示例代码 :
rush:xml;">
<button type="primary" bindtap="audio Play">播放
<button type="primary" bindtap="audio Pause">暂停
<button type="primary" bindtap="audio 14">设置当前播放时间为14秒
<button type="primary" bindtap="audio Start">回到开头
rush:js;">
// au
dio .js
Page({
onReady: function (e) {
// 使用 wx.createAu
dio Context
获取 au
dio 上下文 context
this.au
dio Ctx = wx.createAu
dio Context('myAu
dio ')
},data: {
src: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46',},au
dio Play: function () {
this.au
dio Ctx.play()
},au
dio Pause: function () {
this.au
dio Ctx.pause()
},au
dio 14: function () {
this.au
dio Ctx.seek(14)
},au
dio Start: function () {
this.au
dio Ctx.seek(0)
}
})
视频组件控制:
wx.createVideoContext(videoId)
创建并返回 video 上下文 videoContext 对象
videoContext
videoContext 通过 videoId 跟一个 video 组件绑定,通过它可以操作一个 video 组件。
videoContext对象的方法 列表:
示例代码 :
rush:xml;">
rush: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 (res) {
this.videoContext = wx.createVideoContext('myVideo')
},inputValue: '',bindInputBlur: function(e) {
this.inputValue = e.detail.value
},bindSendDanmu: function () {
this.videoContext.sendDanmu({
text: this.inputValue,color: getRandomColor()
})
}
})
以上就是本文的全部内容 ,希望对大家的学习有所帮助,也希望大家多多支持 编程之家。