/ItemID/src/com/ideal/itemid/VoiceRecorderRecordingView.java

http://eyes-free.googlecode.com/ · Java · 137 lines · 105 code · 13 blank · 19 comment · 3 complexity · d1d842abea25f8383fbcad0634b2c533 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 The IDEAL Group
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.ideal.itemid;
  17. import android.content.Context;
  18. import android.graphics.Canvas;
  19. import android.graphics.Color;
  20. import android.graphics.Paint;
  21. import android.graphics.Typeface;
  22. import android.media.MediaRecorder;
  23. import android.view.MotionEvent;
  24. import android.widget.TextView;
  25. import java.io.File;
  26. import java.io.IOException;
  27. /**
  28. * The main View for IDEAL Item ID Label Maker. Handles the touch
  29. * events and does the recording.
  30. */
  31. public class VoiceRecorderRecordingView extends TextView {
  32. public VoiceRecorderActivity parent;
  33. private MediaRecorder recorder;
  34. boolean screenIsBeingTouched = false;
  35. public VoiceRecorderRecordingView(Context context) {
  36. super(context);
  37. parent = (VoiceRecorderActivity) context;
  38. setClickable(true);
  39. setFocusable(true);
  40. setFocusableInTouchMode(true);
  41. requestFocus();
  42. screenIsBeingTouched = false;
  43. parent.mTts
  44. .speak(
  45. "Touch the screen, speak your message, and let go of the screen when you are done.",
  46. 0, null);
  47. }
  48. @Override
  49. public boolean onTouchEvent(MotionEvent event) {
  50. int action = event.getAction();
  51. long[] pattern = {
  52. 0, 1, 40, 41
  53. };
  54. switch (action) {
  55. case MotionEvent.ACTION_DOWN:
  56. screenIsBeingTouched = true;
  57. parent.mTts.playEarcon("[tock]", 0, null);
  58. parent.mVibe.vibrate(pattern, -1);
  59. startRecording();
  60. break;
  61. case MotionEvent.ACTION_UP:
  62. saveRecording();
  63. parent.mTts.playEarcon("[tock]", 0, null);
  64. parent.mVibe.vibrate(pattern, -1);
  65. parent.showConfirmationView();
  66. break;
  67. default:
  68. break;
  69. }
  70. invalidate();
  71. return true;
  72. }
  73. private void startRecording() {
  74. File outputDir = new File("/sdcard/idealItemId/");
  75. outputDir.mkdirs();
  76. recorder = new MediaRecorder();
  77. recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  78. recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  79. recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  80. recorder.setOutputFile("/sdcard/idealItemId/" + parent.FILE_TIMESTAMP + ".amr");
  81. try {
  82. recorder.prepare();
  83. recorder.start();
  84. } catch (IllegalStateException e) {
  85. e.printStackTrace();
  86. } catch (IOException e) {
  87. e.printStackTrace();
  88. }
  89. }
  90. private void saveRecording() {
  91. recorder.stop();
  92. recorder.release();
  93. }
  94. @Override
  95. public void onDraw(Canvas canvas) {
  96. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  97. paint.setColor(Color.WHITE);
  98. paint.setTextAlign(Paint.Align.CENTER);
  99. paint.setTypeface(Typeface.DEFAULT_BOLD);
  100. int x = getWidth() / 2;
  101. int y = (getHeight() / 2) - 35;
  102. if (!screenIsBeingTouched) {
  103. x = 5;
  104. y = getHeight() - 40;
  105. paint.setTextSize(20);
  106. paint.setTextAlign(Paint.Align.LEFT);
  107. y -= paint.ascent() / 2;
  108. canvas.drawText("Touch screen to record.", x, y, paint);
  109. x = 5;
  110. y = getHeight() - 20;
  111. paint.setTextSize(20);
  112. paint.setTextAlign(Paint.Align.LEFT);
  113. y -= paint.ascent() / 2;
  114. canvas.drawText("Lift up when done.", x, y, paint);
  115. } else {
  116. x = 5;
  117. y = getHeight() - 40;
  118. paint.setTextSize(20);
  119. paint.setTextAlign(Paint.Align.LEFT);
  120. y -= paint.ascent() / 2;
  121. canvas.drawText("Lift up when you are done.", x, y, paint);
  122. }
  123. }
  124. }