起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 4844|回复: 9

[分享] 这两天写了一个拍摄视频预览并上传的功能

  [复制链接]

14

主题

55

帖子

205

积分

中级会员

Rank: 3Rank: 3

积分
205
QQ
发表于 2017-8-14 16:05:39 | 显示全部楼层 |阅读模式
这两天写了一个拍摄视频预览并上传的功能,使用了Vitamio-Cordova-Plugin-master和cordova-plugin-video-capture-plus-master插件。
Vitamio-Cordova-Plugin-master 主要用来预览和播放,cordova-plugin-video-capture-plus-master可以进行高清录像、设置录像时间限制。
目前只在安卓有限的手机上测试成功。

稍晚点上代码。

14

主题

55

帖子

205

积分

中级会员

Rank: 3Rank: 3

积分
205
QQ
 楼主| 发表于 2017-8-15 03:01:56 | 显示全部楼层
插件下载地址:
https://github.com/nchutchind/Vitamio-Cordova-Plugin(Vitamio-Cordova-Plugin-master)
https://github.com/danielsogl/cordova-plugin-video-capture-plus(cordova-plugin-video-capture-plus-master)

下载插件,放到\model\Native\plugins目录
我把插件的id修改了,分别为
Cordova.Plugin.Vitamio 和Cordova.Plugin.Video
引入插件

  1.         require("$UI/system/lib/cordova/cordova");

  2.         require("cordova!cordova-plugin-file");
  3.         require("cordova!cordova-plugin-file-transfer");
  4.         require("cordova!<span style="background-color: rgb(255, 255, 255);">Cordova.Plugin.Vitamio</span>");
  5.         require("cordova!<span style="background-color: rgb(255, 255, 255);">Cordova.Plugin.Video</span>");
复制代码
处理录像和预览播放还引入文件上传的插件


录像

  1.         Model.prototype.onVideo = function(event) {
  2.                 var self = this;var duration = 20;
  3.                 var highquality = true;
  4.                 var frontcamera = true;
  5.                 window.plugins.videocaptureplus.captureVideo(captureSuccess, captureError, {
  6.                         limit : 1, // the nr of videos to record, default 1 (on iOS always 1)
  7.                         duration : duration, // max duration in seconds, default 0, which is 'forever'
  8.                         highquality : highquality, // set to true to override the default low quality setting
  9.                         frontcamera : frontcamera, // set to true to override the
  10.                         // default backfacing camera
  11.                         // setting. iOS: works fine,
  12.                         // Android: YMMV (#18)
  13.                         // you'll want to sniff the useragent/device and pass the best
  14.                         // overlay based on that.. assuming iphone here
  15.                         portraitOverlay : '', // put the png in your www folder
  16.                         landscapeOverlay : '', // not passing an overlay means no image is shown for the landscape orientation
  17.                         overlayText : '' // iOS only
  18.                 });

  19.                 function captureSuccess(mediaFiles) {
  20.                         var i, len;
  21.                         for (i = 0, len = mediaFiles.length; i < len; i++) {
  22.                                 var mediaFile = mediaFiles[i];
复制代码
设置了限制视频时间最长20秒,录像为高清,可以使用前置摄像头。录入的视频信息存入data组件里。 需要在model里增加 this.video = '';用来判断是否已有视频文件。由于业务需求需要视频图片一起上传,这里用data组件存文件信息,如果只是上传一个视频直接使用this.video即可。


视频预览

  1.         Model.prototype.onVideoPlay = function() {
  2.                 var video = this.video;
  3.                 if (video == '') {
  4.                         return;
  5.                 }
  6.                 // Play a pre-recorded video with callbacks
  7.                 var options = {
  8.                         isStreaming : false,
  9.                         successCallback : function(obj) {
  10.                                 // console.log("Video was closed without error at " + obj.pos + ".");
  11.                         },
  12.                         progressCallback : function(obj) {
  13.                                 // obj.action may be "start", "stop", or "pause"
  14.                                 // obj.pos is the current time in the clip (only available for
  15.                                 // non-continuous streams)
  16.                                 // and shows a format of "00:12:34"
  17.                                 // myAnalytics.track(obj.action, obj.pos);
  18.                         },
  19.                         errorCallback : function(obj) {
  20.                                 console.log("Error! " + obj.message);
  21.                         }
  22.                 };
  23.                 window.plugins.vitamio.playVideo(video.fullPath, options);
  24.         }
复制代码
这里没有设置progressCallback 所以播放完后就会自动关闭。


视频上传
  1. Model.prototype.uploadFile = function() {
  2.                 var self = this;
  3. fileInfo = self .video;
  4.                 var options = new FileUploadOptions();
  5.                 options.fileKey = 'video'; // 用于设置参数
  6.                 options.fileName = fileInfo.name; //文件名称
  7.                 // 文件格式,如果是图片格式,就用image/jpeg,其他文件格式上官网查API
  8.                 options.mimeType = fileInfo.type;
  9.                 // 这里的uri根据自己的需求设定,是一个接收上传文件的地址
  10.                 var uri = encodeURI(dp.apiUrl('gameUpdate'));
  11.                 options.chunkedMode = false;
  12.                 var params = new Object();
  13.                 params.token = token;
  14.                                 options.params = params;
  15.                 var ft = new FileTransfer();
  16.                 function success(result) {
  17. //上传成功
  18.                 }
  19.                 function fail(message) {
  20.                         //alert("失败:" + JSON.stringify(message));
  21. //上传失败
  22.                 }
  23.                 ft.upload(fileInfo.filePath, uri, success, fail, options);
  24.         }
复制代码
dp.apiUrl('gameUpdate') 是我自己封装的api接口列表里取的url。 token是接口的登录令牌,我是model前面var的,可改成其他跟文件一起提交到接口的参数。


删除按钮
  1. Model.prototype.onFileDel = function(event) {
复制代码




html部分代码
  1. <blockquote><?xml version="1.0" encoding="utf-8"?>
复制代码


css部分
  1. <blockquote>.x-titlebar-right .glyphicon{ color:#fff; font-size:20px; padding:0 10px;}
复制代码



完整的js
  1. define(function(require) {
  2.         var $ = require("jquery");
  3.         var justep = require("$UI/system/lib/justep");
  4.         require("$UI/system/lib/cordova/cordova");
  5.         require("cordova!cordova-plugin-file");
  6.         require("cordova!cordova-plugin-file-transfer");
  7.         require("cordova!Cordova.Plugin.Vitamio");
  8.         require("cordova!Cordova.Plugin.Video");

  9.         var token = 'H1234567890';
  10.         var Model = function() {
  11.                 this.video = '';
  12.                 this.callParent();
  13.         };
  14.         //拍摄视频
  15.         Model.prototype.onVideo = function(event) {
  16.                 var self = this;
  17.                 var duration = 20;
  18.                 var highquality = true;
  19.                 var frontcamera = true;
  20.                 window.plugins.videocaptureplus.captureVideo(captureSuccess, captureError, {
  21.                         limit : 1, // the nr of videos to record, default 1 (on iOS always 1)
  22.                         duration : duration, // max duration in seconds, default 0, which is 'forever'
  23.                         highquality : highquality, // set to true to override the default low quality setting
  24.                         frontcamera : frontcamera, // set to true to override the default backfacing camera
  25.                         // setting. iOS: works fine,
  26.                         // Android: YMMV (#18)
  27.                         // you'll want to sniff the useragent/device and pass the best
  28.                         // overlay based on that.. assuming iphone here
  29.                         portraitOverlay : '', // put the png in your www folder
  30.                         landscapeOverlay : '', // not passing an overlay means no image is
  31.                         // shown for
  32.                         // the landscape orientation
  33.                         overlayText : '' // iOS only
  34.                 });

  35.                 function captureSuccess(mediaFiles) {
  36.                         var i, len;
  37.                         for (i = 0, len = mediaFiles.length; i < len; i++) {
  38.                                 var mediaFile = mediaFiles[i];
  39.                                 self.video = mediaFile;
  40.                                 var fileData = self.comp("fileData");
  41.                                 var data = {
  42.                                         "fileType" : "video",
  43.                                         "fileInfo" : [ mediaFile ]
  44.                                 }
  45.                                 fileData.loadData([ data ]);
  46.                         }
  47.                 }

  48.                 function captureError(error) {
  49.                         // code 3 = cancel by user
  50.                         // alert('Returncode: ' + JSON.stringify(error.code));
  51.                 }
  52.         };
  53.         //播放视频
  54.         Model.prototype.onVideoPlay = function() {
  55.                 var video = this.video;
  56.                 if (video == '') {
  57.                         return;
  58.                 }
  59.                 // Play a pre-recorded video with callbacks
  60.                 var options = {
  61.                         isStreaming : false,
  62.                         successCallback : function(obj) {
  63.                                 // console.log("Video was closed without error at " + obj.pos + ".");
  64.                         },
  65.                         progressCallback : function(obj) {
  66.                                 // obj.action may be "start", "stop", or "pause"
  67.                                 // obj.pos is the current time in the clip (only available for non-continuous streams)
  68.                                 // and shows a format of "00:12:34"
  69.                                 // myAnalytics.track(obj.action, obj.pos);
  70.                         },
  71.                         errorCallback : function(obj) {
  72.                                 console.log("Error! " + obj.message);
  73.                         }
  74.                 };
  75.                 window.plugins.vitamio.playVideo(video.fullPath, options);
  76.         }
  77.         //上传文件
  78.         Model.prototype.uploadFile = function() {
  79.                 var self = this;
  80.                 fileInfo = self.video;
  81.                 var options = new FileUploadOptions();
  82.                 options.fileKey = 'video'; // 用于设置参数
  83.                 options.fileName = fileInfo.name; // 文件名称
  84.                 // 文件格式,如果是图片格式,就用image/jpeg,其他文件格式上官网查API
  85.                 options.mimeType = fileInfo.type;
  86.                 // 这里的uri根据自己的需求设定,是一个接收上传文件的地址
  87.                 var uri = encodeURI('http://www.shiav.com/upload');
  88.                 options.chunkedMode = false;
  89.                 var params = new Object();
  90.                 params.token = token;
  91.                 options.params = params;
  92.                 var ft = new FileTransfer();
  93.                 function success(result) {
  94.                         // 上传成功
  95.                 }
  96.                 function fail(message) {
  97.                         // alert("失败:" + JSON.stringify(message));
  98.                         // 上传成功
  99.                 }
  100.                 ft.upload(fileInfo.filePath, uri, success, fail, options);
  101.         }
  102.         //删除按钮
  103.         Model.prototype.onFileDel = function(event) {
  104.                 var parentRow = event.bindingContext.$parent;
  105.                 this.comp("fileData").deleteData(parentRow)
  106.         }
  107.         return Model;
  108. });
复制代码




在苹果手和其他大部分安卓手机没有做测试,只是部分安卓手机测试通过。

回复 支持 反对

使用道具 举报

88

主题

464

帖子

763

积分

高级会员

Rank: 4

积分
763
QQ
发表于 2017-8-15 03:06:36 | 显示全部楼层
eye215 发表于 2017-8-15 03:01
插件下载地址:
https://github.com/nchutchind/Vitamio-Cordova-Plugin(Vitamio-Cordova-Plugin-master) ...

楼主哈    能否做个demo出来分享下,,attachment也能录视频,,不能用么
回复 支持 反对

使用道具 举报

41

主题

837

帖子

2271

积分

金牌会员

搬砖工

Rank: 6Rank: 6

积分
2271
QQ
发表于 2017-8-15 08:52:52 | 显示全部楼层
顶!不过 我告诉你在ioS上无法实现
Wex5开发者,欢迎互相交流学习
交流群:30057529


我要成为代码女神
回复 支持 反对

使用道具 举报

2

主题

19

帖子

27

积分

新手上路

Rank: 1

积分
27
QQ
发表于 2017-8-15 09:00:53 | 显示全部楼层
我能告诉你,拍出来的录像文件太大,上传根本不现实吗?随便一拍就是几十兆
回复 支持 反对

使用道具 举报

718

主题

2841

帖子

5657

积分

论坛元老

Rank: 8Rank: 8

积分
5657
QQ
发表于 2017-8-15 09:05:28 | 显示全部楼层
支持
WEX5初学者,欢迎初学者交流
QQ:597558229
tel:15857336322
回复

使用道具 举报

14

主题

55

帖子

205

积分

中级会员

Rank: 3Rank: 3

积分
205
QQ
 楼主| 发表于 2017-8-15 09:23:29 | 显示全部楼层
q2045377 发表于 2017-8-15 03:06
楼主哈    能否做个demo出来分享下,,attachment也能录视频,,不能用么

attachment 我这里一直不好用
回复 支持 反对

使用道具 举报

2

主题

19

帖子

27

积分

新手上路

Rank: 1

积分
27
QQ
发表于 2017-8-15 09:24:35 | 显示全部楼层
求一个视频拍摄比较小,像微信那样,比较实用的的方案。。。。。现有cordova的插件拍出来太大了。。。。不具备上传的可行性。。。。
目前我用recordRTC的方案拍出来还可以,就是兼容性太差
回复 支持 反对

使用道具 举报

14

主题

55

帖子

205

积分

中级会员

Rank: 3Rank: 3

积分
205
QQ
 楼主| 发表于 2017-8-15 11:13:19 | 显示全部楼层
cucu1978 发表于 2017-8-15 09:24
求一个视频拍摄比较小,像微信那样,比较实用的的方案。。。。。现有cordova的插件拍出来太大了。。。。不 ...

找个压缩的插件或方法么,刚接触视频类的
我也想知道
回复 支持 反对

使用道具 举报

14

主题

55

帖子

205

积分

中级会员

Rank: 3Rank: 3

积分
205
QQ
 楼主| 发表于 2017-8-16 17:24:32 | 显示全部楼层
cucu1978 发表于 2017-8-15 09:24
求一个视频拍摄比较小,像微信那样,比较实用的的方案。。。。。现有cordova的插件拍出来太大了。。。。不 ...

微信是控制的视频的比特率的,不知道有没有cordova可以实现的
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2024-5-1 16:50 , Processed in 0.076085 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表