1
0
mirror of https://github.com/ndarilek/tts-rs.git synced 2024-11-17 12:19:37 +00:00

TTS seems to initialize now.

This commit is contained in:
Nolan Darilek 2020-12-29 14:10:39 -06:00
parent fc20431916
commit 965bea0adf
4 changed files with 26 additions and 6 deletions

View File

@ -41,4 +41,5 @@ wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = ["EventTarget", "SpeechSynthesis", "SpeechSynthesisErrorCode", "SpeechSynthesisErrorEvent", "SpeechSynthesisEvent", "SpeechSynthesisUtterance", "Window", ] }
[target.'cfg(target_os="android")'.dependencies]
jni = "0.18"
jni = "0.18"
ndk-glue = "0.2"

View File

@ -1,8 +1,15 @@
package rs.tts
import android.app.NativeActivity
import android.speech.tts.TextToSpeech
import android.speech.tts.TextToSpeech.OnInitListener
class MainActivity : NativeActivity(), OnInitListener {
override fun onInit(status:Int) {}
override fun onInit(status:Int) {
if(status == TextToSpeech.SUCCESS) {
println("Successfully initialized TTS!")
} else {
println("Failed to initialize TTS.")
}
}
}

View File

@ -62,5 +62,6 @@ fn run() -> Result<(), Error> {
#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
println!("xxxx In library");
run().expect("Failed to run");
}

View File

@ -1,6 +1,7 @@
#[cfg(target_os = "android")]
use std::sync::Mutex;
use jni::objects::JValue;
use lazy_static::lazy_static;
use log::info;
@ -11,25 +12,35 @@ lazy_static! {
}
#[derive(Clone, Debug)]
pub(crate) struct Android(BackendId);
pub(crate) struct Android {
id: BackendId,
}
impl Android {
pub(crate) fn new() -> Result<Self, Error> {
info!("Initializing Android backend");
let mut backend_id = NEXT_BACKEND_ID.lock().unwrap();
let bid = BackendId::Android(*backend_id);
let id = BackendId::Android(*backend_id);
*backend_id += 1;
let native_activity = ndk_glue::native_activity();
let vm_ptr = native_activity.vm();
let vm = unsafe { jni::JavaVM::from_raw(vm_ptr) }?;
let env = vm.attach_current_thread()?;
Ok(Self(bid))
let tts = env.new_object(
"android/speech/tts/TextToSpeech",
"(Landroid/content/Context;Landroid/speech/tts/TextToSpeech$OnInitListener;)V",
&[
native_activity.activity().into(),
native_activity.activity().into(),
],
)?;
Ok(Self { id })
}
}
impl Backend for Android {
fn id(&self) -> Option<BackendId> {
Some(self.0)
Some(self.id)
}
fn supported_features(&self) -> Features {