简体   繁体   English

使用Cordova在iOS中创建和捕获视频

[英]Creating and capturing video in iOS with cordova

I want to capture a video. 我想捕捉视频。 After that, i want to play the video after clicking a button. 之后,我想在单击按钮后播放视频。 I'm working with cordova for a iOS and Android app. 我正在与Cordova合作开发iOS和Android应用程序。 I'm working with the cordova-plugin-media-capture and cordova-plugin-streaming-media plugins. 我正在使用cordova-plugin-media-capturecordova-plugin-streaming-media插件。

Caputring the video works finde. 给视频作品添加字幕。 But if i'm clicking on the "Play Video" button, i get an error in console: 但是,如果我单击“播放视频”按钮,则会在控制台中出现错误:

ReferenceError: Can't find variable: playVideo ReferenceError:找不到变量:playVideo

Whats wrong? 怎么了? Here is my function: 这是我的功能:

//cordova media caputre plugin
document.addEventListener("deviceready", init, false);
function init() {

    document.querySelector("#takeVideo").addEventListener("touchend", function() {
        console.log("Take video");
        navigator.device.capture.captureVideo(captureSuccess, captureError, {limit: 1});
    }, false);

}

function captureError(e) {
    console.log("capture error: "+JSON.stringify(e));
}

// capture callback
var captureSuccess = function(mediaFiles) {
    var i, path, len;
    for (i = 0, len = mediaFiles.length; i < len; i += 1) {
        path = mediaFiles[i].fullPath;
        console.log(mediaFiles[i].fullPath);

          function playVideo(videoUrl) {
              // Play a video with callbacks
              var options = {
                mustWatch: true,
                successCallback: function() {
                  console.log("Video was closed without error.");
                },
                errorCallback: function(errMsg) {
                  console.log("Error! " + errMsg);
                }
              };
              window.plugins.streamingMedia.playVideo(path, options);

          }

    }
};

My buttons (HTML) 我的按钮(HTML)

<button id="takeVideo">Take Video</button>

<input type="url" size="60" value="" id="vidUrl"/><br/>
<button type="button" onclick="playVideo(document.getElementById('vidUrl').value);">Play Video</button>

Edit 编辑

Here is my attempt at adjusting your code to work: 这是我尝试调整您的代码以使其正常工作的尝试:

JavaScript; JavaScript;

document.addEventListener("deviceready", init, false);

function init() {

    document.getElementById("takeVideo").addEventListener("touchend", function() {

        console.log("Take video");

        navigator.device.capture.captureVideo(captureSuccess, captureError, {
            limit: 1
        });

    }, false);

    document.getElementById("playButton").addEventListener("touchend", function() {

        playVideo(document.getElementById('vidUrl').innerHTML);

    });

}

function captureError(e) {

    console.log("capture error: " + JSON.stringify(e));
}


function captureSucess(mediaFile) {

    //No need for a loop as we have limited the user to take 1 video
    var path = mediaFile.fullPath;
    var vidUrlElement = document.getElementById('vidUrl');
    vidUrlElement.innerHTML = path;


}

function playVideo(videoUrl) {
    // Play a video with callbacks
    var options = {
        mustWatch: true,
        successCallback: function() {
            console.log("Video was closed without error.");
        },
        errorCallback: function(errMsg) {
            console.log("Error! " + errMsg);
        }
    };
    window.plugins.streamingMedia.playVideo(videoUrl, options);

}

HTML; HTML;

<button id="takeVideo">Take Video</button>
<br/>
<div id="vidUrl"></div>
<br/>
<button id="playButton">Play Video</button>

Original 原版的

Have you tried moving the playVideo() function outside of the for loop? 您是否尝试过将playVideo()函数移至for循环之外?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM