xunfei


    private ToastDeal deal = new ToastDeal();
    String grammar = null;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        speech_listenAndWrite(this);
        speechCombining("我今天的运气很好,出门捡了50元钱钱...嘿嘿...",this);
        //speechDistinguish();
    }
   
    /**
     * 创建对话框   语音听写示例
     */
    public void speech_listenAndWrite(Context context)
    {
        RecognizerDialog isrDialog = new RecognizerDialog(context,Parameters.XUNFEI_APP_ID);
        isrDialog.setEngine("sms", null, null);
        isrDialog.setListener(recoListener);
        isrDialog.show();
    }
   
    private RecognizerDialogListener recoListener = new RecognizerDialogListener()
    {
        String result_text = "";
        public void onResults(ArrayList<RecognizerResult> results,boolean isLast)
        {
           
            if (isLast)
            {
                if (null != results)
                {
                    result_text = results.get(0).text;
                }
                deal.showToast(XunfeiTTSActivity.this, "结果为:" + result_text);
            }
            else
            {
                deal.showToast(XunfeiTTSActivity.this, "结果未取完!");
            }
        }
        public void onEnd(SpeechError error)
        {
            if (error != null)
            {
                deal.showToast(XunfeiTTSActivity.this, "错误返回码:" + error + " 文本内容为 :" + result_text);
                //showText(text);
            }
        }
    };
    /**
     * 语音合成示例
     */
    public void speechCombining(String speak_text,Context context)
    {
        // 后台方式
        SynthesizerPlayer player = SynthesizerPlayer.createSynthesizerPlayer(context,Parameters.XUNFEI_APP_ID);
        player.setVoiceName(Parameters.XUNFEI_VOICENAME_XIAOMEI);
        player.playText(speak_text, Parameters.XUNFEI_SPEAK_FASHION, null);
        player.setVolume(100);//80为语音播放音量   最高100  最低0
    }
   
    /**
     * 暂停语音播报功能
     */
    public void pause(Context context)
    {
        SynthesizerPlayer player = SynthesizerPlayer.createSynthesizerPlayer(context,Parameters.XUNFEI_APP_ID);
        player.pause();
    }
   
    /**
     * 开始语音播报功能
     */
    public void resume(Context context)
    {
        SynthesizerPlayer player = SynthesizerPlayer.createSynthesizerPlayer(context,Parameters.XUNFEI_APP_ID);
        player.resume();
    }

你可能感兴趣的:(speech)