How do I voice-enable Android and iOS applications with Delphi?
Last reviewed: 7/8/2022
HOW Article ID: H072220
The information in this article applies to:
- SpeechKit 11
Summary
Voice enabling Delphi Android and iOS applications is easier with SpeechKit classes for managing speech recognition and speech synthesis.
More Information
Develop Delphi applications that speak and listen using your favorite version of Delphi. SpeechKit includes Delphi units comprised of the Delphi classes that manage recognizers and synthesizers. These are common classes, methods, and events across all platforms.
SpeechKit Units
SpeechKit includes an Object Pascal source file Chant.SpeechKit.pas comprised of the Object Pascal classes that manage speech recognition and speech synthesis. It also includes an Object Pascal source file Chant.Shared.pas comprised of common Object Pascal classes that all Chant libraries use.
To access the SpeechKit Object Pascal classes within your application, first add a project reference to the SpeechKit Object Pascal source files:
- Within your Delphi project, select Project Options.
- Select the Delphi compiler options.
- Add Search path reference to the SpeechKit unit source file director: C:\Program Files\Chant\SpeechKit 11\Delphi\source.
- Add unit output path reference to the local directory with a period '.' character.
To access the SpeechKit Object Pascal classes within your application, add a reference to the Chant.Shared, and Chant.SpeechKit units in your uses clause.
unit Unit1;
interface
uses
..., Chant.Shared, Chant.SpeechKit;
SpeechKit Instantiation
Instantiate SpeechKit and set the credentials.
// Instantiate SpeechKit object
_SpeechKit := TSpeechKit.Create();
if (_SpeechKit <> nil) then
begin
// Set credentials
_SpeechKit.SetCredentials('Credentials');
end;
Event Callbacks
Event callbacks are the mechanism in which the class object sends information back to the application such as speech recognition occurred, audio playback finished, or there was an error.
procedure TForm1.Recognition(Sender: TObject; Args: TRecognitionDictationEventArgs);
begin
If ((Args <> nil) and (Length(Args.Text) > 0)) then
begin
...
end;
end;
Android Devices
The following are Delphi project configuration and application implementation considerations for targeting Android devices.
SpeechKit Jars
SpeechKit includes Android compatible jar libraries required by the SpeechKit Delphi classes.
Right click the Libraries folder in your Android Target Platform and select the Add... option to add the following SpeechKit jars to your project:
- Program Files\Chant\SpeechKit 11\Android\lib\speechkit.jar and
- Program Files\Chant\SpeechKit 11\Android\lib\chant.shared.jar.
Android Units
Additional Delphi Android units may be required such as Androidapi.Helpers to access the application context and Androidapi.Jni.Os and Androidapi.JNIBridge for permissions.
Permissions
Speech recognition requires the user to grant RECORD_AUDIO permission. Add the appropriate permission in the manifest file by selecting Project->Options->Application->Uses Permission and setting Record audio to true.
Speech synthesis streamed to a file requires WRITE_EXTERNAL_STORAGE permission. Add the appropriate permission in the manifest file by selecting Project->Options->Application->Uses Permission and setting Write external storage to true.
Apps Targeting Android 11
Speech recognition apps targeting Android 11 with API 30 require an additional manifest entry. Add the following queries section before the application element.
<queries>
<intent>
<action android:name="android.speech.RecognitionService" />
</intent>
</queries>
Speech synthesis apps targeting Android 11 with API 30 require an additional manifest entry. Add the following queries section before the application element.
<queries>
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>
iOS Devices
The following are Delphi project configuration and application implementation considerations for targeting iOS devices.
SpeechKit Static Library
SpeechKit includes an iOS compatible static library required by the SpeechKit Object Pascal classes. Copy the libChantSpeechKit.a to your project folder from C:\Program Files\Chant\SpeechKit 11\iOS\lib (do not add it to the project).
Add static library linking options to your project by selecting Project->Options->Building->Delphi Compiler->Linking->Options passed to the LD Linker->Value from "All configurations - iOS Device 64-bit":
-ObjC -bequiet
SpeechKit Unit
SpeechKit includes Object Pascal source files iOSapiChantSpeechKit.pas and Chant.SpeechKit.Bridge.pas that serve as a bridge for the Delphi class to communicate with iOS.
Add the SpeechKit Object Pascal classes to your application project:
- Within your Delphi project, select Project->Add to Project.
- Navigate to C:\Program Files\Chant\SpeechKit 11\Delphi\source.
- Add iOSapi.ChantSpeechKit.pas and Chant.SpeechKit.Bridge.pas to the project.
Permissions
Speech recognition requires the user to grant permission for speech recognition and microphone usage. Add the appropriate permission via version string key values by selecting Project->Options->VerionInfo and setting string values for NSSpeechRecognitionUsageDescription and NSMicrophoneUsageDescription.
iOS Frameworks
Verify that you have the necessary iOS development prerequisites and configured them for development per the RAD Studio Delphi documentation.
There may be additional iOS frameworks required by the iOS for an application to use Speech Recognition. To verify or add them to your SDK, select Tools->Options->Deployment->SDK Manager->iOS Device 64-bit.
-
Scroll through the list to verify or add the following Frameworks to your project:
- Speech
- AVFoundation
- CoreMIDI
- ImageIO
- MediaToolbox
- AudioToolbox
- CoreAudio
- IOSurface
- Click the Update Local File Cache button to bring them from your device into the C++Builder environment.
- Click the Save button to save changes to the environment.