W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
解釋:拍攝視頻或從手機(jī)相冊中選視頻,返回視頻的臨時(shí)文件路徑。
Web 態(tài)說明:由于瀏覽器限制,Web 態(tài)不支持圖片壓縮,而且僅能默認(rèn)拉起后置攝像頭,詳見參數(shù)說明。
Object object
屬性名 | 類型 | 必填 | 默認(rèn)值 | 說明 | 最低版本 | Web 態(tài)說明 |
---|---|---|---|---|---|---|
sourceType | Array.<string> | 否 | ['album', 'camera'] | album 從相冊選擇視頻,camera 使用相機(jī),默認(rèn)二者都有。 | - | - |
compressed | Boolean | 否 | true | 是否壓縮所選的視頻源文件,默認(rèn)值為 true,需要壓縮。 | - | Web 態(tài)不支持 |
maxDuration | Number | 否 | 60 | 拍攝視頻最長拍攝時(shí)間,(單位:s)。最長支持 60 秒。 | - | - |
camera | String | 否 | 'back' | 默認(rèn)拉起的是前置或者后置攝像頭。部分 Android 手機(jī)下由于系統(tǒng) ROM 不支持無法生效。 | 3.120.1 | Web 態(tài)始終默認(rèn)拉起后置攝像頭 |
success | Function | 否 | 接口調(diào)用成功,返回視頻文件的臨時(shí)文件路徑,詳見返回參數(shù)說明。 | - | - | |
fail | Function | 否 | 接口調(diào)用失敗的回調(diào)函數(shù) | - | - | |
complete | Function | 否 | 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會執(zhí)行) | - | - |
參數(shù)名 | 參數(shù)類型 | 說明 |
---|---|---|
back | String | 默認(rèn)拉起后置攝像頭 |
front | String | 默認(rèn)拉起前置攝像頭 |
參數(shù) | 參數(shù)類型 | 說明 |
---|---|---|
tempFilePath | String | 選定視頻的臨時(shí)文件路徑 |
duration | Number | 選定視頻的時(shí)間長度 (單位:s) |
size | Number | 選定視頻的數(shù)據(jù)量大?。▎挝唬築) |
height | Number | 返回選定視頻的長 |
width | Number | 返回選定視頻的寬 |
<view class="wrap">
<view class="card-area">
<view class="display-area">
<video class="video" s-if="videoSrc" src="{{videoSrc}}" controls="true" binderror="videoError"></video>
<view s-else>視頻顯示區(qū)</view>
</view>
<view class="middle-area border-top border-bottom">
<view class="list-area border-bottom" hover-class="option-active">
<picker mode="selector" value="{{sourceIndex}}" range="{{sourceArray}}" bind:change="sourceChange">
<text class="list-item-key-4">視頻來源</text>
<view class="list-item-value">{{sourceArray[sourceIndex]}}</view>
</picker>
</view>
<view class="list-area border-bottom" hover-class="option-active">
<picker mode="selector" value="{{compressIndex}}" range="{{compressArray}}" bind:change="compressChange">
<text class="list-item-key-4">視頻質(zhì)量</text>
<view class="list-item-value">{{compressArray[compressIndex]}}</view>
</picker>
</view>
<view class="list-area border-bottom" hover-class="option-active">
<picker mode="selector" value="{{cameraIndex}}" range="{{cameraArray}}" bind:change="cameraChange">
<text class="list-item-key-4">默認(rèn)攝像頭</text>
<view class="list-item-value">{{cameraArray[cameraIndex]}}</view>
</picker>
</view>
<view class="list-area border-bottom" hover-class="option-active">
<picker mode="selector" value="{{countIndex}}" range="{{countArray}}" bind:change="countChange">
<text class="list-item-key-4">拍攝長度</text>
<view class="list-item-value">{{countArray[countIndex]}}</view>
</picker>
</view>
</view>
<view class="button-area">
<button class="btn" type="primary" bindtap="selectVideo">添加視頻</button>
<button class="btn" type="default" bindtap="clearVideo">清空視頻</button>
</view>
</view>
</view>
Page({
data: {
sourceIndex: 0,
sourceArray: ['拍攝', '相冊', '拍攝或相冊'],
compressIndex: 0,
compressArray: ['壓縮', '不壓縮'],
cameraIndex: 0,
cameraArray: ['后置', '前置'],
countIndex: 0,
countArray: [60, 30, 0],
videoSrc: ''
},
onLoad() {
const array = [];
for (let i = 0; i < 60; i++) {
array.push(i + 1 + '秒');
}
this.setData({
countIndex: array.length - 1,
countArray: array
});
},
sourceChange(e) {
this.setData('sourceIndex', e.detail.value);
},
compressChange(e) {
this.setData('compressIndex', e.detail.value);
},
cameraChange(e) {
this.setData('cameraIndex', e.detail.value);
},
countChange(e) {
this.setData('countIndex', e.detail.value);
},
selectVideo() {
const sourceIndex = this.getData('sourceIndex');
const compressed = this.getData('compressIndex') === 0;
const maxDuration = this.getData('countIndex') + 1;
const camera = this.getData('cameraIndex') === 0 ? 'back' : 'front';
if (sourceIndex === 2) {
swan.showActionSheet({
itemList: ['拍攝', '相冊'],
success: res => {
const sourceType = res.tapIndex === 0 ? 'camera' : 'album';
this.chooseVideo(sourceType, compressed, maxDuration, camera);
}
});
}
else {
const sourceType = sourceIndex === 0 ? 'camera' : 'album';
this.chooseVideo(sourceType, compressed, maxDuration, camera);
}
},
chooseVideo(sourceType, compressed, maxDuration, camera) {
swan.chooseVideo({
sourceType: [sourceType],
compressed,
maxDuration,
camera,
success: res => {
this.setData('videoSrc', res.tempFilePath);
},
fail: err => {
swan.showToast({
title: JSON.stringify(err),
icon: 'none'
});
}
});
},
clearVideo() {
if (this.getData('videoSrc')) {
this.setData('videoSrc', '');
swan.showToast({title: '視頻已清空', icon: 'none'});
}
},
videoError() {
this.setData('videoSrc', '');
swan.showToast({title: '添加失敗,請稍后重試', icon: 'none'});
}
});
錯(cuò)誤碼 | 說明 |
---|---|
201 | 解析失敗,請檢查調(diào)起協(xié)議是否合法 |
1002 | 用戶取消操作 |
錯(cuò)誤碼 | 說明 |
---|---|
202 | 解析失敗,請檢查參數(shù)是否正確 |
1002 | 用戶取消操作 |
1004 | 小程序文件目錄為空 |
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: