我一直在这里和github上看其他类似的问题,但未能找到基于类比的解决方案,所以也许这里有人可以提供帮助?
我正在尝试让以下插件在Android上运行:
var app = require("application");
var context = android.content.Context;
var TextToSpeech = android.speech.tts.TextToSpeech;
var initialised = false;
var tts;
var text_to_speech = {
speak : function(text, queue, pitch, speakRate, volume){
if(!tts || !initialised) {
tts = new TextToSpeech(context, new TextToSpeech.OnInitListener({
onInit : function(status) {
// some code here
}
}));
}
}
};
不幸的是,我在初始化tts对象时遇到异常:
JS: ORIGINAL STACKTRACE:
JS: Error: Cannot convert JavaScript object with id 683715827 at index 0
JS: at Error (native)
JS: at Object.text_to_speech.speak (/data/data/org.nativescript.CzystyDywan/files/app/tns_modules/nativescript-texttospeech/texttospeech.js:20:10)
JS: at TextToSpeech.speakText (/data/data/org.nativescript.CzystyDywan/files/app/pages/texttospeech/texttospeech.component.js:9:13)
JS: at DebugAppView._View_TextToSpeech0._handle_tap_4_0 (TextToSpeech.template.js:148:28)
JS: at Object.<anonymous> (/data/data/org.nativescript.CzystyDywan/files/app/tns_modules/@angular/core/src/linker/view.js:316:24)
JS: at ZoneDelegate.invoke (/data/data/org.nativescript.CzystyDywan/files/app/tns_modules/zone.js/dist/zone-node.js:281:29)
JS: at Object.NgZoneImpl.inner.inner.fork.onInvoke (/data/data/org.nativescript.CzystyDywan/files/app/tns_modules/@angular/core/src/zone/ng_zone_impl.js:45:41)
JS: at ZoneDelegate.invoke (/data/data/org.nativescript.CzystyDywan/files/app/tns_modules/zone.js/dist/zone-node.js:280:35)
JS: at Zone.runGuarded (/data/data/org.nativescript.CzystyDywan/files/app/tns_modules/zone.js/dist/zone-node.js:188:48)
JS: at Object.callback (/data/data/org.nativescript.CzystyDywan/files/app/tns_modules/zone.js/dist/zone-node.js:164:30)
JS: ERROR CONTEXT:
JS: [object Object]
W/System.err( 3701): at com.tns.Runtime.callJSMethodNative(Native Method)
W/System.err( 3701): at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:861)
W/System.err( 3701): at com.tns.Runtime.callJSMethodImpl(Runtime.java:726)
W/System.err( 3701): at com.tns.Runtime.callJSMethod(Runtime.java:712)
W/System.err( 3701): at com.tns.Runtime.callJSMethod(Runtime.java:693)
W/System.err( 3701): at com.tns.Runtime.callJSMethod(Runtime.java:683)
我知道这与TextToSpeech.OnInitListener接口的实现有关,但我真的不知道我在这里做错了什么。任何人都有类似的问题,可以分享一些提示吗?谢谢!
答案 0 :(得分:0)
没关系,我实际上构造了tts对象错误 - 而不是使用app.android.context我使用android.content.Context来获取构造函数的上下文。这是因为我为前者“未定义”。现在我正在使用这个辅助函数来获取当前上下文:
function _getContext() {
if (appModule.android.context) {
return (appModule.android.context);
}
var ctx = java.lang.Class.forName("android.app.AppGlobals").getMethod("getInitialApplication", null).invoke(null, null);
if (ctx) return ctx;
ctx = java.lang.Class.forName("android.app.ActivityThread").getMethod("currentApplication", null).invoke(null, null);
return ctx;
}