2016-10-27 2 views
1

Android를 사용할 때 Codename One 앱에서 음성 인식을 통합 할 수 있기를 바 랐기 때문에 작은 예제를 만들려고했습니다. Codename One의 Android에서 음성 인식 간단한 예제

나는 새로운 응용 프로그램을 작성하고 난 다음 this link을 따라, 그래서 기본 액세스하기위한 인터페이스 생성 :

public interface SpeechRecognition extends NativeInterface { 
    void startListening(); 
    String getResult(); 
} 

주요 형태는 다음과 같습니다

SpeechRecognition speechRecognition = NativeLookup.create(SpeechRecognition.class); 

    Form hi = new Form("Hi World"); 
    Button startButton = new Button("start"); 
    Button stopButton = new Button("stop"); 
    Label label = new Label("non"); 
    startButton.addActionListener(event -> { 
     if (speechRecognition != null && speechRecognition.isSupported()) { 
      speechRecognition.startListening(); 
     } 
    }); 

    stopButton.addActionListener(event -> { 
     if (speechRecognition != null && speechRecognition.isSupported()) { 
      label.setText(speechRecognition.getResult()); 
     } 
    }); 
    hi.addComponent(startButton); 
    hi.addComponent(stopButton); 
    hi.addComponent(label); 
    hi.show(); 
을 여기에 내가 무슨 짓을

기본적으로 "시작"을 클릭 할 때마다 음성 인식을 시작하여 청취를 시작하고 중지 할 때마다 인식 된 결과로 레이블을 업데이트하고 싶습니다. 그런 다음 네이티브 액세스를 생성하고 안드로이드 구현에서 다음을 사용했습니다.

public class SpeechRecognitionImpl { 
//maybe use a getter for message to get the last event 
static String message = ""; 
static String speech = ""; 
private SpeechRecognizer sr; 

public void startListening() { 
    //initialize the Intent 

    final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
      RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); 

    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
      RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); 
    //number of guesses 
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3); 
    //Speechrecognition must be run on main Thread 

    Activity activity = AndroidNativeUtil.getActivity(); 
    activity.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      sr = SpeechRecognizer.createSpeechRecognizer(AndroidNativeUtil.getActivity()); 
      sr.setRecognitionListener(new listener()); 
      sr.startListening(intent); 
     } 
    }); 
} 


public String getResult() { 
    return speech; 
} 

public boolean isSupported() { 
    return true; 
} 

class listener implements RecognitionListener { 

    public void onReadyForSpeech(Bundle params) { 
     message = "onReadyForSpeech"; 
    } 

    public void onBeginningOfSpeech() { 
     message = "onBeginningOfSpeech"; 
    } 

    public void onRmsChanged(float rmsdB) { 
     message = "onRmsChanged"; 
    } 

    public void onBufferReceived(byte[] buffer) { 
     message = "onBufferReceived"; 
    } 

    public void onEndOfSpeech() { 
     message = "onEndofSpeech"; 
    } 

    public void onError(int error) { 
     message = "error " + error; 
    } 

    public void onResults(Bundle results) { 
     //here you have what google understand from the speech 
     //maybe only save the first guess, which would have the highest 
     //possibility 
     speech += "on results"; 
     String str = ""; 

     ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
     for (int i = 0; i < data.size(); i++) { 

      str += data.get(i)+";"; 
     } 
     speech += str; 
    } 

    public void onPartialResults(Bundle partialResults) { 
     message = "onPartialResults"; 

    } 

    public void onEvent(int eventType, Bundle params) { 

     message = "onEvent " + eventType; 
    } 
} 

이것은 기본적으로 "해야"하는 예제를 붙여 넣습니다. 그러나, 내 안드로이드 애플 리케이션에서 그것을 실행할 때, 아무 일도 일어나지 않습니다. 누구든지 성공 했나요?

답변

0

DDMS로 앱을 실행하고 앱의 로그를 봐야합니다. 빌드 힌트를 사용하여 매니페스트에 대한 권한을 추가하지 못한 것 같습니다.

+1

빌드 힌트 추가 android.xpermissions = <사용 권한 android : name = "android.permission.RECORD_AUDIO"/> 이 문제를 해결했습니다. –