我正在尝试为Cordova 3.4创建自己的Android插件。
该插件必须简单地调用Android mediaRecorder,从麦克风录制7秒钟,在SD卡上创建一个文件并停止录制。
当我尝试从Cordova / Ionic项目中调用插件时,我不断得到“未找到类”错误。
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;
}
}
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;
插件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>
按如下方式调用:
var onSuccess = function(dataReturned){
console.log(dataReturned);
}
var onFailure = function(dataReturned){
console.log(dataReturned);
}
RecordAudio.recordAudio(onSuccess, onFailure);
任何人都可以看到导致此错误的Java有什么问题吗?
非常感谢任何反馈!!!
答案 0 :(得分:0)
您是否已将插件添加到config.xml文件中?
它应该有这样的东西
<feature name="Recorder" >
<param name="android-package" value="com.example.myplugin.Recorder " />
</feature>
如果是,请提供堆栈跟踪以获取更多信息。
EDIT1: 刚看到plugin.xml ..我认为它已被删除,因为我只能通过将代码放在Config.xml中来使我的插件正常工作