Mobile SDKのユースケース
- 印刷する
- PDF
Mobile SDKのユースケース
- 印刷する
- PDF
記事の要約
この要約は役に立ちましたか?
ご意見ありがとうございます
Classic/VPC環境で利用できます。
CLOVA Speech Recognition(CSR) Mobile SDK APIを使用するユースケースを紹介します。
Mobile SDK API
音声認識に使用する言語と音声データの入力を受け、その認識結果をモバイル環境でテキストに変換する Mobile SDK APIのユースケースを説明します。
Java
Android環境での、Javaベースの Mobile SDK APIのサンプルコードは次の通りです。
サンプルコードの保存先アドレス: https://github.com/NaverCloudPlatform/naverspeech-sdk-android
説明
- Main Activityクラス:
SpeechRecognitionListener
を初期化し、その後にイベントを handleMessageから受けて処理します。 - SpeechRecognitionListenerを継承したクラス: 音声認識サーバへの接続、音声の伝達、認識結果発生などのイベントによる結果の処理方法を定義します。
- Main Activityクラス:
// 1. Main Activityクラス
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private static final String CLIENT_ID = "YOUR CLIENT ID"; // 「マイアプリケーション」で Client IDを確認し、ここに入力してください。
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: // 音声認識準備完了
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: // 最終認識結果
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(); // 音声認識サーバの初期化はこちら
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(); // 音声認識サーバ終了
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. 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
iOS環境での、ObjectiveCベースの Mobile SDK APIのサンプルコードは次の通りです。
- サンプルコードの保存先アドレス: https://github.com/NaverCloudPlatform/naverspeech-sdk-ios
import UIKit
import NaverSpeech
import Common
let ClientID = "YOUR_CLIENT_ID"
class AutoViewController: UIViewController {
required init?(coder aDecoder: NSCoder) { // NSKRecognizer初期化に必要な NSKRecognizerConfigurationを作成
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) { // ボタンを押すと音声認識を開始
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 { //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 { //音声の認識中に言語が変更されると音声の認識を直ちに停止(cancel)
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)
}
}
この記事は役に立ちましたか?