cordova 录音 &录像(转)

1,插件的安装
首先我们要在“终端”中进入工程所在的目录,然后运行如下命令:
1
cordova plugin add cordova-plugin-media-capture

2,录音功能
下面样例,点击“开始录音”按钮后则系统会打开录音器进行录音。录音完毕后,输出录音文件的保存地址。
可以通过 duration 属性参数控制音频录制长度,如果到达这个时长也会停止录音。
        原文:Cordova - MediaCapture插件的使用(录制视频、录音)         原文:Cordova - MediaCapture插件的使用(录制视频、录音)
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
<!DOCTYPE html>
<html>
    <head>
        <title>Capture Audio</title>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
        <script type="text/javascript" charset="utf-8">
 
            function audioCapture() {
               //开始录音(最长录制时间:15秒)
               navigator.device.capture.captureAudio(onSuccess, onError, {duration: 15});
 
               //录制成功
               function onSuccess(mediaFiles) {
                  var i, path, len;
                  //遍历获取录制的文件(iOS只支持一次录制一个视频或音频)
                  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('录制失败!错误码:' + error.code);
               }
          }
        </script>
    </head>
    <body style="padding-top:50px">
        <button style="font-size:23px;" onclick="audioCapture();">开始录音</button>
    </body>
</html>

3,录像功能
下面样例,点击“开始录像”按钮后则系统会打开摄像头进行拍摄视频。录像完毕后,输出录像文件的保存地址。
可以通过 duration 属性参数控制视频录制长度,当到达这个长度也会停止录制。
        原文:Cordova - MediaCapture插件的使用(录制视频、录音)         原文:Cordova - MediaCapture插件的使用(录制视频、录音)
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
<!DOCTYPE html>
<html>
    <head>
        <title>Capture Video</title>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
        <script type="text/javascript" charset="utf-8">
 
            function videoCapture() {
               //开始录像(最长录制时间:15秒)
               navigator.device.capture.captureVideo(onSuccess, onError, {duration: 15});
 
               //录制成功
               function onSuccess(mediaFiles) {
                  var i, path, len;
                  //遍历获取录制的文件(iOS只支持一次录制一个视频或音频)
                  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('录制失败!错误码:' + error.code);
               }
          }
        </script>
    </head>
    <body style="padding-top:50px">
        <button style="font-size:23px;" onclick="videoCapture();">开始录像</button>
    </body>
</html>

 转载请保留原文链接:http://www.hangge.com/blog/cache/detail_1186.html
posted @ 2017-08-02 11:21  rainMicro  阅读(1341)  评论(0编辑  收藏  举报