Mobile SDK examples
    • PDF

    Mobile SDK examples

    • PDF

    Article summary

    Available in Classic and VPC

    This document introduces examples of using the CLOVA Speech Recognition (CSR) Mobile SDK API.

    Mobile SDK API

    This section describes a Mobile SDK API example that takes in language and speech data for speech recognition and converts the recognition results to text in a mobile environment.

    Java

    The following is a Java-based sample code for the Mobile SDK API in the Android environment.

    • Example code storage address: https://github.com/NaverCloudPlatform/naverspeech-sdk-android

    • Description

      • Main Activity class: Initialize SpeechRecognitionListener, and then receive and process events in handleMessage.
      • Class that inherits from SpeechRecognitionListener: Define how to handle results based on events such as connecting to the speech recognition server, delivering speech, and recognition results occurring.
    // 1. Main Activity class
    public class MainActivity extends Activity {
    	private static final String TAG = MainActivity.class.getSimpleName();
    	private static final String CLIENT_ID = "YOUR CLIENT ID"; // Find your client ID in "My Applications" and write it here.
        private RecognitionHandler handler;
        private NaverRecognizer naverRecognizer;
        private TextView txtResult;
        private Button btnStart;
        private String mResult;
        private AudioWriterPCM writer;
        // Handle speech recognition Messages.
        private void handleMessage(Message msg) {
            switch (msg.what) {
                case R.id.clientReady: // Ready for speech recognition
                    txtResult.setText("Connected");
                    writer = new AudioWriterPCM(Environment.getExternalStorageDirectory().getAbsolutePath() + "/NaverSpeechTest");
                    writer.open("Test");
                    break;
                case R.id.audioRecording:
                    writer.write((short[]) msg.obj);
                    break;
                case R.id.partialResult:
                    mResult = (String) (msg.obj);
                    txtResult.setText(mResult);
                    break;
                case R.id.finalResult: // Final recognition results
                	SpeechRecognitionResult speechRecognitionResult = (SpeechRecognitionResult) msg.obj;
                	List<String> results = speechRecognitionResult.getResults();
                	StringBuilder strBuf = new StringBuilder();
                	for(String result : results) {
                		strBuf.append(result);
                		strBuf.append("\n");
                	}
                    mResult = strBuf.toString();
                    txtResult.setText(mResult);
                    break;
                case R.id.recognitionError:
                    if (writer != null) {
                        writer.close();
                    }
                    mResult = "Error code : " + msg.obj.toString();
                    txtResult.setText(mResult);
                    btnStart.setText(R.string.str_start);
                    btnStart.setEnabled(true);
                    break;
                case R.id.clientInactive:
                    if (writer != null) {
                        writer.close();
                    }
                    btnStart.setText(R.string.str_start);
                    btnStart.setEnabled(true);
                    break;
            }
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            txtResult = (TextView) findViewById(R.id.txt_result);
            btnStart = (Button) findViewById(R.id.btn_start);
            handler = new RecognitionHandler(this);
            naverRecognizer = new NaverRecognizer(this, handler, CLIENT_ID);
            btnStart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!naverRecognizer.getSpeechRecognizer().isRunning()) {
                        mResult = "";
                        txtResult.setText("Connecting...");
                        btnStart.setText(R.string.str_stop);
                        naverRecognizer.recognize();
                    } else {
                        Log.d(TAG, "stop and wait Final Result");
                        btnStart.setEnabled(false);
                        naverRecognizer.getSpeechRecognizer().stop();
                    }
                }
            });
        }
        @Override
        protected void onStart() {
        	super.onStart(); // Initialize the speech recognition server here
        	naverRecognizer.getSpeechRecognizer().initialize();
        }
        @Override
        protected void onResume() {
            super.onResume();
            mResult = "";
            txtResult.setText("");
            btnStart.setText(R.string.str_start);
            btnStart.setEnabled(true);
        }
        @Override
        protected void onStop() {
        	super.onStop(); // Shut down the speech recognition server
        	naverRecognizer.getSpeechRecognizer().release();
        }
        // Declare handler for handling SpeechRecognizer thread's Messages.
        static class RecognitionHandler extends Handler {
            private final WeakReference<MainActivity> mActivity;
            RecognitionHandler(MainActivity activity) {
                mActivity = new WeakReference<MainActivity>(activity);
            }
            @Override
            public void handleMessage(Message msg) {
                MainActivity activity = mActivity.get();
                if (activity != null) {
                    activity.handleMessage(msg);
                }
            }
        }
    }
    
    // 2. Class that inherits from SpeechRecognitionListener
    class NaverRecognizer implements SpeechRecognitionListener {
    	private final static String TAG = NaverRecognizer.class.getSimpleName();
    	private Handler mHandler;
    	private SpeechRecognizer mRecognizer;
    	public NaverRecognizer(Context context, Handler handler, String clientId) {
    		this.mHandler = handler;
    		try {
    			mRecognizer = new SpeechRecognizer(context, clientId);
    		} catch (SpeechRecognitionException e) {
    			e.printStackTrace();
    		}
    		mRecognizer.setSpeechRecognitionListener(this);
    	}
    	public SpeechRecognizer getSpeechRecognizer() {
    		return mRecognizer;
    	}
    	public void recognize() {
    		try {
    			mRecognizer.recognize(new SpeechConfig(LanguageType.KOREAN, EndPointDetectType.AUTO));
    		} catch (SpeechRecognitionException e) {
    			e.printStackTrace();
    		}
    	}
    	@Override
    	@WorkerThread
    	public void onInactive() {
    		Message msg = Message.obtain(mHandler, R.id.clientInactive);
    		msg.sendToTarget();
    	}
    	@Override
    	@WorkerThread
    	public void onReady() {
    		Message msg = Message.obtain(mHandler, R.id.clientReady);
    		msg.sendToTarget();
    	}
    	@Override
    	@WorkerThread
    	public void onRecord(short[] speech) {
    		Message msg = Message.obtain(mHandler, R.id.audioRecording, speech);
    		msg.sendToTarget();
    	}
    	@Override
    	@WorkerThread
    	public void onPartialResult(String result) {
    		Message msg = Message.obtain(mHandler, R.id.partialResult, result);
    		msg.sendToTarget();
    	}
    	@Override
    	@WorkerThread
    	public void onEndPointDetected() {
    		Log.d(TAG, "Event occurred : EndPointDetected");
    	}
    	@Override
    	@WorkerThread
    	public void onResult(SpeechRecognitionResult result) {
    		Message msg = Message.obtain(mHandler, R.id.finalResult, result);
    		msg.sendToTarget();
    	}
    	@Override
    	@WorkerThread
    	public void onError(int errorCode) {
    		Message msg = Message.obtain(mHandler, R.id.recognitionError, errorCode);
    		msg.sendToTarget();
    	}
    	@Override
    	@WorkerThread
    	public void onEndPointDetectTypeSelected(EndPointDetectType epdType) {
    		Message msg = Message.obtain(mHandler, R.id.endPointDetectTypeSelected, epdType);
    		msg.sendToTarget();
    	}
    }
    

    ObjectiveC

    The following is a ObjectiveC-based sample code for the Mobile SDK API in the iOS environment.

    import UIKit
    import NaverSpeech
    import Common
    let ClientID = "YOUR_CLIENT_ID"
    class AutoViewController: UIViewController {
        required init?(coder aDecoder: NSCoder) { // Create NSKRecognizerConfiguration needed to initialize NSKRecognizer
            let configuration = NSKRecognizerConfiguration(clientID: ClientID)
            configuration?.canQuestionDetected = true
            self.speechRecognizer = NSKRecognizer(configuration: configuration)
            super.init(coder: aDecoder)
            self.speechRecognizer.delegate = self
        }
        override func viewDidLoad() {
            super.viewDidLoad()
            self.setupLanguagePicker()
        }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            if self.isViewLoaded && self.view.window == nil {
                self.view = nil
            }
        }
        override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
            let x = languagePickerButton.frame.minX
            let y = languagePickerButton.frame.maxY
            self.pickerView.frame = CGRect.init(x: x, y: y, width: languagePickerButton.bounds.size.width, height: self.pickerView.bounds.size.height)
        }
        @IBAction func languagePickerButtonTapped(_ sender: Any) {
            self.pickerView.isHidden = false
        }
        @IBAction func recognitionButtonTapped(_ sender: Any) { // Press the button to start speech recognition
            if self.speechRecognizer.isRunning {
                self.speechRecognizer.stop()
            } else {
                self.speechRecognizer.start(with: self.languages.selectedLanguage)
                self.recognitionButton.isEnabled = false
                self.statusLabel.text = "Connecting......"
            }
        }
        @IBOutlet weak var languagePickerButton: UIButton!
        @IBOutlet weak var recognitionResultLabel: UILabel!
        @IBOutlet weak var recognitionButton: UIButton!
        @IBOutlet weak var statusLabel: UILabel!
        fileprivate let speechRecognizer: NSKRecognizer
        fileprivate let languages = Languages()
        fileprivate let pickerView = UIPickerView()
    }
    
    extension AutoViewController: NSKRecognizerDelegate { // Implement NSKRecognizerDelegate protocol
    
        public func recognizerDidEnterReady(_ aRecognizer: NSKRecognizer!) {
            print("Event occurred: Ready")
            self.statusLabel.text = "Connected"
            self.recognitionResultLabel.text = "Recognizing......"
            self.setRecognitionButtonTitle(withText: "Stop", color: .red)
            self.recognitionButton.isEnabled = true
        }
        public func recognizerDidDetectEndPoint(_ aRecognizer: NSKRecognizer!) {
            print("Event occurred: End point detected")
        }
        public func recognizerDidEnterInactive(_ aRecognizer: NSKRecognizer!) {
            print("Event occurred: Inactive")
            self.setRecognitionButtonTitle(withText: "Record", color: .blue)
            self.recognitionButton.isEnabled = true
            self.statusLabel.text = ""
        }
        public func recognizer(_ aRecognizer: NSKRecognizer!, didRecordSpeechData aSpeechData: Data!) {
            print("Record speech data, data size: \(aSpeechData.count)")
        }
        public func recognizer(_ aRecognizer: NSKRecognizer!, didReceivePartialResult aResult: String!) {
            print("Partial result: \(aResult)")
            self.recognitionResultLabel.text = aResult
        }
        public func recognizer(_ aRecognizer: NSKRecognizer!, didReceiveError aError: Error!) {
            print("Error: \(aError)")
            self.setRecognitionButtonTitle(withText: "Record", color: .blue)
            self.recognitionButton.isEnabled = true
        }
        public func recognizer(_ aRecognizer: NSKRecognizer!, didReceive aResult: NSKRecognizedResult!) {
            print("Final result: \(aResult)")
            if let result = aResult.results.first as? String {
                self.recognitionResultLabel.text = "Result: " + result
            }
        }
    }
    extension AutoViewController: UIPickerViewDelegate, UIPickerViewDataSource {
        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 1
        }
        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            return self.languages.count
        }
        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
            return languages.languageString(at: row)
        }
        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            languages.selectLanguage(at: row)
            languagePickerButton.setTitle(languages.selectedLanguageString, for: .normal)
            self.pickerView.isHidden = true
            if self.speechRecognizer.isRunning { // Immediately stop (cancel) speech recognition if the language is changed during speech recognition
                self.speechRecognizer.cancel()
                self.recognitionResultLabel.text = "Canceled"
                self.setRecognitionButtonTitle(withText: "Record", color: .blue)
                self.recognitionButton.isEnabled = true
            }
        }
    }
    fileprivate extension AutoViewController {
        func setupLanguagePicker() {
            self.view.addSubview(self.pickerView)
            self.pickerView.dataSource = self
            self.pickerView.delegate = self
            self.pickerView.showsSelectionIndicator = true
            self.pickerView.backgroundColor = UIColor.white
            self.pickerView.isHidden = true
        }
        func setRecognitionButtonTitle(withText text: String, color: UIColor) {
            self.recognitionButton.setTitle(text, for: .normal)
            self.recognitionButton.setTitleColor(color, for: .normal)
        }
    }
    

    Was this article helpful?

    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.