本文介紹了微信小程序的開(kāi)發(fā),主要包括圖片、錄音、音頻播放、音樂(lè)播放、視頻、文件,具體如下:
圖片:
wx.chooseImage(OBJECT)
從本地相冊(cè)選擇圖片或使用相機(jī)拍照。
OBJECT參數(shù)說(shuō)明:
注:文件的臨時(shí)路徑,在小程序本次啟動(dòng)期間可以正常使用,如需持久保存,需在主動(dòng)調(diào)用 wx.saveFile,在小程序下次啟動(dòng)時(shí)才能訪問(wèn)得到。
示例代碼:
wx.chooseImage({ count: 1, // 默認(rèn)9 sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認(rèn)二者都有 sourceType: ['album', 'camera'], // 可以指定來(lái)源是相冊(cè)還是相機(jī),默認(rèn)二者都有 success: function (res) { // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標(biāo)簽的src屬性顯示圖片 var tempFilePaths = res.tempFilePaths }})
wx.previewImage(OBJECT)
預(yù)覽圖片。
OBJECT參數(shù)說(shuō)明:
示例代碼:
wx.previewImage({ current: '', // 當(dāng)前顯示圖片的http鏈接 urls: [] // 需要預(yù)覽的圖片http鏈接列表})
wx.getImageInfo(OBJECT)
獲取圖片信息
OBJECT參數(shù)說(shuō)明:
success返回參數(shù)說(shuō)明:
示例代碼:
wx.getImageInfo({ src: 'images/a.jpg', success: function (res) { console.log(res.width) console.log(res.height) }})wx.chooseImage({ success: function (res) { wx.getImageInfo({ src: res.tempFilePaths[0], success: function (res) { console.log(res.width) console.log(res.height) } }) }})
錄音:
wx.startRecord(OBJECT)
開(kāi)始錄音。當(dāng)主動(dòng)調(diào)用wx.stopRecord,或者錄音超過(guò)1分鐘時(shí)自動(dòng)結(jié)束錄音,返回錄音文件的臨時(shí)文件路徑。
OBJECT參數(shù)說(shuō)明:
注:文件的臨時(shí)路徑,在小程序本次啟動(dòng)期間可以正常使用,如需持久保存,需在主動(dòng)調(diào)用wx.saveFile,在小程序下次啟動(dòng)時(shí)才能訪問(wèn)得到。
wx.stopRecord()
主動(dòng)調(diào)用停止錄音。
示例代碼:
wx.startRecord({ success: function(res) { var tempFilePath = res.tempFilePath }, fail: function(res) { //錄音失敗 }})setTimeout(function() { //結(jié)束錄音 wx.stopRecord()}, 10000)
音頻播放控制:
wx.playVoice(OBJECT)
開(kāi)始播放語(yǔ)音,同時(shí)只允許一個(gè)語(yǔ)音文件正在播放,如果前一個(gè)語(yǔ)音文件還沒(méi)播放完,將中斷前一個(gè)語(yǔ)音播放。
OBJECT參數(shù)說(shuō)明:
示例代碼:
wx.startRecord({ success: function(res) { var tempFilePath = res.tempFilePath wx.playVoice({ filePath: tempFilePath, complete: function(){ } }) }})
wx.pauseVoice()
暫停正在播放的語(yǔ)音。再次調(diào)用wx.playVoice播放同一個(gè)文件時(shí),會(huì)從暫停處開(kāi)始播放。如果想從頭開(kāi)始播放,需要先調(diào)用 wx.stopVoice。
示例代碼:
wx.startRecord({ success: function(res) { var tempFilePath = res.tempFilePath wx.playVoice({ filePath: tempFilePath }) setTimeout(function() { //暫停播放 wx.pauseVoice() }, 5000) }})
wx.stopVoice()
結(jié)束播放語(yǔ)音。
示例代碼:
wx.startRecord({ success: function(res) { var tempFilePath = res.tempFilePath wx.playVoice({ filePath:tempFilePath }) setTimeout(function(){ wx.stopVoice() }, 5000) }})
音樂(lè)播放控制:
wx.getBackgroundAudioPlayerState(OBJECT)
獲取音樂(lè)播放狀態(tài)。
OBJECT參數(shù)說(shuō)明:
success返回參數(shù)說(shuō)明:
示例代碼:
wx.getBackgroundAudioPlayerState({ 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)
播放音樂(lè),同時(shí)只能有一首音樂(lè)正在播放。
OBJECT參數(shù)說(shuō)明
示例代碼
wx.playBackgroundAudio({ dataUrl: '', title: '', coverImgUrl: ''})
wx.pauseBackgroundAudio()
暫停播放音樂(lè)。
示例代碼
wx.pauseBackgroundAudio()
wx.seekBackgroundAudio(OBJECT)
控制音樂(lè)播放進(jìn)度。
OBJECT參數(shù)說(shuō)明
示例代碼
wx.seekBackgroundAudio({ position: 30})
wx.stopBackgroundAudio()
停止播放音樂(lè)。
示例代碼
wx.stopBackgroundAudio()
wx.onBackgroundAudioPlay(CALLBACK)
監(jiān)聽(tīng)音樂(lè)播放。
wx.onBackgroundAudioPause(CALLBACK)
監(jiān)聽(tīng)音樂(lè)暫停。
wx.onBackgroundAudioStop(CALLBACK)
監(jiān)聽(tīng)音樂(lè)停止。
文件:
wx.saveFile(OBJECT)
保存文件到本地。
OBJECT參數(shù)說(shuō)明:
示例代碼:
wx.startRecord({ success: function(res) { var tempFilePath = res.tempFilePath wx.saveFile({ tempFilePath: tempFilePath, success: function(res) { var savedFilePath = res.savedFilePath } }) }})
wx.getSavedFileList(OBJECT)
獲取本地已保存的文件列表
OBJECT參數(shù)說(shuō)明:
success返回參數(shù)說(shuō)明:
fileList中的項(xiàng)目說(shuō)明:
示例代碼:
wx.getSavedFileList({ success: function(res) { console.log(res.fileList) }})
wx.getSavedFileInfo(OBJECT)
獲取本地文件的文件信息
OBJECT參數(shù)說(shuō)明:
success返回參數(shù)說(shuō)明:
示例代碼:
wx.getSavedFileInfo({ filePath: 'wxfile://somefile', //僅做示例用,非真正的文件路徑 success: function(res) { console.log(res.size) console.log(res.createTime) }})
wx.removeSavedFile(OBJECT)
刪除本地存儲(chǔ)的文件
OBJECT參數(shù)說(shuō)明:
示例代碼:
wx.getSavedFileList({ success: function(res) { if (res.fileList.length > 0){ wx.removeSavedFile({ filePath: res.fileList[0].filePath, complete: function(res) { console.log(res) } }) } }})
wx.openDocument(OBJECT)
新開(kāi)頁(yè)面打開(kāi)文檔,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx
OBJECT參數(shù)說(shuō)明:
示例代碼
wx.downloadFile({ url: 'http://example.com/somefile.pdf', success: function (res) { var filePath = res.tempFilePath wx.openDocument({ filePath: filePath, success: function (res) { console.log('打開(kāi)文檔成功') } }) }})
視頻:
wx.chooseVideo(OBJECT)
拍攝視頻或從手機(jī)相冊(cè)中選視頻,返回視頻的臨時(shí)文件路徑。
OBJECT參數(shù)說(shuō)明:
返回參數(shù)說(shuō)明:
注:文件的臨時(shí)路徑,在小程序本次啟動(dòng)期間可以正常使用,如需持久保存,需在主動(dòng)調(diào)用 wx.saveFile,在小程序下次啟動(dòng)時(shí)才能訪問(wèn)得到。
示例代碼:
<view class="container"> <video src="{{src}}"></video> <button bindtap="bindButtonTap">獲取視頻</button></view>
Page({ bindButtonTap: function() { var that = this wx.chooseVideo({ sourceType: ['album','camera'], maxDuration: 60, camera: ['front','back'], success: function(res) { that.setData({ src: res.tempFilePath }) } }) }})
音頻組件控制:
wx.createAudioContext(audioId)
創(chuàng)建并返回 audio 上下文 audioContext 對(duì)象
audioContext
audioContext 通過(guò) audioId 跟一個(gè) audio 組件綁定,通過(guò)它可以操作一個(gè) audio 組件。
audioContext對(duì)象的方法列表:
示例代碼:
<!-- audio.wxml --><audio src="{{src}}" id="myAudio" ></audio><button type="primary" bindtap="audioPlay">播放</button><button type="primary" bindtap="audioPause">暫停</button><button type="primary" bindtap="audio14">設(shè)置當(dāng)前播放時(shí)間為14秒</button><button type="primary" bindtap="audioStart">回到開(kāi)頭</button>
// audio.jsPage({ onReady: function (e) { // 使用 wx.createAudioContext 獲取 audio 上下文 context this.audioCtx = wx.createAudioContext('myAudio') }, data: { 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) }})
視頻組件控制:
wx.createVideoContext(videoId)
創(chuàng)建并返回 video 上下文 videoContext 對(duì)象
videoContext
videoContext 通過(guò) videoId 跟一個(gè) video 組件綁定,通過(guò)它可以操作一個(gè) video 組件。
videoContext對(duì)象的方法列表:
示例代碼:
<view class="section tc"> <video id="myVideo" src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400" enable-danmu danmu-btn controls></video> <view class="btn-area"> <input bindblur="bindInputBlur"/> <button bindtap="bindSendDanmu">發(fā)送彈幕</button> </view></view>
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() }) }})
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注