diff --git a/docs/api/media/video.md b/docs/api/media/video.md index aa7101dc413d2848ad401879d1d1293eb37f57a3..17489e2937814ca2b847d56b6502970b9ee2904b 100644 --- a/docs/api/media/video.md +++ b/docs/api/media/video.md @@ -241,7 +241,7 @@ export default { |App|H5|微信小程序|支付宝小程序|百度小程序|字节跳动小程序|QQ小程序| |:-:|:-:|:-:|:-:|:-:|:-:|:-:| -|x|x|2.11.0+|x|x|x|x| +|3.1.10+|3.1.10+|2.11.0+|x|x|x|x| **OBJECT 参数说明** @@ -254,16 +254,16 @@ export default { **success 返回参数说明** -|参数名 |类型 |说明 | -|:- |:- |:- | -|orientation|string |画面方向 | -|type |string |视频格式 | -|duration |number |视频长度 | -|size |number |视频大小,单位 kB | -|height |number |视频的长,单位 px | -|width |number |视频的宽,单位 px | -|fps |number |视频帧率 | -|bitrate |number |视频码率,单位 kbps| +|参数名 |类型 |说明 |平台差异说明| +|:- |:- |:- || +|orientation|string |画面方向 |微信小程序| +|type |string |视频格式 |微信小程序| +|duration |number |视频长度 |微信小程序、App、H5| +|size |number |视频大小,单位 kB |微信小程序、App、H5| +|height |number |视频的长,单位 px |微信小程序、App、H5| +|width |number |视频的宽,单位 px |微信小程序、App、H5| +|fps |number |视频帧率 |微信小程序、App| +|bitrate |number |视频码率,单位 kbps|微信小程序| **res.orientation参数说明** @@ -286,7 +286,7 @@ export default { |App|H5|微信小程序|支付宝小程序|百度小程序|字节跳动小程序|QQ小程序| |:-:|:-:|:-:|:-:|:-:|:-:|:-:| -|x|x|2.11.0+|x|x|x|x| +|3.1.10+|x|2.11.0+|x|x|x|x| App端有很多插件支持视频压缩,详见[插件市场](https://ext.dcloud.net.cn/search?q=%E8%A7%86%E9%A2%91%E5%8E%8B%E7%BC%A9) diff --git a/src/platforms/app-plus/service/api/ad/ad-base.js b/src/platforms/app-plus/service/api/ad/ad-base.js new file mode 100644 index 0000000000000000000000000000000000000000..4d210eae0ca5b7cb8b0c83165ece159a151af788 --- /dev/null +++ b/src/platforms/app-plus/service/api/ad/ad-base.js @@ -0,0 +1,156 @@ +const eventTypes = { + load: 'load', + close: 'close', + error: 'error', + adClicked: 'adClicked' +} + +const eventNames = [ + eventTypes.load, + eventTypes.close, + eventTypes.error, + eventTypes.adClicked +] + +class AdBase { + constructor (adInstance, options) { + const _callbacks = this._callbacks = {} + eventNames.forEach(item => { + _callbacks[item] = [] + const name = item[0].toUpperCase() + item.substr(1) + this[`on${name}`] = function (callback) { + _callbacks[item].push(callback) + } + }) + + this._preload = options.preload !== undefined ? options.preload : false + + this._isLoaded = false + this._isLoading = false + this._adError = '' + this._loadPromiseResolve = null + this._loadPromiseReject = null + this._showPromiseResolve = null + this._showPromiseReject = null + + const ad = this._ad = adInstance + ad.onLoad((e) => { + this._isLoaded = true + this._isLoading = false + this._dispatchEvent(eventTypes.load, {}) + + if (this._loadPromiseResolve != null) { + this._loadPromiseResolve() + this._loadPromiseResolve = null + } + if (this._showPromiseResolve != null) { + this._showPromiseResolve() + this._showPromiseResolve = null + this._showAd() + } + }) + ad.onClose((e) => { + this._isLoaded = false + this._isLoading = false + this._dispatchEvent(eventTypes.close, { isEnded: e.isEnded }) + + if (this._preload === true) { + this._loadAd() + } + }) + ad.onError((e) => { + this._isLoading = false + + const data = { + code: e.code, + errMsg: e.message + } + + this._adError = data + + this._dispatchEvent(eventTypes.error, data) + + const promiseError = new Error(JSON.stringify(this._adError)) + promiseError.code = e.code + promiseError.errMsg = e.message + + if (this._loadPromiseReject != null) { + this._loadPromiseReject(promiseError) + this._loadPromiseReject = null + } + + if (this._showPromiseReject != null) { + this._showPromiseReject(promiseError) + this._showPromiseReject = null + } + }) + ad.onAdClicked && ad.onAdClicked((e) => { + this._dispatchEvent(eventTypes.adClicked, {}) + }) + } + + load () { + return new Promise((resolve, reject) => { + this._loadPromiseResolve = resolve + this._loadPromiseReject = reject + if (this._isLoading) { + return + } + + if (this._isLoaded) { + resolve() + } else { + this._loadAd() + } + }) + } + + show () { + return new Promise((resolve, reject) => { + this._showPromiseResolve = resolve + this._showPromiseReject = reject + + if (this._isLoading) { + return + } + + if (this._isLoaded) { + this._showAd() + resolve() + } else { + this._loadAd() + } + }) + } + + destroy () { + this._ad.destroy() + } + + getProvider () { + return this._ad.getProvider() + } + + _loadAd () { + this._adError = '' + this._isLoaded = false + this._isLoading = true + this._ad.load() + } + + _showAd () { + this._ad.show() + } + + _dispatchEvent (name, data) { + this._callbacks[name].forEach(callback => { + if (typeof callback === 'function') { + callback(data || {}) + } + }) + } +} + +export { + AdBase +} diff --git a/src/platforms/app-plus/service/api/ad/full-screen-video-ad.js b/src/platforms/app-plus/service/api/ad/full-screen-video-ad.js index 8f5efbc54c920ccf2d5d0ae14593de6136bea15d..bb9053def9d057609ad9b0dfcf14489b4815f274 100644 --- a/src/platforms/app-plus/service/api/ad/full-screen-video-ad.js +++ b/src/platforms/app-plus/service/api/ad/full-screen-video-ad.js @@ -1,103 +1,10 @@ -const eventNames = [ - 'load', - 'close', - 'error', - 'adClicked' -] +import { + AdBase +} from './ad-base.js' -class FullScreenVideoAd { +class FullScreenVideoAd extends AdBase { constructor (options = {}) { - const _callbacks = this._callbacks = {} - eventNames.forEach(item => { - _callbacks[item] = [] - const name = item[0].toUpperCase() + item.substr(1) - this[`on${name}`] = function (callback) { - _callbacks[item].push(callback) - } - }) - - this._isLoad = false - this._adError = '' - this._loadPromiseResolve = null - this._loadPromiseReject = null - this._lastLoadTime = 0 - - const ad = this._ad = plus.ad.createFullScreenVideoAd(options) - ad.onLoad((e) => { - this._isLoad = true - this._lastLoadTime = Date.now() - this._dispatchEvent('load', {}) - - if (this._loadPromiseResolve != null) { - this._loadPromiseResolve() - this._loadPromiseResolve = null - } - }) - ad.onClose((e) => { - this._isLoad = false - this._dispatchEvent('close', { isEnded: e.isEnded }) - }) - ad.onError((e) => { - const { code, message } = e - const data = { code: code, errMsg: message } - this._adError = message - if (code === -5008) { - this._isLoad = false - } - this._dispatchEvent('error', data) - - if (this._loadPromiseReject != null) { - this._loadPromiseReject(data) - this._loadPromiseReject = null - } - }) - ad.onAdClicked((e) => { - this._dispatchEvent('adClicked', {}) - }) - } - - load () { - return new Promise((resolve, reject) => { - if (this._isLoad) { - resolve() - return - } - this._loadPromiseResolve = resolve - this._loadPromiseReject = reject - this._loadAd() - }) - } - - show () { - return new Promise((resolve, reject) => { - if (this._isLoad) { - this._ad.show() - resolve() - } else { - reject(new Error(this._adError)) - } - }) - } - - getProvider () { - return this._ad.getProvider() - } - - destroy () { - this._ad.destroy() - } - - _loadAd () { - this._isLoad = false - this._ad.load() - } - - _dispatchEvent (name, data) { - this._callbacks[name].forEach(callback => { - if (typeof callback === 'function') { - callback(data || {}) - } - }) + super(plus.ad.createFullScreenVideoAd(options), options) } } diff --git a/src/platforms/app-plus/service/api/ad/interstitial-ad.js b/src/platforms/app-plus/service/api/ad/interstitial-ad.js index 27425744935172b5b24764db024135dc183c4a62..adbc4143d06107b31aaf356488284e1902aa29d5 100644 --- a/src/platforms/app-plus/service/api/ad/interstitial-ad.js +++ b/src/platforms/app-plus/service/api/ad/interstitial-ad.js @@ -1,112 +1,12 @@ -const eventNames = [ - 'load', - 'close', - 'error', - 'adClicked' -] +import { + AdBase +} from './ad-base.js' -class InterstitialAd { +class InterstitialAd extends AdBase { constructor (options = {}) { - const _callbacks = this._callbacks = {} - eventNames.forEach(item => { - _callbacks[item] = [] - const name = item[0].toUpperCase() + item.substr(1) - this[`on${name}`] = function (callback) { - _callbacks[item].push(callback) - } - }) + super(plus.ad.createInterstitialAd(options), options) - this._isLoad = false - this._isLoading = false - this._adError = '' - this._loadPromiseResolve = null - this._loadPromiseReject = null - - const ad = this._ad = plus.ad.createInterstitialAd(options) - ad.onLoad((e) => { - this._isLoad = true - this._isLoading = false - this._dispatchEvent('load', {}) - - if (this._loadPromiseResolve != null) { - this._loadPromiseResolve() - this._loadPromiseResolve = null - } - }) - ad.onClose((e) => { - this._isLoad = false - this._isLoading = false - this._dispatchEvent('close', {}) - }) - ad.onError((e) => { - this._isLoading = false - - const { code, message } = e - const data = { code: code, errMsg: message } - this._adError = message - - this._dispatchEvent('error', data) - - if (this._loadPromiseReject != null) { - this._loadPromiseReject(data) - this._loadPromiseReject = null - } - }) - ad.onAdClicked((e) => { - this._dispatchEvent('adClicked', {}) - }) - } - - load () { - return new Promise((resolve, reject) => { - this._loadPromiseResolve = resolve - this._loadPromiseReject = reject - if (this._isLoading) { - return - } - if (this._isLoad) { - resolve() - return - } - this._loadAd() - }) - } - - show () { - return new Promise((resolve, reject) => { - if (this._isLoading) { - return - } - - if (this._isLoad) { - this._ad.show() - resolve() - } else { - reject(new Error(this._adError)) - } - }) - } - - getProvider () { - return this._ad.getProvider() - } - - destroy () { - this._ad.destroy() - } - - _loadAd () { - this._isLoad = false - this._isLoading = true - this._ad.load() - } - - _dispatchEvent (name, data) { - this._callbacks[name].forEach(callback => { - if (typeof callback === 'function') { - callback(data || {}) - } - }) + this.load() } } diff --git a/src/platforms/app-plus/service/api/media/choose-image.js b/src/platforms/app-plus/service/api/media/choose-image.js index fe724ce1454c6bb18b672359e455b23757eb07b2..e8f6d304974a0af0a0df093a1a560d8e20337027 100644 --- a/src/platforms/app-plus/service/api/media/choose-image.js +++ b/src/platforms/app-plus/service/api/media/choose-image.js @@ -30,7 +30,7 @@ function getFileInfo (filePath) { function compressImage (tempFilePath) { const dstPath = `${TEMP_PATH}/compressed/${Date.now()}_${getFileName(tempFilePath)}` - return new Promise((resolve, reject) => { + return new Promise((resolve) => { plus.nativeUI.showWaiting() plus.zip.compressImage({ src: tempFilePath, @@ -39,9 +39,9 @@ function compressImage (tempFilePath) { }, () => { plus.nativeUI.closeWaiting() resolve(dstPath) - }, (error) => { + }, () => { plus.nativeUI.closeWaiting() - reject(error) + resolve(tempFilePath) }) }) } diff --git a/src/platforms/app-plus/service/api/media/choose-video.js b/src/platforms/app-plus/service/api/media/choose-video.js index 19dfb184245a5677c860658f3ef9cd39e4c2af5d..4535e39a37a7bbe780fc105618509a4264e4b3cc 100644 --- a/src/platforms/app-plus/service/api/media/choose-video.js +++ b/src/platforms/app-plus/service/api/media/choose-video.js @@ -25,16 +25,20 @@ export function chooseVideo ({ function successCallback (tempFilePath = '') { const dst = `${TEMP_PATH}/compressed/${Date.now()}_${getFileName(tempFilePath)}` - const compressVideo = compressed ? plus.zip.compressVideo : function (_, callback) { - callback({ tempFilePath }) - } + const compressVideo = compressed ? new Promise((resolve) => { + plus.zip.compressVideo({ + src: tempFilePath, + dst + }, ({ tempFilePath }) => { + resolve(tempFilePath) + }, () => { + resolve(tempFilePath) + }) + }) : Promise.resolve() if (compressed) { plus.nativeUI.showWaiting() } - compressVideo({ - src: tempFilePath, - dst - }, ({ tempFilePath }) => { + compressVideo.then(tempFilePath => { if (compressed) { plus.nativeUI.closeWaiting() } @@ -50,12 +54,9 @@ export function chooseVideo ({ result.width = videoInfo.width result.height = videoInfo.height invoke(callbackId, result) - }, + }, errorCallback }) - }, error => { - plus.nativeUI.closeWaiting() - errorCallback(error) }) }