Capture (采集)音频图像视频 基于ngcordova

http://ngcordova.com/docs/plugins/capture/

This plugin allows you to record sound, video and images through the native capabilities of the device.

$ cordova plugin add org.apache.cordova.media-capture
module.controller('MyCtrl', function($scope, $cordovaCapture) {

  $scope.captureAudio = function() {
    var options = { limit: 3, duration: 10 };

    $cordovaCapture.captureAudio(options).then(function(audioData) {
      // Success! Audio data is here
    }, function(err) {
      // An error occurred. Show a message to the user
    });
  }

  $scope.captureImage = function() {
    var options = { limit: 3 };

    $cordovaCapture.captureImage(options).then(function(imageData) {
      // Success! Image data is here
    }, function(err) {
      // An error occurred. Show a message to the user
    });
  }

  $scope.captureVideo = function() {
    var options = { limit: 3, duration: 15 };

    $cordovaCapture.captureVideo(options).then(function(videoData) {
      // Success! Video data is here
    }, function(err) {
      // An error occurred. Show a message to the user
    });
  }

});


codorva可以捕捉音频、图片和视频。需要给phonegap项目增加安装Capture的plugin,cordova新版本更新后,其各种文档的更新速度貌似慢了一些,根据实践,可以采用以下步骤:

1、安装插件,插件地址:https://git-wip-us.apache.org/repos/asf/cordova-plugin-media-capture.git

在Node.js command prompt中执行:

1
cordova plugin add https: //git-wip-us .apache.org /repos/asf/cordova-plugin-media-capture .git

2、在安卓的app/AndroidManifest.xml配置文件中,增加权限:

1
2
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

这样就启动了录音和写文件到外部存储的权限。

3、经过以上两个步骤以后,可以写一些js代码测试录音功能了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
< html >
< head >
< title >Capture Audio</ title >
< meta  charset = "utf-8"  />
< script  type = "text/javascript"  src = "phonegap.js" ></ script >
< script  type = "text/javascript" >
  
// 采集操作成功完成后的回调函数 
function captureSuccess(mediaFiles) {
     var i, len = mediaFiles.length;
     for (i = 0, len = mediaFiles.length; i <  len ; i += 1) {
         doFile(mediaFiles[i]);
     }
}
 
// 采集操作出错后的回调函数 
function captureError(error) {
     var  msg  =  '录音出错: '  + error.code;
     alert(msg);
}
 
// “Capture Audio”按钮点击事件触发函数 
function captureAudio() {
 
     // 启动设备的音频录制应用程序,   
     navigator.device.capture.captureAudio(captureSuccess, captureError);
}
 
// 处理文件  
function doFile(mediaFile) {   
     path  =  mediaFile .fullPath,
     name  =  mediaFile .name;
     alert(path + name);
}
      
</script>
</ head >
< body >
     < button  onclick = "captureAudio();" >Capture Audio</ button >
</ body >
</ html >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值