xe5 android tts(Text To Speech) [转]

时间:2022-04-16 03:00:31

TTS是Text To Speech的缩写,即“从文本到语音”,是人机对话的一部分,让机器能够说话。

以下代码实现xe5 开发的文本转语音的方法

和访问蓝牙一样,这里用javaclass的接口实现

接口声明部分:

{*******************************************************}
{ }
{ CodeGear Delphi Runtime Library }
{ Copyright(c) 2013 Jeff Overcash }
{ }
{*******************************************************} { Delphi trnslation for TTS Android Java classes from }
{ http://developer.android.com/reference/android/speech/tts/package-summary.html } unit Androidapi.JNI.TTS; interface uses Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Os,
Androidapi.JNI.App,
Androidapi.JNI.GraphicsContentViewText; type {Forward declarations}
JSynthesisCallback = interface; // android.speech.tts.SynthesisCallback
JSynthesisRequest = interface; // android.speech.tts.SynthesisRequest
JTextToSpeech = interface; // android.speech.tts.TextToSpeech
JTextToSpeech_Engine = interface; // android.speech.tts.TextToSpeech$Engine
JTextToSpeech_EngineInfo = interface; //android.speech.tts.TextToSpeech$EngineInfo
JTextToSpeech_OnInitListener = interface; // android.speech.tts.TextToSpeech$OnInitListener
JTextToSpeech_OnUtteranceCompletedListener = interface; // android.speech.tts.TextToSpeech$OnUtteranceCompletedListener
JTextToSpeechService = interface; // android.speech.tts.TextToSpeechService
JUtteranceProgressListener = interface; // android.speech.tts.UtteranceProgressListener JSynthesisCallbackClass = interface(IJavaClass)
['{3B30766E-A43B-4005-BE92-472CD075F325}']
end; [JavaSignature('android/speech/tts/SynthesisCallback')]
JSynthesisCallback = interface(IJavaInstance)
['{190C13DE-BC7E-44CE-94F6-4AF1A84A4612}']
function audioAvailable(buffer: TJavaArray<byte>; offset: Integer; length: Integer): Integer; cdecl;
function done: Integer; cdecl;
procedure error; cdecl;
function getMaxBufferSize: Integer; cdecl;
function start(sampleRateInHz: Integer; audioFormat: Integer; channelCount: Integer): Integer; cdecl;
end;
TJSynthesisCallback = class(TJavaGenericImport<JSynthesisCallbackClass, JSynthesisCallback>) end; JSynthesisRequestClass = interface(JObjectClass)
['{AE41459D-42C6-4E66-B174-F6FA5216A1DA}']
{ Method }
function init(text: JString; params: JBundle): JSynthesisRequest;
end; [JavaSignature('android/speech/tts/SynthesisRequest')]
JSynthesisRequest = interface(JObject)
['{1963DAD8-C58F-4868-BF8A-B43AE7A14145}']
function getCountry: JString; cdecl;
function getLanguage: JString; cdecl;
function getParams: JBundle; cdecl;
function getPitch: Integer; cdecl;
function getSpeechRate: Integer; cdecl;
function getText: JString; cdecl;
function getVariant: JString; cdecl;
end;
TJSynthesisRequest = class(TJavaGenericImport<JSynthesisRequestClass, JSynthesisRequest>) end; JTextToSpeechClass = interface(JObjectClass)
['{0E2C5E49-95BE-4F19-BCCD-21960D03E957}']
{ Property Methods }
function _GetACTION_TTS_QUEUE_PROCESSING_COMPLETED: JString;
function _GetERROR: Integer;
function _GetLANG_AVAILABLE: Integer;
function _GetLANG_COUNTRY_AVAILABLE: Integer;
function _GetLANG_COUNTRY_VAR_AVAILABLE: Integer;
function _GetLANG_MISSING_DATA: Integer;
function _GetLANG_NOT_SUPPORTED: Integer;
function _GetQUEUE_ADD: Integer;
function _GetQUEUE_FLUSH: Integer;
function _GetSUCCESS: Integer;
{ Methods }
function init(contect: JContext; listener: JTextToSpeech_OnInitListener) : JTextToSpeech; cdecl; overload;
function init(context: JContext; listener: JTextToSpeech_OnInitListener; engine: JString): JTextToSpeech; overload;
{ Properties }
property ACTION_TTS_QUEUE_PROCESSING_COMPLETED: JString read _GetACTION_TTS_QUEUE_PROCESSING_COMPLETED;
property ERROR: Integer read _GetERROR;
property LANG_AVAILABLE: Integer read _GetLANG_AVAILABLE;
property LANG_COUNTRY_AVAILABLE: Integer read _GetLANG_COUNTRY_AVAILABLE;
property LANG_COUNTRY_VAR_AVAILABLE: Integer read _GetLANG_COUNTRY_VAR_AVAILABLE;
property LANG_MISSING_DATA: Integer read _GetLANG_MISSING_DATA;
property LANG_NOT_SUPPORTED: Integer read _GetLANG_NOT_SUPPORTED;
property QUEUE_ADD: Integer read _GetQUEUE_ADD;
property QUEUE_FLUSH: Integer read _GetQUEUE_FLUSH;
property SUCCESS: Integer read _GetSUCCESS;
end; [JavaSignature('android/speech/tts/TextToSpeech')]
JTextToSpeech = interface(JObject)
['{E1D06364-F967-4381-B178-EEDD42C203C7}']
function addEarcon(earcon: JString; filename: JString): Integer; cdecl; overload;
function addEarcon(earcon: JString; packagename: JString; resourceID: Integer): Integer; cdecl; overload;
function addSpeech(text: JString; filename: JString): Integer; cdecl; overload;
function addSpeech(text: JString; packagename: JString; resourceID: Integer) : Integer; cdecl; overload;
function areDefaultsEnforced: Boolean; cdecl;
function getDefaultEngine: JString; cdecl;
function getEngines: JList; cdecl;
function getFeatures(locale: JLocale): JSet; cdecl;
function getLanguage: JLocale; cdecl;
function isLanguageAvailable(loc: JLocale): Integer; cdecl;
function isSpeaking: Boolean; cdecl;
function playEarcon(earcon: JString; queueMode: Integer; params: JHashMap) : Integer; cdecl;
function playSilence(durationInMs: Int64; queueMode: Integer; params: JHashMap): Integer; cdecl;
function setEngineByPackageName(enginePackageName: JString): Integer; cdecl;
function setLanguage(loc: JLocale): Integer; cdecl;
function setOnUtteranceCompletedListener(listener: JTextToSpeech_OnUtteranceCompletedListener): Integer; cdecl;
function setOnUtteranceProgressListener(listener: JUtteranceProgressListener): Integer; cdecl;
function setPitch(pitch: Single): Integer; cdecl;
function setSpeechRate(speechRate: Single): Integer; cdecl;
procedure shutdown; cdecl;
function speak(text: JString; queueMode: Integer; params: JHashMap) : Integer; cdecl;
function stop: Integer; cdecl;
function synthesizeToFile(text: JString; params: JHashMap; filename: String) : Integer; cdecl;
end;
TJTextToSpeech = class(TJavaGenericImport<JTextToSpeechClass, JTextToSpeech>) end; JTextToSpeech_EngineClass = interface(JObjectClass)
['{8516EA75-A410-4EEE-8281-9ABCE1577F46}']
{ Property Methods }
function _GeCHECK_VOICE_DATA_PASS: Integer;
function _GetACTION_CHECK_TTS_DATA: JString;
function _GetACTION_INSTALL_TTS_DATA: JString;
function _GetACTION_TTS_DATA_INSTALLED: JString;
function _GetCHECK_VOICE_DATA_BAD_DATA: Integer;
function _GetCHECK_VOICE_DATA_FAIL: Integer;
function _GetCHECK_VOICE_DATA_MISSING_DATA: Integer;
function _GetCHECK_VOICE_DATA_MISSING_VOLUME: Integer;
function _GetDEFAULT_STREAM: Integer;
function _GetEXTRA_AVAILABLE_VOICES: JString;
function _GetEXTRA_CHECK_VOICE_DATA_FOR: JString;
function _GetEXTRA_TTS_DATA_INSTALLED: JString;
function _GetEXTRA_UNAVAILABLE_VOICES: JString;
function _GetEXTRA_VOICE_DATA_FILES: JString;
function _GetEXTRA_VOICE_DATA_FILES_INFO: JString;
function _GetEXTRA_VOICE_DATA_ROOT_DIRECTORY: JString;
function _GetINTENT_ACTION_TTS_SERVICE: JString;
function _GetKEY_FEATURE_EMBEDDED_SYNTHESIS: JString;
function _GetKEY_FEATURE_NETWORK_SYNTHESIS: JString;
function _GetKEY_PARAM_PAN: JString;
function _GetKEY_PARAM_STREAM: JString;
function _GetKEY_PARAM_UTTERANCE_ID: JString;
function _GetKEY_PARAM_VOLUME: JString;
function _GetSERVICE_META_DATA: JString;
{ Methods }
function init: JTextToSpeech_Engine;
{ Properties }
property ACTION_CHECK_TTS_DATA: JString read _GetACTION_CHECK_TTS_DATA;
property ACTION_INSTALL_TTS_DATA: JString read _GetACTION_INSTALL_TTS_DATA;
property ACTION_TTS_DATA_INSTALLED: JString read _GetACTION_TTS_DATA_INSTALLED;
property CHECK_VOICE_DATA_BAD_DATA: Integer read _GetCHECK_VOICE_DATA_BAD_DATA;
property CHECK_VOICE_DATA_FAIL: Integer read _GetCHECK_VOICE_DATA_FAIL;
property CHECK_VOICE_DATA_MISSING_DATA: Integer read _GetCHECK_VOICE_DATA_MISSING_DATA;
property CHECK_VOICE_DATA_MISSING_VOLUME: Integer read _GetCHECK_VOICE_DATA_MISSING_VOLUME;
property CHECK_VOICE_DATA_PASS: Integer read _GeCHECK_VOICE_DATA_PASS;
property DEFAULT_STREAM: Integer read _GetDEFAULT_STREAM;
property EXTRA_AVAILABLE_VOICES: JString read _GetEXTRA_AVAILABLE_VOICES;
property EXTRA_CHECK_VOICE_DATA_FOR: JString read _GetEXTRA_CHECK_VOICE_DATA_FOR;
property EXTRA_TTS_DATA_INSTALLED: JString read _GetEXTRA_TTS_DATA_INSTALLED;
property EXTRA_UNAVAILABLE_VOICES: JString read _GetEXTRA_UNAVAILABLE_VOICES;
property EXTRA_VOICE_DATA_FILES: JString read _GetEXTRA_VOICE_DATA_FILES;
property EXTRA_VOICE_DATA_FILES_INFO: JString read _GetEXTRA_VOICE_DATA_FILES_INFO;
property EXTRA_VOICE_DATA_ROOT_DIRECTORY: JString read _GetEXTRA_VOICE_DATA_ROOT_DIRECTORY;
property INTENT_ACTION_TTS_SERVICE: JString read _GetINTENT_ACTION_TTS_SERVICE;
property KEY_FEATURE_EMBEDDED_SYNTHESIS: JString read _GetKEY_FEATURE_EMBEDDED_SYNTHESIS;
property KEY_FEATURE_NETWORK_SYNTHESIS: JString read _GetKEY_FEATURE_NETWORK_SYNTHESIS;
property KEY_PARAM_PAN: JString read _GetKEY_PARAM_PAN;
property KEY_PARAM_STREAM: JString read _GetKEY_PARAM_STREAM;
property KEY_PARAM_UTTERANCE_ID: JString read _GetKEY_PARAM_UTTERANCE_ID;
property KEY_PARAM_VOLUME: JString read _GetKEY_PARAM_VOLUME;
property SERVICE_META_DATA: JString read _GetSERVICE_META_DATA;
end; JTextToSpeech_Engine = interface(JObject)
['{5BAC3048-CB0C-4DC4-AF62-D0D9AE4394CF}']
end;
TJTextToSpeech_Engine = class(TJavaGenericImport<JTextToSpeech_EngineClass, JTextToSpeech_Engine>) end; JTextToSpeech_EngineInfoClass = interface(JObjectClass)
['{8297AD59-5A6F-4867-A185-CA09BAD90159}']
{Methods}
function init : JTextToSpeech_EngineInfo;
end; JTextToSpeech_EngineInfo = interface(JObject)
['{204B30D8-6E25-4531-847D-18588D48D9BF}']
{property Methods}
function _Geticon: integer;
function _Getlabel: JString;
function _Getname: JString;
procedure _Seticon(const Value: integer);
procedure _Setlabel(const Value: JString);
procedure _Setname(const Value: JString);
{Properties}
property icon : integer read _Geticon write _Seticon;
property _label : JString read _Getlabel write _Setlabel;
property name : JString read _Getname write _Setname;
end;
TJTextToSpeech_EngineInfo = class(TJavaGenericImport<JTextToSpeech_EngineInfoClass, JTextToSpeech_EngineInfo>) end; JTextToSpeech_OnInitListenerClass = interface(IJavaClass)
['{58D32EFB-6528-4EC6-BA4F-28B22FE8E573}']
end; [JavaSignature('android/speech/tts/TextToSpeech$OnInitListener')]
JTextToSpeech_OnInitListener = interface(IJavaInstance)
['{027DA109-680A-4A69-905D-F62E2BD1282F}']
procedure onInit(status: Integer); cdecl;
end;
TJTextToSpeech_OnInitListener = class(TJavaGenericImport<JTextToSpeech_OnInitListenerClass,JTextToSpeech_OnInitListener>) end; JTextToSpeech_OnUtteranceCompletedListenerClass = interface(IJavaClass)
['{0A608BB9-A6BF-4746-8419-9317AD625DFA}']
end; [JavaSignature('android/speech/tts/TextToSpeech$OnUtteranceCompletedListener')]
JTextToSpeech_OnUtteranceCompletedListener = interface(IJavaInstance)
['{00439F2B-E73C-4B93-A9E3-832AE3CC1D5F}']
procedure onUtteranceCompleted(utteranceID: JString); cdecl;
end;
TJTextToSpeech_OnUtteranceCompletedListener = class(TJavaGenericImport<JTextToSpeech_OnUtteranceCompletedListenerClass, JTextToSpeech_OnUtteranceCompletedListener>) end; JTextToSpeechServiceClass = interface(JServiceClass)
['{6BCC6ADC-CBCB-4515-A5C3-E33F9787EEC2}']
function init : JTextToSpeechService;
end; [JavaSignature('android/speech/tts/TextToSpeechService')]
JTextToSpeechService = interface(JService)
['{EE3943B6-88DC-488B-9E10-E0FA9914708D}']
function onGetFeaturesForLanguage(lang : JString; country : JString; variant : JString) : JSet; cdecl;
function onGetLanguage : TJavaArray<JString>; cdecl;
function onIsLanguageAvailable(lang : JString; country : JString; variant : JString) : Integer; cdecl;
function onLoadLanguage(lang : JString; country : JString; variant : JString) : Integer; cdecl;
procedure onStop; cdecl;
procedure onSynthesizeText(request : JSynthesisRequest; callback : JSynthesisCallback); cdecl;
end;
TJTextToSpeechService = class(TJavaGenericImport<JTextToSpeechServiceClass, JTextToSpeechService>) end; JUtteranceProgressListenerClass = interface(JObjectClass)
['{0870532F-6FD7-4B1F-9DEF-B53F0095C98A}']
{ Method }
function init: JUtteranceProgressListener;
end; [JavaSignature('android/speech/tts/UtteranceProgressListener')]
JUtteranceProgressListener = interface(JObject)
['{8B03499D-0B26-4F36-90E8-F724BD78DB0C}']
procedure onDone(utteranceID: JString); cdecl;
procedure onError(utteranceID: JString); cdecl;
procedure onStart(utteranceID: JString); cdecl;
end; TJUtteranceProgressListener = class(TJavaGenericImport<JUtteranceProgressListenerClass, JUtteranceProgressListener>) end; implementation end.
unit Unit1;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
Androidapi.JNI.TTS, FMX.StdCtrls, FMX.Layouts, FMX.Memo, AndroidAPI.JNIBridge; type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private type
TttsOnInitListener = class(TJavaLocal, JTextToSpeech_OnInitListener)
private
[weak] FParent : TForm1;
public
constructor Create(AParent : TForm1);
procedure onInit(status: Integer); cdecl;
end; private
{ Private declarations }
ttsListener : TttsOnInitListener;
tts : JTextToSpeech;
procedure SpeakOut;
public
{ Public declarations }
constructor Create(AOwner : TComponent); override;
destructor Destroy; override; end; var
Form1: TForm1; implementation {$R *.fmx} uses Androidapi.JNI.JavaTypes, FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText; procedure TForm1.Button1Click(Sender: TObject);
begin
SpeakOut;
end; procedure TForm1.Button2Click(Sender: TObject);
begin
tts := TJTextToSpeech.JavaClass.init(SharedActivityContext, ttsListener);
end; constructor TForm1.Create(AOwner: TComponent);
begin
inherited;
ttsListener := TttsOnInitListener.Create(self);
end; destructor TForm1.Destroy;
begin
if Assigned(tts) then
begin
tts.stop;
tts.shutdown;
tts := nil;
end;
ttsListener := nil;
inherited;
end; procedure TForm1.SpeakOut;
var
text : JString;
begin
text := StringToJString(Memo1.Lines.Text);
tts.speak(text, TJTextToSpeech.JavaClass.QUEUE_FLUSH, nil);
end; { TForm1.TttsOnInitListener } constructor TForm1.TttsOnInitListener.Create(AParent: TForm1);
begin
inherited Create;
FParent := AParent
end; procedure TForm1.TttsOnInitListener.onInit(status: Integer);
var
Result : Integer;
begin
if (status = TJTextToSpeech.JavaClass.SUCCESS) then
begin
result := FParent.tts.setLanguage(TJLocale.JavaClass.US);
if (result = TJTextToSpeech.JavaClass.LANG_MISSING_DATA) or
(result = TJTextToSpeech.JavaClass.LANG_NOT_SUPPORTED) then
ShowMessage('This Language is not supported')
else
begin
FParent.Button1.Enabled := true;
FParent.button2.Enabled := false;
end;
end
else
ShowMessage('Initilization Failed!');
end; end.

下载地址

http://pan.baidu.com/s/1kRuAK

xe5 android tts(Text To Speech) [转]的更多相关文章

  1. xe5 android tts&lpar;Text To Speech&rpar;

    xe5 android  tts(Text To Speech) TTS是Text To Speech的缩写,即“从文本到语音”,是人机对话的一部分,让机器能够说话. 以下代码实现xe5 开发的文本转 ...

  2. delphi xe5 android tts&lpar;Text To Speech&rpar;

    TTS是Text To Speech的缩写,即“从文本到语音”,是人机对话的一部分,让机器能够说话. 以下代码实现xe5 开发的文本转语音的方法 和访问蓝牙一样,这里用javaclass的接口实现 接 ...

  3. 第二篇:TTS(Text To Speech)语音合成

    TTS(Text To Speech)语音合成: 百度AI语音合成SDK文档--python SDK: https://ai.baidu.com/docs#/TTS-Online-Python-SDK ...

  4. 音频&lpar;3&rpar;Android TTS技术支持朗读英文

    Android对TTS技术的支持 Android 1.6开始支持TTS(Text To Speech)技术,通过该技术可以将文本转换成语音.目前2015-09-06只支持朗读英文. TTS技术的核心是 ...

  5. 解读 Android TTS 语音合成播报

    随着从事 Android 开发年限增加,负责的工作项目也从应用层开发逐步过渡到 Android Framework 层开发.虽然一开始就知道 Android 知识体系的庞大,但是当你逐渐从 Appli ...

  6. XE5 ANDROID通过webservice访问操作MSSQL数据库

    上接XE5 ANDROID平台 调用 webservice 一.服务端 在ro里添加函数(在impl上添加阿东connection,adoquery,dataprovider) function TN ...

  7. 必应语音API&lpar;Bing text to speech API&rpar;

    前言 Link : Microsoft Speech API overview 通过这个链接,大致了解Bing speech API的语音识别和语音合成两部分, 这次是需要用到TTS,所以就直接看TT ...

  8. delphi xe5 android 服务端和手机端的源码下载

    xe5 android的服务端和手机客户端的源代码下载地址 http://files.cnblogs.com/nywh2008/AndroidTest.rar

  9. xe5 android 手机上使用sqlite &lbrack;转&rsqb;

    在android手机上怎样使用sqlite数据库,这里用Navigator实现 增删改查. 1.新建firemonkey mobile application 2.选择blank applicatio ...

随机推荐

  1. CE 内存申请

    char ch_ReadByte='H'; char *ptr_OneLineData; unsigned ); if ((ptr_OneLineData = (char *)malloc(bufsi ...

  2. 【POJ 2406】Power Strings(KMP循环节)

    终于靠着理解写出KMP了,两种KMP要代码中这种才能求循环节.i-next[i]就是循环节. #include<cstdio> #define N 1000005 char s[N]; i ...

  3. 很少有人会告诉你的Android开发基本常识

    原文:很少有人会告诉你的Android开发基本常识. 文章介绍了一些关于开发.测试.版本管理.工具使用等方面的知识.

  4. hdu 2102 A计划

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸 ...

  5. c语言下多线程

    原文:c语言下多线程 [问题]创建了10个线程,10个线程公用一个线程体,创建如下: int t1=0,t2=1,t3=2,t4=3,t5=4,t6=5,t7=6,t8=7,t9=8,t10=9; i ...

  6. floor&lpar;&rpar;函数 向下取整 ceil&lpar;&rpar;函数向上取整

    floor(x)  is the largest integer not greater than x , 也就是,floor(x) 返回的是小于等于x的所有整数中最大的整数,简单的说,就是去掉x的小 ...

  7. 配置tomcat的远程调试

    参考文档: http://jingyan.baidu.com/article/0320e2c1f4ef6b1b87507b06.html http://flyer2010.iteye.com/blog ...

  8. python&lowbar;如何为创建大量实例节省内存?

    案例: 某网络游戏中,定义了玩家类Player(id, name, status,....),每有一个在线玩家,在服务器程序内有一个Player的实例,当在线人数很多时,将产生大量实例(百万级别) 需 ...

  9. 特定场景下Ajax技术的使用

    ajax介绍 jax技术包含了几种技术:javascript.xml.css.xstl.dom.xhtml和XMLHttpRequest七种技术,所以ajax就像是粘合剂把七种技术整合到一起,从而发挥 ...

  10. 牛客网第9场多校E(思维求期望)

    链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 题目描述 Niuniu likes to play OSU! We simplify the ...