如何暂停Nuance会话

时间:2019-03-08 14:35:27

标签: java android nuance

我正在开发使用Nuance tts sdk的应用程序。

在主要活动中,我的应用程序将通过在另一个类中调用synthesize方法来播放音频文件。当用户离开主要活动时,我想停止会话(声音),但我不知道如何执行此操作

从我的主要活动中,我在类中调用

            illustrate.startSynthesize();

我实现SDK的类:

 package com.example.illuminate_me;

    import android.content.Context;

import com.nuance.speechkit.Audio;
import com.nuance.speechkit.AudioPlayer;
import com.nuance.speechkit.Language;
import com.nuance.speechkit.Session;
import com.nuance.speechkit.Transaction;
import com.nuance.speechkit.TransactionException;
import com.nuance.speechkit.Voice;

public class Illustrate implements  AudioPlayer.Listener{
    private Session speechSession;
    private Transaction ttsTransaction;
    private Illustrate.State state = Illustrate.State.IDLE;
    private String voice ="Tarik";
    private Context context;
    private String text;


    public Illustrate(String tts, Context context) {
    this.context=context;
    this.text=tts;

        //Create a session
        speechSession = Session.Factory.session(context, Configuration.SERVER_URI, Configuration.APP_KEY);
        speechSession.getAudioPlayer().setListener(this);

        setState(Illustrate.State.IDLE);
      //  startSynthesize();
    }

    public void startSynthesize() {
       toggleTTS();
    }


    private void toggleTTS() {
        switch (state) {
            case IDLE:
                if(ttsTransaction == null) {
                    synthesize();
                }
                //Otherwise lets attempt to cancel that transaction
                else {
                    cancel();
                }
                break;
            case PLAYING:
                speechSession.getAudioPlayer().pause();
                setState(Illustrate.State.PAUSED);
                break;
            case PAUSED:
                speechSession.getAudioPlayer().play();
                setState(Illustrate.State.PLAYING);
                break;
        }
    }

    public void synthesize() {
        //Setup our TTS transaction options.
        Transaction.Options options = new Transaction.Options();
        options.setLanguage(new Language(Configuration.LANGUAGE));

        options.setVoice(new Voice(voice)); 
        ttsTransaction = speechSession.speakString(text, options, new Transaction.Listener() {
            @Override
            public void onAudio(Transaction transaction, Audio audio) {

                ttsTransaction = null;
            }

            @Override
            public void onSuccess(Transaction transaction, String s) {

            }

            @Override
            public void onError(Transaction transaction, String s, TransactionException e) {

                ttsTransaction = null;
            }
        });
    }


    /**
     * Cancel the TTS transaction.
     * This will only cancel if we have not received the audio from the server yet.
     */
    private void cancel() {
        ttsTransaction.cancel();
    }

    @Override
    public void onBeginPlaying(AudioPlayer audioPlayer, Audio audio) {

        setState(Illustrate.State.PLAYING);
    }

    @Override
    public void onFinishedPlaying(AudioPlayer audioPlayer, Audio audio) {    
        setState(Illustrate.State.IDLE);
    }




    private enum State {
        IDLE,
        PLAYING,
        PAUSED
    }

    /**
     * Set the state and update the button text.
     */
    private void setState(Illustrate.State newState) {
        state = newState;
        switch (newState) {
            case IDLE:
                // Next possible action is speaking
                break;
            case PLAYING:
                // Next possible action is pausing
                break;
            case PAUSED:
                // Next possible action is resuming the speech
                // toggleTTS.setText(getResources().getString(R.string.speak_string));
                break;
        }
    }  
}

非常感谢您的帮助。 如果有人想看的话,这是文档 Speech synthesis (TTS)

0 个答案:

没有答案