PageRenderTime 29ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/ecyber/ivivaanywhere/smartfm/popup/AudioRecordDialog.java

https://bitbucket.org/Fidenz-kelumw/ec_ivivaanyware_android
Java | 270 lines | 214 code | 52 blank | 4 comment | 8 complexity | 660a87f0c839c195dfeffcbbaee640d7 MD5 | raw file
  1. package com.ecyber.ivivaanywhere.smartfm.popup;
  2. import android.Manifest;
  3. import android.app.Activity;
  4. import android.app.Dialog;
  5. import android.content.Context;
  6. import android.content.pm.PackageManager;
  7. import android.graphics.drawable.ColorDrawable;
  8. import android.media.MediaPlayer;
  9. import android.media.MediaRecorder;
  10. import android.os.Environment;
  11. import android.os.SystemClock;
  12. import android.support.annotation.NonNull;
  13. import android.support.v7.app.AppCompatActivity;
  14. import android.util.DisplayMetrics;
  15. import android.view.LayoutInflater;
  16. import android.view.View;
  17. import android.view.ViewGroup;
  18. import android.widget.Chronometer;
  19. import android.widget.ImageButton;
  20. import com.ecyber.ivivaanywhere.smartfm.R;
  21. import com.ecyber.ivivaanywhere.smartfm.helper.utilities.AccountPermission;
  22. import com.ecyber.ivivaanywhere.smartfm.customviews.CustomButton;
  23. import com.ecyber.ivivaanywhere.smartfm.helper.utilities.DisplayPixelCalculator;
  24. import com.ecyber.ivivaanywhere.smartfm.helper.utilities.Utility;
  25. import java.io.IOException;
  26. /**
  27. * Created by diharaw on 3/6/17.
  28. */
  29. public class AudioRecordDialog extends AppCompatActivity {
  30. private Activity mActivity;
  31. private AudioRecordCallback mDelegate;
  32. private ImageButton mBtnPlayStop;
  33. private ImageButton mBtnRecord;
  34. private ImageButton mBtnReset;
  35. private Chronometer mRecordTime;
  36. private CustomButton mBtnSaveFile;
  37. private boolean mIsRecording;
  38. private boolean mIsPlaying;
  39. private Context mContext;
  40. private String mFileName = "recording.3gp";
  41. private String mUserId;
  42. private MediaRecorder mRecorder = null;
  43. private MediaPlayer mPlayer = null;
  44. private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
  45. // Requesting permission to RECORD_AUDIO
  46. private boolean permissionToRecordAccepted = false;
  47. private String [] permissions = {Manifest.permission.RECORD_AUDIO};
  48. public AudioRecordDialog(Context context, Activity activity, AudioRecordCallback delegate) {
  49. mActivity = activity;
  50. mDelegate = delegate;
  51. mContext = context;
  52. mUserId = new AccountPermission().getAccountInfo().get(2);
  53. }
  54. public void show() {
  55. final Dialog mAudioDialog = new Dialog(mActivity, R.style.CustomDialog);
  56. LayoutInflater inflater = LayoutInflater.from(mActivity.getApplicationContext());
  57. View view = inflater.inflate(R.layout.view_audio_dialog, null);
  58. mIsRecording = false;
  59. ImageButton btnClose = (ImageButton) view.findViewById(R.id.audio_dialog_close_btn);
  60. mBtnPlayStop = (ImageButton) view.findViewById(R.id.img_btn_play);
  61. mBtnRecord = (ImageButton) view.findViewById(R.id.img_btn_record);
  62. mBtnReset = (ImageButton) view.findViewById(R.id.img_btn_reset);
  63. mRecordTime = (Chronometer) view.findViewById(R.id.lbl_record_time);
  64. mBtnSaveFile = (CustomButton) view.findViewById(R.id.btn_save_audio);
  65. mRecordTime.setOnChronometerTickListener(cArg -> {
  66. long time = SystemClock.elapsedRealtime() - cArg.getBase();
  67. int h = (int)(time /3600000);
  68. int m = (int)(time - h*3600000)/60000;
  69. int s= (int)(time - h*3600000- m*60000)/1000 ;
  70. String hh = h < 10 ? "0"+h: h+"";
  71. String mm = m < 10 ? "0"+m: m+"";
  72. String ss = s < 10 ? "0"+s: s+"";
  73. cArg.setText(hh+":"+mm+":"+ss);
  74. });
  75. mRecordTime.setText("00:00:00");
  76. disableResetButton();
  77. disablePlayButton();
  78. enableRecordButton();
  79. btnClose.setOnClickListener(view1 -> mAudioDialog.dismiss());
  80. mBtnPlayStop.setOnClickListener(view12 -> {
  81. if(mIsPlaying) {
  82. stopPlayback();
  83. } else {
  84. startPlayback();
  85. mBtnPlayStop.setImageResource(R.drawable.stop_player);
  86. mIsPlaying = true;
  87. mRecordTime.setBase(SystemClock.elapsedRealtime());
  88. mRecordTime.start();
  89. }
  90. });
  91. mBtnRecord.setOnClickListener(view13 -> {
  92. if(mIsRecording) {
  93. stopRecording();
  94. mBtnRecord.setImageResource(R.drawable.mic);
  95. mRecordTime.setBase(SystemClock.elapsedRealtime());
  96. mRecordTime.stop();
  97. mRecordTime.setText("00:00:00");
  98. mIsRecording = false;
  99. } else {
  100. startRecording();
  101. mBtnRecord.setImageResource(R.drawable.stop);
  102. mRecordTime.setBase(SystemClock.elapsedRealtime());
  103. mRecordTime.start();
  104. mIsRecording = true;
  105. }
  106. });
  107. mBtnReset.setOnClickListener(view14 -> {
  108. enableRecordButton();
  109. disablePlayButton();
  110. disableResetButton();
  111. mRecordTime.stop();
  112. mRecordTime.setText("00:00:00");
  113. mBtnSaveFile.setEnabled(false);
  114. });
  115. mBtnSaveFile.setOnClickListener(view15 -> {
  116. if(!Utility.IsEmpty(mFileName)) {
  117. mDelegate.onSaveAudio(mFileName);
  118. mAudioDialog.dismiss();
  119. }
  120. });
  121. mAudioDialog.setContentView(view);
  122. mAudioDialog.show();
  123. mAudioDialog.setCanceledOnTouchOutside(false);
  124. mAudioDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
  125. DisplayMetrics displayMetrics = mActivity.getResources().getDisplayMetrics();
  126. float dpHeight = ((displayMetrics.heightPixels / displayMetrics.density) / 100) * 57;
  127. float dpWidth = ((displayMetrics.widthPixels / displayMetrics.density) / 100) * 90;
  128. final float x, y;
  129. DisplayPixelCalculator converter = new DisplayPixelCalculator();
  130. x = converter.dipToPixels(mActivity, dpWidth);
  131. y = converter.dipToPixels(mActivity, dpHeight);
  132. mAudioDialog.getWindow().setLayout((int)x, ViewGroup.LayoutParams.WRAP_CONTENT);
  133. mBtnSaveFile.setEnabled(false);
  134. }
  135. private void startRecording() {
  136. enableRecordButton();
  137. disablePlayButton();
  138. disableResetButton();
  139. mRecorder = new MediaRecorder();
  140. mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  141. mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
  142. mRecorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + mFileName);
  143. mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
  144. try {
  145. mRecorder.prepare();
  146. } catch (IOException e) {
  147. }
  148. mRecorder.start();
  149. }
  150. private void stopRecording() {
  151. disableRecordButton();
  152. enablePlayButton();
  153. enableResetButton();
  154. mRecorder.stop();
  155. mRecorder.release();
  156. mRecorder = null;
  157. mBtnSaveFile.setEnabled(true);
  158. }
  159. private void enablePlayButton() {
  160. mBtnPlayStop.setEnabled(true);
  161. mBtnPlayStop.setAlpha(1.0f);
  162. }
  163. private void disablePlayButton() {
  164. mBtnPlayStop.setEnabled(false);
  165. mBtnPlayStop.setAlpha(0.5f);
  166. }
  167. private void enableRecordButton() {
  168. mBtnRecord.setEnabled(true);
  169. mBtnRecord.setAlpha(1.0f);
  170. }
  171. private void disableRecordButton() {
  172. mBtnRecord.setEnabled(false);
  173. mBtnRecord.setAlpha(0.5f);
  174. }
  175. private void enableResetButton() {
  176. mBtnReset.setEnabled(true);
  177. mBtnReset.setAlpha(1.0f);
  178. }
  179. private void disableResetButton() {
  180. mBtnReset.setEnabled(false);
  181. mBtnReset.setAlpha(0.5f);
  182. }
  183. private void startPlayback() {
  184. disableRecordButton();
  185. enablePlayButton();
  186. enableResetButton();
  187. mPlayer = new MediaPlayer();
  188. try {
  189. mPlayer.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + mFileName);
  190. mPlayer.setOnCompletionListener(mp -> stopPlayback());
  191. mPlayer.prepare();
  192. mPlayer.start();
  193. } catch (IOException e) {
  194. }
  195. }
  196. private void stopPlayback() {
  197. mPlayer.release();
  198. mPlayer = null;
  199. mBtnPlayStop.setImageResource(R.drawable.play);
  200. mIsPlaying = false;
  201. mRecordTime.setBase(SystemClock.elapsedRealtime());
  202. mRecordTime.stop();
  203. mRecordTime.setText("00:00:00");
  204. }
  205. public interface AudioRecordCallback {
  206. void onSaveAudio(String fileName);
  207. }
  208. @Override
  209. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  210. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  211. switch (requestCode){
  212. case REQUEST_RECORD_AUDIO_PERMISSION:
  213. permissionToRecordAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
  214. break;
  215. }
  216. if (!permissionToRecordAccepted ) finish();
  217. }
  218. }