Destroy Nuance session

457 Views Asked by At

I have an activity that creates an "Audio" class and tries to use android Text to Speech API to read some text. If the language is not supported, it tries to use MediaPlayer to play a custom mp3 file from the server. Finally if MediaPlayer fails, it uses Nuance SpeechKit to read the text:

fluxogram

My problem is when I destroy the activity, I want to destroy/stop the Nuance audio too and I'm not sure how to shutdown Nuance audio.

Activity class

private Audio audio;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);
    audio = new Audio(this).play("my text to read");
}

@Override
protected void onPause() {
    audio.pause();
    super.onPause();
}

@Override
protected void onDestroy() {
    audio.destroy();
    super.onDestroy();
}

Audio class

private TextToSpeech tts;
private MediaPlayer player;
private Session session;

public void play(String text) {
    // check if supported 
    if (supported) tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    else mediaPlayer(text);
}

private void mediaPlayer(String text) {
    // make some queries on server to find the file url
    if (queryFoundFile) {
        player = new MediaPlayer();
        player.setDataSource(myFileUrl);
        player.setAudioStreamType(3);
        player.prepare();
        player.start();
    } else nuancePlayer(text);
}

private void nuancePlayer(String text) {
    Transaction.Options options = new Transaction.Options();
    options.setLanguage(new Language("eng-USA"));

    session = Session.Factory.session(activity, myServer, appKey);
    session.speakString(text, options, new Transaction.Listener() {
        @Override
        public void onError(Transaction transaction, String s, TransactionException e) { 
            e.printStackTrace()
        }
    });

    // it reaches here and nuance plays the audio
}

// these are the methods I call when the activity is paused or destroyed
public void pause() {
    if (tts != null) tts.stop();
    if (player != null) player.stop();
    if (nuance != null) nuance.getAudioPlayer().stop(); // don't work
}

public void destroy() {
    if (tts != null) tts.shutdown();
    if (player != null) player.release();
    if (nuance != null) nuance.getAudioPlayer().stop(); // don't work
}

If I'm using Text to Speech or MediaPlayer and if I destroy my Activity, the audio is immediately destroyed. But I can't seem to destroy the audio if is Nuance playing. It just keeps talking.

I did some debugging and the pause() and destroy() methods are being called. Also nuance.getAudioPlayer is not null and is the AudioPlayer playing. I can't find the reason why he is not stopping when I call the method stop() on him.


What is Nuance?

This is my first time using Nuance so I am not that experienced with this. Basically I see it like an alternative to the Android Text to Speech.

Nuance Developers

Why I have this on my project?

My project has 4 main languages, and I need to have a text to speech function to read some text. The problem is, android Text to Speech don't support some of these languages which Nuance support.

Why is Nuance my last option?

Because Nuance has costs. I try to use android TTS or MediaPlayer. Only if those two fail, I use Nuance. It is a last resort to read my text!

3

There are 3 best solutions below

1
On BEST ANSWER

As per change log, this issue is known since a year ago and no fixed yet (as per changelog).

For temporary solution till they gave you fixed release you can do the following:

Break your text in small chunks and instead of playing complete text(as audio) at once, Queued these small text chunks into the audio player so that you audio will stop after finish playing the current chunk instead of complete text.

because as per known issue in the change log given below:

enter image description here

Please note this line:

However, if multiple Audios are queued for playback and stop() is called, then the next Audio will not begin playing and the queue will be cleared.

I hope this will help you.

5
On

A quick look on the nuance API shows that session.speakString(...) returns a Transaction, and a Transaction has 2 methods that might be of interest for you:

cancel() - Cancel the Transaction.
stopRecording() - Stop recording audio and complete the Transaction, if there is an ongoing recording.

Looks like cancel() is what you need.

0
On

Try setting session to null to see if the garbage collector solves the problem.