프로그래밍/android2013. 5. 12. 18:40


   
public class MainActivity extends Activity
{
	private ImageView _imgview;
	private SpeechRecognizer _sr;
	private TypedArray _imgs;


	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		_imgview = (ImageView) findViewById(R.id.imageView1);

		Button btn = (Button) findViewById(R.id.button1);
		btn.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				_sr.cancel();

				Intent kIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
				kIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
				kIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ko-KR");
				kIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Google Speech recognition");
				kIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
//				kIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, new Long(1000));
				_sr.startListening(kIntent);
			}
		});

		_sr = SpeechRecognizer.createSpeechRecognizer(this);
		_sr.setRecognitionListener(onSRListener);

		_imgs = getResources().obtainTypedArray(R.array.volume_list);
	}


	@Override
	protected void onDestroy()
	{
		_sr.destroy();
		super.onDestroy();
	}

	private RecognitionListener onSRListener = new RecognitionListener()
	{
		@Override
		public void onRmsChanged(float rmsdB)
		{
			if (rmsdB <= 0)
			{
				_imgview.setImageDrawable(_imgs.getDrawable(0));
				return;
			}

			if (rmsdB >= 10)
			{
				_imgview.setImageDrawable(_imgs.getDrawable(_imgs.length() - 1));
				return;
			}

			int level = (int) rmsdB;
			Log.w("MainActivity.java | onRmsChanged", "=== " + level + " | " + rmsdB + " | " + _imgs.getString(level));
			_imgview.setImageDrawable(_imgs.getDrawable(level + 1));
		}


		@Override
		public void onReadyForSpeech(Bundle params){}
		@Override
		public void onResults(Bundle result){}
		@Override
		public void onError(int error){}
		@Override
		public void onPartialResults(Bundle results){}
		@Override
		public void onEvent(int eventType, Bundle params){}
		@Override
		public void onEndOfSpeech(){}
		@Override
		public void onBufferReceived(byte[] buffer){}
		@Override
		public void onBeginningOfSpeech(){}
	};
}





   
<resources>

    <array name="volume_list">
        <item>@drawable/a0</item>
        <item>@drawable/a1</item>
        <item>@drawable/a2</item>
        <item>@drawable/a3</item>
        <item>@drawable/a4</item>
        <item>@drawable/a5</item>
        <item>@drawable/a6</item>
        <item>@drawable/a7</item>
        <item>@drawable/a8</item>
        <item>@drawable/a9</item>
        <item>@drawable/a10</item>
    </array>

</resources>




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




TestAnimationDrawable.zip


Posted by 쎄미

댓글을 달아 주세요