Use native TextToSpeech on iOS

78 views
Skip to first unread message

donat...@gmail.com

unread,
Sep 2, 2015, 6:05:52 AM9/2/15
to CodenameOne Discussions
Hello everyone, and thanks for reading !

I have to use the TextToSpeech functionality on both Android and iOS. During my researches, it has seemed that nobody posted anything about a working application using these technologies. In order to achieve this, it seems I have to use native code, which I did without much problem on Android (the application is perfectly working on this platform), but I'm having issues with iOS.

The last problem I had was a library import issue, which I solved by adding a "ios.add_libs" property (with value "AVFoundation.framework").


But here is my problem :

The build result I get from the server was an error, with an error log you can find attached, but in which I can't find any error. I know a few potential issues that can occur, but as I don't find anything useful on this log, I prefer asking for help first.


Miscellaneous infos :

IDE : NetBeans
Desktop OS : W7
Interface extending NativeInterface name : NativeCalls

I also attached the "userclasses_NativeCallsImpl.h" and "userclasses_NativeCallsImpl.m" files. There could be errors there too.


Thanks !
error_log_02.09.2015_1152.txt
userclasses_NativeCallsImpl.m
userclasses_NativeCallsImpl.h

Shai Almog

unread,
Sep 2, 2015, 11:17:28 PM9/2/15
to CodenameOne Discussions, donat...@gmail.com
Hi,
we have a fully functioning demo that works on iOS & Android called DrSbaitso that has a TTS native interface. I created a big tutorial covering everything in building the app including the TTS portion:
https://www.udemy.com/learn-mobile-programming-by-example-with-codename-one/

You can see the actual code and try the JavaScript version (without the TTS portion) here:
https://www.codenameone.com/demos-DrSbaitso.html

donat...@gmail.com

unread,
Sep 4, 2015, 8:11:43 AM9/4/15
to CodenameOne Discussions
Thanks a lot for your answer !

I watched the video "The Native Text To Speech Interface" on udemy, it helped me alot.

However, I still have an issue (which I think is probably trivial) about the AVFoundation library import I think.

The error log contains the following :

/var/folders/p_/xlvwhg4101z8r81_nl13cds80000gn/T/build1312142854404591961xxx/dist/../build/xcode/src/app/userclasses_NativeCallsImpl.m: In function '-[userclasses_NativeCallsImpl readString:param1:]':
/var/folders/p_/xlvwhg4101z8r81_nl13cds80000gn/T/build1312142854404591961xxx/dist/../build/xcode/src/app/userclasses_NativeCallsImpl.m:9: error: 'AVSpeechSynthesisVoice' undeclared (first use in this function)
/var/folders/p_/xlvwhg4101z8r81_nl13cds80000gn/T/build1312142854404591961xxx/dist/../build/xcode/src/app/userclasses_NativeCallsImpl.m:9: error: (Each undeclared identifier is reported only once
/var/folders/p_/xlvwhg4101z8r81_nl13cds80000gn/T/build1312142854404591961xxx/dist/../build/xcode/src/app/userclasses_NativeCallsImpl.m:9: error: for each function it appears in.)
/var/folders/p_/xlvwhg4101z8r81_nl13cds80000gn/T/build1312142854404591961xxx/dist/../build/xcode/src/app/userclasses_NativeCallsImpl.m:9: error: 'voice' undeclared (first use in this function)
/var/folders/p_/xlvwhg4101z8r81_nl13cds80000gn/T/build1312142854404591961xxx/dist/../build/xcode/src/app/userclasses_NativeCallsImpl.m:10: error: 'AVSpeechUtterance' undeclared (first use in this function)
/var/folders/p_/xlvwhg4101z8r81_nl13cds80000gn/T/build1312142854404591961xxx/dist/../build/xcode/src/app/userclasses_NativeCallsImpl.m:10: error: 'utterance' undeclared (first use in this function)
/var/folders/p_/xlvwhg4101z8r81_nl13cds80000gn/T/build1312142854404591961xxx/dist/../build/xcode/src/app/userclasses_NativeCallsImpl.m:11: error: 'AVSpeechSynthesizer' undeclared (first use in this function)
/var/folders/p_/xlvwhg4101z8r81_nl13cds80000gn/T/build1312142854404591961xxx/dist/../build/xcode/src/app/userclasses_NativeCallsImpl.m:11: error: 'syn' undeclared (first use in this function)
/var/folders/p_/xlvwhg4101z8r81_nl13cds80000gn/T/build1312142854404591961xxx/dist/../build/xcode/src/app/userclasses_NativeCallsImpl.m:12: error: 'AVSpeechUtteranceDefaultSpeechRate' undeclared (first use in this function


My userclasses_NativeCallsImpl.m now contains the following :

#import "userclasses_NativeCallsImpl.h"
#import <AVFoundation/AVFoundation.h>

@implementation userclasses_NativeCallsImpl

-(void)readString:(NSString*)param param1:(NSString*)param1{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSString *lang = [[param1 stringAppendingString:@"-"] stringAppendingString:[param1 uppercaseString]];
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:lang];
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:param];
    AVSpeechSynthesizer *syn = [[[AVSpeechSynthesizer alloc] init] autorelease];
    utterance.rate = AVSpeechUtteranceDefaultSpeechRate;
    utterance.voice = voice;
    [syn speakUtterance:utterance];
    [pool release];
}

-(BOOL)isSupported{
    return YES;
}

@end

And I added the build option in the projet properties as follows :


An adapted version of the code (with fewer lines) works fine on Xcode, and the test is working as intended, so I don't think it's an inner code issue.



Thanks again for your help !


Shai Almog

unread,
Sep 5, 2015, 12:14:58 AM9/5/15
to CodenameOne Discussions, donat...@gmail.com
Its got nothing to do with the missing add_libs since AVFoundation framework is already included and so are its headers.
I have no idea why you are getting these errors, are you building with a certificate?
If you add the specific:

#import <AVFoundation/AVFoundation.h>


To the .m file does it work?

donat...@gmail.com

unread,
Sep 7, 2015, 10:57:09 AM9/7/15
to CodenameOne Discussions, donat...@gmail.com
It was indeed a certificate problem, which I solved by using the certificate generator (thanks for this functionality by the way !).

Everything about the TTS now works fine on both Android and iOS.


Thanks for your help !
Reply all
Reply to author
Forward
0 new messages