Destroy Nuance Session

I have an activity that creates an “Audio” class and tries to use the Android API for speech to read text. If the language is not supported, he tries to use MediaPlayer to play a custom mp3 file from the server. Finally, if MediaPlayer does not work, it uses Nuance SpeechKit to read the text:

fluxogram

My problem is when I destroy an action, I want to destroy / stop the Nuance sound, and I'm not sure how to mute the Nuance sound.

Action 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 use Text to Speech or MediaPlayer, and if I destroy my activity, the sound will be destroyed immediately. But I can’t destroy the sound if the Nuance is playing. He just keeps talking.

pause() destroy(). nuance.getAudioPlayer AudioPlayer. , , stop() .


?

, Nuance, . Android Text to Speech.

Nuance

?

4 , . , , Nuance.

Nuance ?

Nuance . android TTS MediaPlayer. , Nuance. , !

+4
3

​​( ).

, , :

, ( ) , , .

, :

enter image description here

:

, stop(), Audio , .

, .

+1

nuance API , session.speakString(...) Transaction, a Transaction 2 , :

cancel() - .
stopRecording() - , .

, cancel() - , .

0

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

0
source

Source: https://habr.com/ru/post/1665343/


All Articles