unity直接调用android方法,调用TextToSpeechBeta需要引入com.goole.tts包。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IBAndroidTTS
{
public delegate void DebugLog(string str);
public static event DebugLog DebugHandle;
#if UNITY_ANDROID
public class TextToSpeechInitListener : AndroidJavaProxy
{
public delegate void TTSInitDelegate(int status);
private TTSInitDelegate m_TTSInitDelegate;
public TextToSpeechInitListener() : base("android.speech.tts.TextToSpeech$OnInitListener")
{
}
private void onInit(int status)
{
if (m_TTSInitDelegate != null)
{
m_TTSInitDelegate(status);
}
else
{
Debug.LogError("Init failed. Please call 'SetInitDelegate(TTSInitDelegate mTTSInitDelegate)' first");
}
}
public void SetInitDelegate(TTSInitDelegate TTSInitDelegate)
{
m_TTSInitDelegate = TTSInitDelegate;
}
}
public class TextToSpeechUtteranceCompletedListener : AndroidJavaProxy
{
public delegate void TTSUtteranceCompletedDelegate(string utteranceId);
private TTSUtteranceCompletedDelegate m_TTSUtteranceCompletedDelegate;
public TextToSpeechUtteranceCompletedListener() : base("android.speech.tts.TextToSpeech$OnUtteranceCompletedListener")
{
}
void onUtteranceCompleted(string utteranceId)
{
if (m_TTSUtteranceCompletedDelegate != null)
{
m_TTSUtteranceCompletedDelegate(utteranceId);
}
else
{
if (DebugHandle != null)
{
DebugHandle("Init failed. Please call 'SetInitDelegate(TTSUtteranceCompletedDelegate m_TTSUtteranceCompletedDelegate)' first");
}
}
}
public void SetUtteranceCompletedDelegate(TTSUtteranceCompletedDelegate TTSUtteranceCompletedDelegate)
{
m_TTSUtteranceCompletedDelegate = TTSUtteranceCompletedDelegate;
}
}
#region singleton
private static object m_locker = new object();
protected static IBAndroidTTS m_instance;
public static IBAndroidTTS Instance
{
get
{
if (m_instance == null)
{
lock (m_locker)
{
if (m_instance == null)
{
m_instance = new IBAndroidTTS();
m_instance.Init();
}
}
}
return m_instance;
}
}
#endregion
#region TextToSpeech STATE
const int TextToSpeech_SUCCESS = 0;
const int TextToSpeech_LANG_MISSING_DATA = -1;
const int TextToSpeech_LANG_NOT_SUPPORTED = -2;
const int TextToSpeech_QUEUE_FLUSH = 0;
#endregion
#region Android TextToSpeech
//AndroidJavaClass
private AndroidJavaClass Locale;
private AndroidJavaClass UnityPlayer;
//AndroidJavaObject
private AndroidJavaObject m_currentActivity;
private AndroidJavaObject m_androidTTS;
private TextToSpeechInitListener m_initListener;
private TextToSpeechUtteranceCompletedListener m_utteranceCompletedListener;
private void Init()
{
Locale = new AndroidJavaClass("java.util.Locale");
m_initListener = new TextToSpeechInitListener();
m_initListener.SetInitDelegate(OnInitTTSEngine);
UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
m_currentActivity = UnityPlayer.GetStatic("currentActivity");
m_androidTTS = new AndroidJavaObject("android.speech.tts.TextToSpeech", m_currentActivity, m_initListener);
m_utteranceCompletedListener = new TextToSpeechUtteranceCompletedListener();
m_utteranceCompletedListener.SetUtteranceCompletedDelegate(OnUtteranceCompleted);
int result = m_androidTTS.Call("setOnUtteranceCompletedListener", m_utteranceCompletedListener);
if (DebugHandle != null)
{
DebugHandle("setComplete" + result);
}
}
#endregion
private bool m_isChinese = false;
public bool IsChinese
{
get
{
return m_isChinese;
}
}
private void OnInitTTSEngine(int status)
{
if (status == TextToSpeech_SUCCESS)
{
int result = m_androidTTS.Call("setLanguage", Locale.GetStatic("CHINESE"));
if (result != TextToSpeech_SUCCESS)
{
string error = "Chinese not supported, English Mode Start Instead";
if (DebugHandle != null)
{
DebugHandle(error);
}
//error.ShowToast();
m_androidTTS.Call("setLanguage", Locale.GetStatic("US"));
}
else
{
m_isChinese = true;
}
}
}
private void OnUtteranceCompleted(string utteranceId)
{
if (DebugHandle != null)
{
DebugHandle(utteranceId);
}
}
public void Speak(string str)
{
if (IsDone() == true)
{
m_androidTTS.Call("speak", new AndroidJavaObject("java.lang.String", CSharpString), TextToSpeech_QUEUE_FLUSH, null, str);
}
}
public bool IsDone()
{
return !IsSpeaking();
}
public void Stop()
{
m_androidTTS.Call("stop");
}
public void SetVolume(int volume)
{
//m_androidTTS.Call("", volume);
}
public void SetRate(float rate)
{
m_androidTTS.Call("setSpeechRate", rate);
}
public void SetPitch(float pitch)
{
m_androidTTS.Call("setPitch", pitch);
}
public bool IsSpeaking()
{
return m_androidTTS.Call("isSpeaking");
}
#region TTSBeta
/**
* public class TextToSpeechBetaInitListener : AndroidJavaProxy
{
public TextToSpeechBetaInitListener() : base("com.google.tts.TextToSpeechBeta$OnInitListener")
{
}
public delegate void TTSBetaInitDelegate(int status, int versionCode);
private TTSBetaInitDelegate m_TTSBetaInitDelegate;
private void OnInit(int status, int versionCode)
{
if (m_TTSBetaInitDelegate != null)
{
m_TTSBetaInitDelegate(status, versionCode);
}
else
{
Debug.LogError("Init failed. Please call 'SetInitDelegate(TextToSpeechBetaInitListener m_TTSBetaInitDelegate)' first");
}
}
public void SetInitDelegate(TTSBetaInitDelegate TTSBetaInitDelegate)
{
m_TTSBetaInitDelegate = TTSBetaInitDelegate;
}
}
private AndroidJavaObject m_androidTTSBeta;
private TextToSpeechBetaInitListener m_betainitListener;
private void InitAndroidTextToSpeechBeta()
{
///需要引入com.google.tts jave包
m_betainitListener = new TextToSpeechBetaInitListener();
m_betainitListener.SetInitDelegate(OnInitTTSBetaEngine);
UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
m_currentActivity = UnityPlayer.GetStatic("currentActivity");
m_androidTTSBeta = new AndroidJavaObject ("com.google.tts.TextToSpeechBeta", m_currentActivity, m_betainitListener);
}
private void OnInitTTSBetaEngine(int status, int versionCode)
{
if (status == TextToSpeech_SUCCESS)
{
int result = m_androidTTSBeta.Call("setLanguage", Locale.GetStatic("CHINA"));
if (result != TextToSpeech_SUCCESS)
{
string error = "Chinese not supported, English Mode Start Instead";
error.ShowToast();
m_androidTTSBeta.Call("setLanguage", Locale.GetStatic("US"));
}
}
}
**/
#endregion
#endif
}