Simple example of iFlytek java speech recognition sdk

1. The official website of iFLYTEK provides a more complete Demo

2. My Demo has deleted the rest of the parts except speech recognition, which is convenient to get started quickly

3. The whole project is in the attachment

4. The lambda expression is used, which requires java8 to run

5. The code is as follows:

package com.iflytek;

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import com.iflytek.cloud.speech.RecognizerListener;
import com.iflytek.cloud.speech.RecognizerResult;
import com.iflytek.cloud.speech.SpeechConstant;
import com.iflytek.cloud.speech.SpeechError;
import com.iflytek.cloud.speech.SpeechRecognizer;
import com.iflytek.cloud.speech.SpeechUtility;

public class MyFrame extends JFrame
{
	private static final long serialVersionUID = 1L;
	JPanel panelNorth, panelSouth;
	JTextArea textArea;
	JButton button_start, button_stop;
	private SpeechRecognizer mIat;

	public static void main(String[] args)
	{
		new MyFrame();

	}

	public MyFrame()
	{
		initIfly();
		Container con = this.getContentPane();
		con.setLayout(new BorderLayout());
		this.setSize(500, 300);
		this.setLocationRelativeTo(null);
		this.setResizable(true);
		this.setDefaultCloseOperation(3);
		this.setLayout(new BorderLayout());
		setFrame();
		this.add(panelSouth, BorderLayout.SOUTH);
		this.add(panelNorth, BorderLayout.NORTH);
		setVisible(true);
	}

	public void initIfly()
	{
		mIat = SpeechRecognizer.createRecognizer();
		SpeechUtility.createUtility("appid=fill in your appid here");
		// 例如SpeechUtility.createUtility("appid=12345678");
	}

	public void setFrame()
	{
		panelNorth = new JPanel();
		panelSouth = new JPanel();
		textArea = new JTextArea(30, 30);
		button_start = new JButton("开始");
		button_start.addActionListener(e ->
		{
			setting();
			textArea.setText("");
			if (!mIat.isListening()) mIat.startListening(recognizerListener);
			else mIat.stopListening ();
		});
		button_stop = new JButton("停止");
		button_stop.addActionListener(e ->
		{
			mIat.stopListening ();
			iatSpeechInitUI ();
		});
		panelNorth.add(textArea);
		panelSouth.add(button_start);
		panelSouth.add(button_stop);
	}

	void setting()// property setting
	{
		final String engType = "cloud";
		mIat.setParameter(SpeechConstant.ENGINE_TYPE, "cloud");
		mIat.setParameter(SpeechConstant.SAMPLE_RATE, "16000");//
		mIat.setParameter(SpeechConstant.NET_TIMEOUT, "20000");
		mIat.setParameter(SpeechConstant.KEY_SPEECH_TIMEOUT, "60000");
		mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");// Language en_us (English)
															// zh_cn(Chinese)
		mIat.setParameter(SpeechConstant.ACCENT, "mandarin");
		mIat.setParameter(SpeechConstant.DOMAIN, "iat");
		mIat.setParameter(SpeechConstant.VAD_BOS, "5000");
		mIat.setParameter(SpeechConstant.VAD_EOS, "1800");
		mIat.setParameter(SpeechConstant.ASR_NBEST, "1");
		mIat.setParameter(SpeechConstant.ASR_WBEST, "1");
		mIat.setParameter(SpeechConstant.ASR_PTT, "0");// Punctuation 0(off) 1(on)
		mIat.setParameter(SpeechConstant.RESULT_TYPE, "plain"); // return data format
																// plain or jason
		mIat.setParameter(SpeechConstant.ASR_AUDIO_PATH, null);
	}

	private RecognizerListener recognizerListener = new RecognizerListener()
	{
		@Override
		public void onBeginOfSpeech()
		{
			button_start.setText("Dictation...");
			button_start.setEnabled(false);
		}

		@Override
		public void onEndOfSpeech()
		{}

		/**
		 * Get the dictation result. Get the recognition result of the RecognizerResult type, accumulate the results, and display them in the Area
		 */
		@Override
		public void onResult(RecognizerResult results, boolean islast)
		{

			String text = results.getResultString();
			textArea.append(text);
			text = textArea.getText();

			if (islast)
			{
				iatSpeechInitUI ();
			}
		}

		@Override
		public void onVolumeChanged(int volume)
		{}

		@Override
		public void onError(SpeechError error)
		{
			if (null != error)
			{
				textArea.setText(error.getErrorDescription(true));
				iatSpeechInitUI ();
			}
		}

		@Override
		public void onEvent(int eventType, int arg1, int agr2, String msg)
		{}
	};

	public void iatSpeechInitUI()
	{
		button_start.setEnabled(true);
		button_start.setText("Start dictation");
	}

}

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326260601&siteId=291194637