简体   繁体   English

科尔多瓦3.4创建自定义插件

[英]cordova 3.4 create custom plugin

I am trying to create my own Android plugin for Cordova 3.4. 我正在尝试为Cordova 3.4创建自己的Android插件。

The plugin must simply call the Android mediaRecorder, record from the mic for 7 seconds, create a file on the SD card and stop recording. 该插件只需调用Android mediaRecorder,从麦克风录制7秒钟,在SD卡上创建一个文件,然后停止录制。

When I try and call the plugin from inside my Cordova / Ionic project I keep getting a "Class not found" error without further elaboration. 当我尝试从我的Cordova / Ionic项目中调用该插件时,我不断收到“找不到类”错误,无需进一步说明。

The Java looks as follows: Java如下所示:

    package com.example.myplugin;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;

public class Recorder extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
        String outputFile = null;
        final MediaRecorder myRecorder;

        outputFile = Environment.getExternalStorageDirectory().
                  getAbsolutePath() + "/TestRecording.m4a";

        myRecorder = new MediaRecorder();
        myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        myRecorder.setAudioSamplingRate(44100);
        myRecorder.setAudioChannels(1);
        myRecorder.setAudioEncodingBitRate(32000);
        myRecorder.setOutputFile(outputFile);

        try 
        {
              myRecorder.prepare();
              myRecorder.start();
        }
        catch (Exception e) {
            callbackContext.error(e.getMessage());   
            return false;   
        }

        CountDownTimer countDowntimer = new CountDownTimer(7000, 1000) {
        public void onTick(long millisUntilFinished) {}

        public void onFinish() {
            myRecorder.stop();
            myRecorder.release();           

            //Sets the callback type and sets the return to true to define successful.      
            callbackContext.success();
            //return true;
        }
        };

        countDowntimer.start();

        return true;

    }

    }

The JS looks as follows: JS如下所示:

    var RecordAudio = {
    recordAudio: function(successCallback, errorCallback) {
        cordova.exec(
            successCallback, // success callback function
            errorCallback, // error callback function
            'Recorder', // mapped to our native Java class called "Recorder"
            'recordAudio', // with this action name
            []                  //Array of arguments to pass
        ); 
    }
};

    module.exports = RecordAudio;

The plugin XML is as follows: 插件XML如下:

    <?xml version="1.0" encoding="UTF-8"?>

<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
           id="com.mytest.recorder"
      version="0.1.0">
  <name>Recorder</name>

  <description>Android Recorder plugin</description>        


    <js-module src="www/Recorder.js" name="Recorder">   
        <clobbers target="window.RecordAudio" />     
    </js-module>

    <!-- android -->
    <platform name="android">
        <config-file target="res/xml/config.xml" parent="/*">
            <feature name="Recorder">
                <param name="android-package" value="Recorder"/>
            </feature>
        </config-file>

        <source-file src="src/Recorder.java" target-dir="com/mytest/recorder/" />      
     </platform>          
</plugin>

calling it as follows: 调用它如下:

var onSuccess = function(dataReturned){
      console.log(dataReturned);

    }

    var onFailure = function(dataReturned){
      console.log(dataReturned);

    }

    RecordAudio.recordAudio(onSuccess, onFailure);

Can anyone see what's wrong with the Java that would cause this error? 谁能看到导致问题的Java错误吗?

Many thanks for any feedback!!! 非常感谢您的任何反馈!!!

have you added the Plugin to config.xml file ?? 您是否已将插件添加到config.xml文件中?

It should have something like this 应该有这样的东西

<feature name="Recorder" >
        <param name="android-package" value="com.example.myplugin.Recorder " />
</feature>

If yes, please provide the stack trace for better information. 如果是,请提供堆栈跟踪以获取更多信息。

EDIT1: Just saw the plugin.xml.. i think it has been removed, coz i could only get my plugin working by putting the code in Config.xml EDIT1:刚刚看到了plugin.xml ..我认为它已被删除,因为我只能通过将代码放入Config.xml来使我的插件正常工作

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

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