viernes, 7 de febrero de 2014

Calling Android's Text to Speech from QT 5.2. An example of QT Android Extras

I wrote this post (in Spanish) a couple of weeks ago as a reminder just in case I have to do something similar in the future. Sometimes I found QT documentation is not all good  I would desire and with Android Extras I had to spend quite a lot of time to solve some problems. What I wanted to do is to call Adroid's TexToSpeech class from  QT so I used QtAndroidExtras documentation as a reference, this example of notifications and this post of someone else trying to do the same.

At the beginning, it seemed enough to me, in fact, changing a little the notification example with some code from the QT forum should have been a good solution. But the guy on the forum did not have a working code, he asked for help and there was no response. On the other hand, the sample code in QT notification example calls a static method and I wanted to create a Java class that inherited TextToSpeech (something that in the end I did not get).


As I said before, these are a set of notes I want to keep regarding some problems I found and how I solved them. So, the format is something like a personal notes, a FAQ or if you prefer, a conversation with myself.

1. I usually write code in QT but I have no experience as Android programmer.
Well, there is no solution for this. The only thing is to google searching for examples. Anyway, I wrote the resources I used at the beginning of this post.

2. After studying the examples, I see it is required to have access to the activity context so, it is necessary to create an object from a class with an activity context parameter in his constructor.
Your new class should extend QtActivity, it will provide the proper context when you invoke TextToSpeech class.

3. But it is required to implement onListener in my class and it is already inheriting QtActivity!
No problem, you can do both things at the same time:

public class TestAndroidClient extends QtActivity implements TextToSpeech.OnInitListener


4. I've created my class but I don't know the correct location in the file tree, moreover it isn't included in the apk when the project is build.
See the notification example and make a similar structure. At your project's root folder create a directory called android-source and a src one within it. From the last one you should have the rest of the structure for your java files, i.e.  org/qtproject/example/your-app-name/YourClass.java



Be sure to have these two lines in your project's .pro:

QT+=android-extras
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-source

Check the build directory (normally named build-xxxx) for your class file. In linux you can use find:

find . -name YourClass.java





5. When I launch the application it crashes and closes without showing any exception report and the error is previous to my class invocation.
Maybe this is because the class is not found. Check your manifest.xml and look your activity tag, it should point to your class


    
        
   

6. But I want to write a class that inherits TextToSpeech so everything would be more object oriented.
Well... I don't have a solution for this. To tell the truth I failed to do a non static method, furthermore I haven't found an example doing that.

7. I'm trying a call my static method from the C++ code and nothing happens.
Take a look to your parameters signature. It is also important to use the right javaobject from C++to being recognised by the Java function. Having the following Java function:

    public static void test(String msg)
    {
        System.out.println(msg );
        tts.speak(msg, TextToSpeech.QUEUE_FLUSH, null);

    }

The appropriate C++ call will be:

    QAndroidJniObject javaMessage = QAndroidJniObject::fromString(msg);
    QAndroidJniObject::callStaticMethod("org/qtproject/example/testandroidextras/TestAndroidClient",
                                              "test",
                                              "(Ljava/lang/String;)V",
                                              javaMessage.object());

8. I need a example!!
Ok, you can grab the source code from GitHub

No hay comentarios: