入力文字を読み上げるAndroidアプリケーションを作る ~Delphiソースコード集
1.フォームの作成
FMXフレームワークで以下のようなTEditとTButtonを2つ配置したフォームを作成します。2.ソースコード
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.speech ,Androidapi.JNIBridge, FMX.StdCtrls, FMX.Controls.Presentation, FMX.Edit //TJavaLocal ; type TttsOnInitListener = class(TJavaLocal, JTextToSpeech_OnInitListener) public constructor Create(); procedure onInit(status: Integer); cdecl; end; TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { private 宣言 } ts:JTextToSpeech; li:TttsOnInitListener; public { public 宣言 } end; var Form1: TForm1; implementation {$R *.fmx} uses Androidapi.Helpers //StrintToJString ,Androidapi.JNI.JavaTypes //JLocale ; { TttsOnInitListener } constructor TttsOnInitListener.Create; begin inherited Create; end; procedure TttsOnInitListener.onInit(status: Integer); begin end; { TForm1 } procedure TForm1.Button1Click(Sender: TObject); begin if not ts.isSpeaking then ts.speak(StringToJString(Edit1.Text), TJTextToSpeech.JavaClass.QUEUE_FLUSH, nil); end; procedure TForm1.Button2Click(Sender: TObject); begin if ts.isSpeaking then ts.stop; end; procedure TForm1.FormCreate(Sender: TObject); begin li := TttsOnInitListener.Create(); ts:=TJTextToSpeech.JavaClass.init(TAndroidHelper.Context,li); JTextToSpeech(ts).setLanguage(TJLocale.JavaClass.getDefault); end; end.