(개발정보-앱)키보드 어플에 보이스타이핑 기능 추가하기

84 views
Skip to first unread message

한영빈

unread,
Mar 12, 2012, 3:39:28 AM3/12/12
to iad-dev...@googlegroups.com
아주 조흔 정보를 발견했습니다 하핳하하하하하하.
보이스타이핑이라고 안드로이드4.0부터 제공되는 기능인대,
안드로이드 디벨로퍼 홈페이지에 키보등앱에다가 보이스타이필 기능을 추가하는 법이 나왔군요.
아마(?)프로요부터 될거 같냉 ㅇㅅㅇ
기존 음성인식이 우리가 말을 하면 인식하고 처리한다음 인식된걸 띄어주지만
보이스타이핑은 그거랑 달리 실시간으로 인식을 하고 띄어줍니다. 조흔 기능이다하하하하하하ㅏㅎㅎㅎ 
물론 인터넷 연결 필요.
아래는 퍼온 내용.


Add Voice Typing To Your IME

Posted by Tim Bray on 12 December 2011 at 1:40 PM

[This post is by Luca Zanolin, an Android engineer who works on voice typing. — Tim Bray]

A new feature available in Android 4.0 is voice typing: the difference for users is that the recognition results appear in the text box while they are still speaking. If you are an IME developer, you can easily integrate with voice typing.

To simplify the integration, if you download this library and modify your IME as described below, everything will work smoothly on any device with Android 2.2 or later. On 4.0+, users will get voice typing, and earlier versions will use standard voice recognition; the difference is illustrated below.

To see how to integrate voice typing you can take a look at this sample IME. The IME is really simple and contains only one button: a microphone. By pressing the microphone, the user triggers voice recognition.

Here are the steps that you need to follow to integrate voice recognition into your IME.

Download the library

Download this library and add it to your IME APK.

Create the voice recognition trigger

The library contains the VoiceRecognitionTrigger helper class. Create an instance of it inside the InputMethodService#onCreate method in your IME.

public void onCreate() {
   
super.onCreate();
   
...
    mVoiceRecognitionTrigger
= new VoiceRecognitionTrigger(this);
}

Add the microphone icon to your IME

You need to modify the UI of your IME, add a microphone icon, and register an OnClickListener to trigger voice recognition. You can find the assets inside the sample IME. The microphone icon should be displayed only if voice recognition is installed; use VoiceRecognitionTrigger#isInstalled().

public View onCreateInputView() {
 
LayoutInflater inflater = (LayoutInflater) getSystemService(
     
Service.LAYOUT_INFLATER_SERVICE);
  mView
= inflater.inflate(R.layout.ime, null);
 
...
  mButton
= (ImageButton) mView.findViewById(R.id.mic_button);
 
if (mVoiceRecognitionTrigger.isInstalled()) {
    mButton
.setOnClickListener(new OnClickListener() {
     
@Override
     
public void onClick(View v) {
        mVoiceRecognitionTrigger
.startVoiceRecognition();
     
}
   
});
    mButton
.setVisibility(View.VISIBLE);
 
} else {
    mButton
.setVisibility(View.GONE);
 
}
 
return mView;
}

If your IME supports multiple languages, you can specify in which language recognition should be done as a parameter of startVoiceRecognition().

Notify the trigger when your IME starts

When your IME starts, you need to notify the trigger, so it can insert into the text view any pending recognition results.

@Override
public void onStartInputView(EditorInfo info, boolean restarting) {
 
super.onStartInputView(info, restarting);
 
if (mVoiceRecognitionTrigger != null) {
    mVoiceRecognitionTrigger
.onStartInputView();
 
}
}

Modify your AndroidManifest

In order to start a voice recognition through the Intent API, the library uses a service and an activity, and you need to add them into your manifest.

<manifest ... >
 
<application ...>
    ...
   
<service android:name="com.google.android.voiceime.ServiceHelper" />
   
<activity
       
android:name="com.google.android.voiceime.ActivityHelper"
       
android:theme="@android:style/Theme.Translucent.NoTitleBar"
       
android:excludeFromRecents="true"
       
android:windowSoftInputMode="stateAlwaysHidden"
       
android:finishOnTaskLaunch="true"
       
android:configChanges="keyboard|keyboardHidden|navigation
                               |orientation"
/>
 
</application>
</manifest>

Update the microphone icon dynamically (optional)

This step is optional, but you should implement it if possible as it will improve the user experience. Voice recognition requires network access, and if there is no network, your IME should notify the user that voice recognition is currently disabled. To achieve this, you need to register theVoiceRecognitionTrigger.Listener and enable/disable the microphone accordingly.

The listener is registered in InputMethodService#onCreate, and you have to unregister it in InputMethodService#onDestroy, otherwise you will leak the listener.

@Override
public void onCreate() {
 
super.onCreate();
 
...
  mVoiceRecognitionTrigger
= new VoiceRecognitionTrigger(this);
  mVoiceRecognitionTrigger
.register(new VoiceRecognitionTrigger.Listener() {
   
@Override
   
public void onVoiceImeEnabledStatusChange() {
      updateVoiceImeStatus
();
   
}
 
});
}

...
@Override
public void onDestroy() {
 
...
 
if (mVoiceRecognitionTrigger != null) {
    mVoiceRecognitionTrigger
.unregister(this);
 
}
 
super.onDestroy();
}

private void updateVoiceImeStatus() {
 
if (mVoiceRecognitionTrigger.isInstalled()) {
    mButton
.setVisibility(View.VISIBLE);
   
if (mVoiceRecognitionTrigger.isEnabled()) {
      mButton
.setEnabled(true);
   
} else {
      mButton
.setEnabled(false);
   
}
 
} else {
    mButton
.setVisibility(View.GONE);
 
}
  mView
.invalidate();
}

And add this permission into your manifest:

<manifest ... >
  ...
 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  ...
</manifest>

That’s all there is to it

Voice recognition makes it easy for users to do more with their Android devices, so we appreciate your support in adding it to your IMEs.



Reply all
Reply to author
Forward
0 new messages