04. 列舉 Cordova 的一些插件

cordova-plugin-media-capture 可用于拍視頻

cordova plugin add cordova-plugin-media-capture

js 調(diào)用片段

function videoCapture() {
               //開始錄像(最長錄制時(shí)間:15秒)
               navigator.device.capture.captureVideo(onSuccess, onError, {duration: 15});
 
               //錄制成功
               function onSuccess(mediaFiles) {
                  var i, path, len;
                  //遍歷獲取錄制的文件(iOS只支持一次錄制一個(gè)視頻或音頻)
                  for (i = 0, len = mediaFiles.length; i < len; i += 1) {
                    console.log(mediaFiles);
                     path = mediaFiles[i].fullPath;
                     alert("錄制成功!\n\n"
                          + "文件名:" + mediaFiles[i].name + "\n"
                          + "大?。? + mediaFiles[i].size + "\n\n"
                          + "localURL地址:" + mediaFiles[i].localURL + "\n\n"
                          + "fullPath地址:" + path);
                  }
               }
 
               //錄制失敗
               function onError(error) {
                  alert('錄制失?。″e(cuò)誤碼:' + error.code);
               }
          }

captureVideo參數(shù)說明

captureVideo(
            onSuccess: (mediaFiles: MediaFile[]) => void,
            onError: (error: CaptureError) => void,
            options?: VideoOptions): void ;

---------------------------

/** Encapsulates properties of a media capture file. */
interface MediaFile {
    /** The name of the file, without path information. */
    name: string;
    /** The full path of the file, including the name. */
    fullPath: string;
    /** The file's mime type */
    type: string;
    /** The date and time when the file was last modified. */
    lastModifiedDate: Date;
    /** The size of the file, in bytes. */
    size: number;
    /**
     * Retrieves format information about the media capture file.
     * @param successCallback Invoked with a MediaFileData object when successful.
     * @param errorCallback   Invoked if the attempt fails, this function. 
     */
    getFormatData(
        successCallback: (data: MediaFileData) => void,
        errorCallback?: () => void): void;
}

---------------------------

/** Encapsulates video capture configuration options. */
interface VideoOptions {
    /**
     * The maximum number of video clips the device's user can capture in a single
     * capture operation. The value must be greater than or equal to 1.
     */
    limit?: number;
    /** The maximum duration of a video clip, in seconds. */
    duration?: number;
}

navigator.device.capture.captureVideo 輸出的log

fullPath: "file:///storage/emulated/0/DCIM/Camera/VID_20190601_120049.mp4"
lastModified: null
lastModifiedDate: 1559361649000
localURL: "cdvfile://localhost/sdcard/DCIM/Camera/VID_20190601_120049.mp4"
name: "VID_20190601_120049.mp4"
size: 2092530
start: 0
type: "video/mp4"

cordova-plugin-video-editor 用于本地壓縮視頻

Installation 安裝

cordova plugin add cordova-plugin-video-editor

this example uses the cordova media capture plugin

navigator.device.capture.captureVideo(
    videoCaptureSuccess,
    videoCaptureError,
    {
        limit: 1,
        duration: 20
    }
);
 
function videoCaptureSuccess(mediaFiles) {
    // Wrap this below in a ~100 ms timeout on Android if
    // you just recorded the video using the capture plugin.
    // For some reason it is not available immediately in the file system.
 
    var file = mediaFiles[0];
    var videoFileName = 'video-name-here'; // I suggest a uuid
 
    VideoEditor.transcodeVideo(
        videoTranscodeSuccess,
        videoTranscodeError,
        {
            fileUri: file.fullPath,
            outputFileName: videoFileName,
            outputFileType: VideoEditorOptions.OutputFileType.MPEG4,
            optimizeForNetworkUse: VideoEditorOptions.OptimizeForNetworkUse.YES,
            saveToLibrary: true,
            maintainAspectRatio: true,
            width: 640,
            height: 640,
            videoBitrate: 1000000, // 1 megabit
            audioChannels: 2,
            audioSampleRate: 44100,
            audioBitrate: 128000, // 128 kilobits
            progress: function(info) {
                console.log('transcodeVideo progress callback, info: ' + info);
            }
        }
    );
}
 
function videoTranscodeSuccess(result) {
    // result is the path to the transcoded video on the device
    console.log('videoTranscodeSuccess, result: ' + result);
}
 
function videoTranscodeError(err) {
    console.log('videoTranscodeError, err: ' + err);
}

cordova-plugin-video-editor 源碼

npm i cordova-plugin-video-editor

我建議將 maintainAspectRatio 設(shè)置為 true。 如果此選項(xiàng)為真,則可以提供任何寬度 / 高度,并且提供的高度將用于計(jì)算輸出視頻的新寬度。 如果您將 maintainAspectRatio 設(shè)置為 false,那么您很有可能會得到拉伸和 / 或扭曲的視頻。 當(dāng) maintainAspectRatio 為 true-時(shí),下面是 iOS 上使用的簡化公式

aspectRatio = videoWidth / videoHeight;
outputWidth = height * aspectRatio;
outputHeight = outputWidth / aspectRatio;

默認(rèn)輸出路徑為: /storage/emulated/0/Movies/HelloCordova

cordova-plugin-zip 插件

https://www.npmjs.com/package/cordova-plugin-zip

cordova plugin add cordova-plugin-zip

壓縮包中的文件必須不包含中文名, 否則返回值為-1, 正確的是返回0

function unZip() {
            var sourceZip = cordova.file.externalDataDirectory + 'abc.zip';
            var destinationDir = cordova.file.externalDataDirectory + 'haha';
            zip.unzip(sourceZip, destinationDir, function (result) {
                // 0: OK   -1: not OK
                console.log(result);
            },
            function (progressEvent) {
                progress = progressEvent.loaded / progressEvent.total;
                console.log(progress);
            });
}

cordova-plugin-dialogs 插件

navigator.notification.confirm

File 插件

可用于文件下載

   function downloadFile() {
            var url = 'https://upload-images.jianshu.io/upload_images/1662509-d5a04be8dd1167b4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp';
            var path = cordova.file.externalDataDirectory + 'ddd.png';
            var fileTransfer = new FileTransfer();
            fileTransfer.download(url, path, function (entry) { console.log(entry) },
                function (error) { console.log(error) }, true);
        }

參考

Cordova - fileTransfer插件的使用詳解(實(shí)現(xiàn)文件上傳、下載)
http://www.hangge.com/blog/cache/detail_1180.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容