PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/yueai/src/main/java/com/yueai/activity/UploadAudioActivity.java

https://bitbucket.org/juzhiwiscom/datelove
Java | 1010 lines | 411 code | 57 blank | 542 comment | 53 complexity | 8ee58e17b4e7d63acad9b97c12584be8 MD5 | raw file
  1. package com.yueai.activity;
  2. import android.Manifest;
  3. import android.content.Context;
  4. import android.content.pm.PackageManager;
  5. import android.graphics.Color;
  6. import android.graphics.drawable.AnimationDrawable;
  7. import android.graphics.drawable.ColorDrawable;
  8. import android.graphics.drawable.Drawable;
  9. import android.os.Bundle;
  10. import android.os.Handler;
  11. import android.os.Message;
  12. import android.os.Vibrator;
  13. import android.support.annotation.NonNull;
  14. import android.support.v4.app.ActivityCompat;
  15. import android.support.v4.content.ContextCompat;
  16. import android.text.TextUtils;
  17. import android.view.Gravity;
  18. import android.view.LayoutInflater;
  19. import android.view.MotionEvent;
  20. import android.view.View;
  21. import android.view.WindowManager;
  22. import android.widget.Button;
  23. import android.widget.ImageView;
  24. import android.widget.PopupWindow;
  25. import android.widget.RelativeLayout;
  26. import android.widget.TextView;
  27. import com.alibaba.fastjson.JSON;
  28. //
  29. import com.alibaba.fastjson.JSON;
  30. import com.library.utils.LogUtil;
  31. import com.library.utils.ToastUtil;
  32. import com.library.utils.Util;
  33. import com.yueai.R;
  34. import com.yueai.base.BaseTitleActivity;
  35. import com.yueai.bean.BaseModel;
  36. import com.yueai.constant.IUrlConstant;
  37. import com.yueai.utils.FileUtil;
  38. import com.yueai.utils.RecordUtil;
  39. import com.yueai.utils.Utils;
  40. import com.yueai.xml.PlatformInfoXml;
  41. import com.yueai.xml.UserInfoXml;
  42. import com.zhy.http.okhttp.OkHttpUtils;
  43. import com.zhy.http.okhttp.callback.Callback;
  44. import java.io.File;
  45. import java.util.ArrayList;
  46. import java.util.List;
  47. import butterknife.BindView;
  48. import okhttp3.Call;
  49. import okhttp3.Response;
  50. //
  51. import com.library.utils.LogUtil;
  52. import com.library.utils.ToastUtil;
  53. import com.library.utils.Util;
  54. import com.zhy.http.okhttp.OkHttpUtils;
  55. import com.zhy.http.okhttp.callback.Callback;
  56. import java.io.File;
  57. import java.util.ArrayList;
  58. import java.util.List;
  59. import butterknife.BindView;
  60. import butterknife.ButterKnife;
  61. import okhttp3.Call;
  62. import okhttp3.Response;
  63. /**
  64. * Created by Administrator on 2017/1/13.
  65. */
  66. public class UploadAudioActivity extends BaseTitleActivity {
  67. @BindView(R.id.record)
  68. Button mBtnRecord;
  69. @BindView(R.id.audition)
  70. Button mBtnAudition;
  71. @BindView(R.id.upLoad)
  72. Button mBtnUpload;
  73. @BindView(R.id.skip)
  74. TextView skip;
  75. @BindView(R.id.iv_receive_voice_pic)
  76. ImageView ivReceiveVoicePic;
  77. @BindView(R.id.rl_receive_voice_viewbg)
  78. RelativeLayout rlReceiveVoiceViewbg;
  79. @BindView(R.id.tv_receive_voice_duration)
  80. TextView tvReceiveVoiceDuration;
  81. // 录音框
  82. private PopupWindow mRecordPopupWindow;
  83. private static final int PERMISSION_CODE_RECORD_AUDIO = 0;
  84. private static final int PERMISSION_CODE_WRITE_EXTERNAL_STORAGE = 1;
  85. private static final int MSG_TYPE_TIMER = 1;
  86. // 录音文件路径
  87. private String mOutputPath;
  88. // 录音文件名
  89. private String mRecordFileName;
  90. // 是否正在录音
  91. private boolean mIsRecording;
  92. // 录音是否被取消
  93. private boolean mIsRecordCanceled;
  94. // 录音时长(毫秒)
  95. private long mRecordDuration;
  96. // 录音计时刷新间隔(毫秒)
  97. private final long mRefreshInterval = 250;
  98. // 计算录音时长的Handler
  99. private Handler mTimerHandler = new Handler() {
  100. @Override
  101. public void handleMessage(Message msg) {
  102. super.handleMessage(msg);
  103. switch (msg.what) {
  104. case MSG_TYPE_TIMER:
  105. if (mIsRecording) {
  106. // 计时
  107. mRecordDuration += mRefreshInterval;
  108. // 录音超过1分钟自动发送
  109. if (mRecordDuration > 60 * 1000) {
  110. stopRecordAndSend();
  111. } else {
  112. mTimerHandler.sendEmptyMessageDelayed(MSG_TYPE_TIMER, mRefreshInterval);
  113. }
  114. }
  115. break;
  116. }
  117. }
  118. };
  119. private String audioSecond;
  120. private boolean needRequestPermission = false;
  121. @Override
  122. protected void onCreate(Bundle savedInstanceState) {
  123. super.onCreate(savedInstanceState);
  124. // TODO: add setContentView(...) invocation
  125. ButterKnife.bind(this);
  126. }
  127. // 录音状态常量
  128. private enum STATE {
  129. /**
  130. * 默认值
  131. */
  132. IDLE,
  133. /**
  134. * 录音中
  135. */
  136. RECORDING,
  137. /**
  138. * 取消
  139. */
  140. CANCELED,
  141. /**
  142. * 录音时间太短
  143. */
  144. TOO_SHORT
  145. }
  146. @Override
  147. protected int getLayoutResId() {
  148. return R.layout.activity_upload_audio;
  149. }
  150. @Override
  151. protected String getCenterTitle() {
  152. return getString(R.string.upload_audio_title);
  153. }
  154. @Override
  155. protected void initViewsAndVariables() {
  156. // 隐藏返回按钮
  157. mIvLeft.setVisibility(View.GONE);
  158. }
  159. @Override
  160. protected void addListeners() {
  161. skip.setOnClickListener(new View.OnClickListener() {
  162. @Override
  163. public void onClick(View view) {
  164. Util.gotoActivity(UploadAudioActivity.this, MainActivity.class, true);
  165. }
  166. });
  167. // 检查权限
  168. if (!needRequestPermission) {// 请求权限时不录音
  169. checkPermissions();
  170. }
  171. mBtnRecord.setOnTouchListener(new View.OnTouchListener() {
  172. @Override
  173. public boolean onTouch(View v, MotionEvent event) {
  174. // // 检查权限
  175. if (!checkPermissions()) {// 请求权限时不录音
  176. switch (event.getAction()) {
  177. case MotionEvent.ACTION_DOWN:// 按下时开始录音
  178. mBtnRecord.setText(getString(R.string.loose_to_end));
  179. showRecordPopupWindow(STATE.RECORDING);
  180. startRecord();
  181. mIsRecordCanceled = false;
  182. LogUtil.e(LogUtil.TAG_ZL, "ACTION_DOWN start()");
  183. break;
  184. case MotionEvent.ACTION_MOVE:
  185. if (event.getY() < -100) { // 上滑取消发送
  186. mIsRecordCanceled = true;
  187. showRecordPopupWindow(STATE.CANCELED);
  188. } else {
  189. mIsRecordCanceled = false;
  190. showRecordPopupWindow(STATE.RECORDING);
  191. }
  192. LogUtil.e(LogUtil.TAG_ZL, "ACTION_MOVE " + event.getY() + " mIsRecordCanceled = " + mIsRecordCanceled);
  193. break;
  194. case MotionEvent.ACTION_UP:// 松开时停止录音并发送
  195. mBtnRecord.setText(getString(R.string.press_to_speak));
  196. if (mIsRecordCanceled) {
  197. cancelRecord();
  198. LogUtil.e(LogUtil.TAG_ZL, "ACTION_UP canceld()");
  199. } else {
  200. stopRecordAndSend();
  201. LogUtil.e(LogUtil.TAG_ZL, "ACTION_UP stop()");
  202. }
  203. break;
  204. }
  205. }
  206. return true;
  207. }
  208. });
  209. mBtnUpload.setOnClickListener(new View.OnClickListener() {
  210. @Override
  211. public void onClick(View v) {
  212. if (mRecordDuration < 1 * 1000) {
  213. ToastUtil.showShortToast(UploadAudioActivity.this, UploadAudioActivity.this.getString(R.string.record_short));
  214. } else {
  215. uploadAudio();
  216. }
  217. }
  218. });
  219. //开始设置监听
  220. final ImageView ieaLlSinger = ivReceiveVoicePic;
  221. mBtnAudition.setOnClickListener(new View.OnClickListener() {
  222. @Override
  223. public void onClick(View v) {
  224. if (mOutputPath != null) {
  225. File file = new File(mOutputPath);
  226. if (file != null && file.exists()) {
  227. final AnimationDrawable animationDrawable = (AnimationDrawable) ieaLlSinger.getBackground();
  228. //获取语音信号栏,开始播放动画
  229. animationDrawable.start();
  230. mBtnAudition.setText(getResources().getString(R.string.pause));
  231. if (RecordUtil.getInstance().mPlayerIsNull()) {
  232. RecordUtil.getInstance().play(mOutputPath, new RecordUtil.OnPlayerListener() {
  233. @Override
  234. public void onCompleted() {
  235. mBtnAudition.setText(getResources().getString(R.string.audition));
  236. animationDrawable.stop();
  237. rlReceiveVoiceViewbg.setVisibility(View.GONE);
  238. }
  239. @Override
  240. public void onPaused() {
  241. }
  242. });
  243. rlReceiveVoiceViewbg.setVisibility(View.VISIBLE);
  244. } else if (RecordUtil.getInstance().isPlaying()) {
  245. RecordUtil.getInstance().pause();
  246. mBtnAudition.setText(getResources().getString(R.string.go_on));
  247. animationDrawable.stop();
  248. rlReceiveVoiceViewbg.setVisibility(View.GONE);
  249. } else {
  250. RecordUtil.getInstance().startPaly(new RecordUtil.OnPlayerListener() {
  251. @Override
  252. public void onCompleted() {
  253. mBtnAudition.setText(getResources().getString(R.string.audition));
  254. animationDrawable.stop();
  255. rlReceiveVoiceViewbg.setVisibility(View.GONE);
  256. }
  257. @Override
  258. public void onPaused() {
  259. }
  260. });
  261. rlReceiveVoiceViewbg.setVisibility(View.VISIBLE);
  262. animationDrawable.start();
  263. mBtnAudition.setText(getResources().getString(R.string.pause));
  264. }
  265. } else {
  266. ToastUtil.showShortToast(UploadAudioActivity.this, getResources().getString(R.string.not_exist));
  267. }
  268. } else {
  269. ToastUtil.showShortToast(UploadAudioActivity.this, getResources().getString(R.string.not_exist));
  270. }
  271. }
  272. });
  273. // mBtnAudition.setOnClickListener(new View.OnClickListener() {
  274. // @Override
  275. // public void onClick(View v) {
  276. // if (mOutputPath != null) {
  277. // File file = new File(mOutputPath);
  278. // boolean exists = file.exists();
  279. // if (file != null && exists) {
  280. // // 播放/暂停语音
  281. // RecordUtil.getInstance().toggle(mOutputPath, new RecordUtil.OnPlayerListener() {
  282. //
  283. // @Override
  284. // public void onCompleted() {
  285. // }
  286. //
  287. // @Override
  288. // public void onPaused() {
  289. // }
  290. // });
  291. // } else {
  292. // ToastUtil.showShortToast(UploadAudioActivity.this, getResources().getString(R.string.not_exist));
  293. // }
  294. // } else {
  295. // ToastUtil.showShortToast(UploadAudioActivity.this, getResources().getString(R.string.not_exist));
  296. // }
  297. // }
  298. // });
  299. }
  300. @Override
  301. public void onBackPressed() {
  302. // 屏蔽返回键
  303. }
  304. private boolean checkPermissions() {
  305. needRequestPermission = false;
  306. if (ContextCompat.checkSelfPermission(UploadAudioActivity.this.getApplicationContext(),
  307. Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_DENIED) {
  308. needRequestPermission = true;
  309. // 请求android.permission.RECORD_AUDIO权限
  310. ActivityCompat.requestPermissions(UploadAudioActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, PERMISSION_CODE_RECORD_AUDIO);
  311. }
  312. if (ContextCompat.checkSelfPermission(UploadAudioActivity.this.getApplicationContext(),
  313. Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
  314. needRequestPermission = true;
  315. // 请求android.permission.WRITE_EXTERNAL_STORAGE权限
  316. ActivityCompat.requestPermissions(UploadAudioActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_CODE_WRITE_EXTERNAL_STORAGE);
  317. }
  318. return needRequestPermission;
  319. }
  320. @Override
  321. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  322. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  323. }
  324. // private boolean checkPermissions() {
  325. // needRequestPermission = false;
  326. // if (ContextCompat.checkSelfPermission(UploadAudioActivity.this.getApplicationContext(),
  327. // Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_DENIED) {
  328. // needRequestPermission = true;
  329. // // 请求android.permission.RECORD_AUDIO权限
  330. // ActivityCompat.requestPermissions(UploadAudioActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, PERMISSION_CODE_RECORD_AUDIO);
  331. // }
  332. // if (ContextCompat.checkSelfPermission(UploadAudioActivity.this.getApplicationContext(),
  333. // Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
  334. // needRequestPermission = true;
  335. // // 请求android.permission.WRITE_EXTERNAL_STORAGE权限
  336. // ActivityCompat.requestPermissions(UploadAudioActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_CODE_WRITE_EXTERNAL_STORAGE);
  337. // }
  338. // return needRequestPermission;
  339. // }
  340. //
  341. // @Override
  342. // public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  343. // super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  344. // }
  345. /**
  346. * 开始录音
  347. */
  348. private void startRecord() {
  349. // 录音开始前震动一下
  350. Vibrator vibrator = (Vibrator) UploadAudioActivity.this.getSystemService(Context.VIBRATOR_SERVICE);
  351. vibrator.vibrate(88);
  352. // 根据系统时间生成文件名
  353. mRecordFileName = "DateLove" + ".mp3";
  354. // 录音文件保存路径: 根目录/Amor/record/用户id/xxx.mp3
  355. mOutputPath = FileUtil.RECORD_DIRECTORY_PATH + File.separator + UserInfoXml.getUID() + File.separator + mRecordFileName;
  356. LogUtil.e(LogUtil.TAG_ZL, "录音存放路径:" + mOutputPath);
  357. // 开始录音
  358. RecordUtil.getInstance().startRecord(mOutputPath);
  359. mIsRecording = true;
  360. mRecordDuration = 0;
  361. // 计时,间隔250毫秒刷新
  362. mTimerHandler.sendEmptyMessageDelayed(MSG_TYPE_TIMER, mRefreshInterval);
  363. }
  364. /**
  365. * 停止录音并发送
  366. */
  367. private void stopRecordAndSend() {
  368. if (mIsRecording) {
  369. mIsRecording = false;
  370. LogUtil.e(LogUtil.TAG_ZL, "录音时长(毫秒):" + mRecordDuration);
  371. if (mRecordDuration < 1 * 1000) {// 录音时长小于1秒的不发送
  372. showRecordPopupWindow(STATE.TOO_SHORT);
  373. new Handler().postDelayed(new Runnable() {
  374. @Override
  375. public void run() {
  376. // 延时500毫秒调用MediaRecorder#stop(),防止出现start()之后立马调用stop()的异常
  377. RecordUtil.getInstance().stopRecord();
  378. // 删除小于1秒的文件
  379. FileUtil.deleteFile(mOutputPath);
  380. }
  381. }, 500);
  382. } else {// 发送语音
  383. RecordUtil.getInstance().stopRecord();
  384. }
  385. // 隐藏录音框
  386. new Handler().postDelayed(new Runnable() {
  387. @Override
  388. public void run() {
  389. if (mRecordPopupWindow != null && mRecordPopupWindow.isShowing()) {
  390. mRecordPopupWindow.dismiss();
  391. }
  392. }
  393. }, 500);
  394. }
  395. }
  396. /**
  397. * 录音取消,删除已经录制的文件
  398. */
  399. private void cancelRecord() {
  400. if (mIsRecordCanceled) {
  401. RecordUtil.getInstance().stopRecord();
  402. mIsRecordCanceled = false;
  403. FileUtil.deleteFile(mOutputPath);
  404. // 隐藏录音框
  405. new Handler().postDelayed(new Runnable() {
  406. @Override
  407. public void run() {
  408. if (mRecordPopupWindow != null && mRecordPopupWindow.isShowing()) {
  409. mRecordPopupWindow.dismiss();
  410. }
  411. }
  412. }, 500);
  413. }
  414. }
  415. /**
  416. * 弹出录音框
  417. */
  418. private void showRecordPopupWindow(STATE state) {
  419. if (mRecordPopupWindow == null) {
  420. View contentView = LayoutInflater.from(UploadAudioActivity.this).inflate(R.layout.popup_chat_record, null);
  421. mRecordPopupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.WRAP_CONTENT,
  422. WindowManager.LayoutParams.WRAP_CONTENT, true);
  423. mRecordPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  424. }
  425. View view = mRecordPopupWindow.getContentView();
  426. if (view != null) {
  427. RelativeLayout rlRecording = (RelativeLayout) view.findViewById(R.id.popup_recording_container);
  428. ImageView ivRecording = (ImageView) view.findViewById(R.id.popup_record_anim);
  429. // 设置动画
  430. List<Drawable> drawableList = new ArrayList<Drawable>();
  431. drawableList.add(getResources().getDrawable(R.drawable.voice1));
  432. drawableList.add(getResources().getDrawable(R.drawable.voice2));
  433. drawableList.add(getResources().getDrawable(R.drawable.voice3));
  434. drawableList.add(getResources().getDrawable(R.drawable.voice4));
  435. drawableList.add(getResources().getDrawable(R.drawable.voice5));
  436. drawableList.add(getResources().getDrawable(R.drawable.voice6));
  437. AnimationDrawable animationDrawable = Utils.getFrameAnim(drawableList, true, 150);
  438. ivRecording.setImageDrawable(animationDrawable);
  439. ImageView ivCancel = (ImageView) view.findViewById(R.id.popup_cancel_record);
  440. ImageView ivTooShort = (ImageView) view.findViewById(R.id.popup_record_too_short);
  441. TextView tvState = (TextView) view.findViewById(R.id.popup_record_state);
  442. switch (state) {
  443. case RECORDING: // 正在录音
  444. rlRecording.setVisibility(View.VISIBLE);
  445. // 播放动画
  446. animationDrawable.start();
  447. ivCancel.setVisibility(View.GONE);
  448. ivTooShort.setVisibility(View.GONE);
  449. tvState.setText(getString(R.string.slideup_to_cancel));
  450. break;
  451. case CANCELED: // 取消录音
  452. rlRecording.setVisibility(View.GONE);
  453. // 停止动画
  454. if (animationDrawable.isRunning()) {
  455. animationDrawable.stop();
  456. }
  457. ivCancel.setVisibility(View.VISIBLE);
  458. ivTooShort.setVisibility(View.GONE);
  459. tvState.setText(getString(R.string.loose_to_cancel));
  460. break;
  461. case TOO_SHORT:// 录音时间太短
  462. rlRecording.setVisibility(View.GONE);
  463. // 停止动画
  464. if (animationDrawable.isRunning()) {
  465. animationDrawable.stop();
  466. }
  467. ivCancel.setVisibility(View.GONE);
  468. ivTooShort.setVisibility(View.VISIBLE);
  469. tvState.setText(getString(R.string.record_too_short));
  470. break;
  471. }
  472. }
  473. mRecordPopupWindow.showAtLocation(mBtnRecord, Gravity.CENTER, 0, -300);
  474. }
  475. /**
  476. * 上传语音
  477. */
  478. private void uploadAudio() {
  479. showLoading();
  480. OkHttpUtils.post()
  481. .url(IUrlConstant.URL_UPLOAD_INTRODUCE_VOICE)
  482. .addHeader("token", PlatformInfoXml.getToken())
  483. .addHeader("platformInfo", PlatformInfoXml.getPlatformJsonString())
  484. .addHeader("audioSecond", String.valueOf(Math.round(mRecordDuration / 1000)))
  485. .addFile("request", "DateLove.mp3", new File(mOutputPath))
  486. .build()
  487. .execute(new Callback<BaseModel>() {
  488. @Override
  489. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  490. String resultJson = response.body().string();
  491. if (!TextUtils.isEmpty(resultJson)) {
  492. return JSON.parseObject(resultJson, BaseModel.class);
  493. }
  494. return null;
  495. }
  496. @Override
  497. public void onError(Call call, Exception e, int id) {
  498. dismissLoading();
  499. ToastUtil.showShortToast(UploadAudioActivity.this, getString(R.string.upload_audio_fail));
  500. }
  501. @Override
  502. public void onResponse(BaseModel response, int id) {
  503. if (response != null) {
  504. String isSucceed = response.getIsSucceed();
  505. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  506. String message = response.getMsg();
  507. if (!TextUtils.isEmpty(message)) {
  508. ToastUtil.showShortToast(UploadAudioActivity.this, message);
  509. }
  510. dismissLoading();
  511. Util.gotoActivity(UploadAudioActivity.this, MainActivity.class, true);
  512. } else {
  513. dismissLoading();
  514. ToastUtil.showShortToast(UploadAudioActivity.this, getString(R.string.upload_audio_fail));
  515. }
  516. } else {
  517. dismissLoading();
  518. ToastUtil.showShortToast(UploadAudioActivity.this, getString(R.string.upload_audio_fail));
  519. }
  520. }
  521. });
  522. }
  523. }
  524. //
  525. //import android.Manifest;
  526. //import android.content.Context;
  527. //import android.content.pm.PackageManager;
  528. //import android.graphics.Color;
  529. //import android.graphics.drawable.AnimationDrawable;
  530. //import android.graphics.drawable.ColorDrawable;
  531. //import android.graphics.drawable.Drawable;
  532. //import android.os.Handler;
  533. //import android.os.Vibrator;
  534. //import android.support.annotation.NonNull;
  535. //import android.support.v4.app.ActivityCompat;
  536. //import android.support.v4.content.ContextCompat;
  537. //import android.text.TextUtils;
  538. //import android.view.Gravity;
  539. //import android.view.LayoutInflater;
  540. //import android.view.MotionEvent;
  541. //import android.view.View;
  542. //import android.view.WindowManager;
  543. //import android.widget.Button;
  544. //import android.widget.ImageView;
  545. //import android.widget.PopupWindow;
  546. //import android.widget.RelativeLayout;
  547. //import android.widget.TextView;
  548. //
  549. //import com.alibaba.fastjson.JSON;
  550. //import com.library.utils.LogUtil;
  551. //import com.library.utils.ToastUtil;
  552. //import com.library.utils.Util;
  553. //import com.yueai.R;
  554. //import com.yueai.base.BaseTitleActivity;
  555. //import com.yueai.bean.BaseModel;
  556. //import com.yueai.constant.IUrlConstant;
  557. //import com.yueai.utils.FileUtil;
  558. //import com.yueai.utils.RecordUtil;
  559. //import com.yueai.utils.Utils;
  560. //import com.yueai.xml.PlatformInfoXml;
  561. //import com.yueai.xml.UserInfoXml;
  562. //import com.zhy.http.okhttp.OkHttpUtils;
  563. //import com.zhy.http.okhttp.callback.Callback;
  564. //
  565. //import java.io.File;
  566. //import java.util.ArrayList;
  567. //import java.util.List;
  568. //
  569. //import butterknife.BindView;
  570. //import okhttp3.Call;
  571. //import okhttp3.Response;
  572. //
  573. ///**
  574. // * Created by Administrator on 2017/1/13.
  575. // */
  576. //public class UploadAudioActivity extends BaseTitleActivity {
  577. // @BindView(R.id.record)
  578. // Button mBtnRecord;
  579. // @BindView(R.id.audition)
  580. // Button mBtnAudition;
  581. // @BindView(R.id.upLoad)
  582. // Button mBtnUpload;
  583. //
  584. // // 录音框
  585. // private PopupWindow mRecordPopupWindow;
  586. // private static final int PERMISSION_CODE_RECORD_AUDIO = 0;
  587. // private static final int PERMISSION_CODE_WRITE_EXTERNAL_STORAGE = 1;
  588. // private static final int MSG_TYPE_TIMER = 1;
  589. //
  590. // // 录音文件路径
  591. // private String mOutputPath;
  592. // // 录音文件名
  593. // private String mRecordFileName;
  594. // // 是否正在录音
  595. // private boolean mIsRecording;
  596. // // 录音是否被取消
  597. // private boolean mIsRecordCanceled;
  598. // // 录音时长(毫秒)
  599. // private long mRecordDuration;
  600. // // 录音计时刷新间隔(毫秒)
  601. // private final long mRefreshInterval = 250;
  602. // // 计算录音时长的Handler
  603. // private Handler mTimerHandler = new Handler() {
  604. //
  605. // @Override
  606. // public void handleMessage(android.os.Message msg) {
  607. // super.handleMessage(msg);
  608. // switch (msg.what) {
  609. // case MSG_TYPE_TIMER:
  610. // if (mIsRecording) {
  611. // // 计时
  612. // mRecordDuration += mRefreshInterval;
  613. // // 录音超过1分钟自动发送
  614. // if (mRecordDuration > 60 * 1000) {
  615. // stopRecordAndSend();
  616. // } else {
  617. // mTimerHandler.sendEmptyMessageDelayed(MSG_TYPE_TIMER, mRefreshInterval);
  618. // }
  619. // }
  620. // break;
  621. // }
  622. // }
  623. // };
  624. //
  625. // // 录音状态常量
  626. // private enum STATE {
  627. // /**
  628. // * 默认值
  629. // */
  630. // IDLE,
  631. // /**
  632. // * 录音中
  633. // */
  634. // RECORDING,
  635. // /**
  636. // * 取消
  637. // */
  638. // CANCELED,
  639. // /**
  640. // * 录音时间太短
  641. // */
  642. // TOO_SHORT
  643. // }
  644. //
  645. // @Override
  646. // protected int getLayoutResId() {
  647. // return R.layout.activity_upload_audio;
  648. // }
  649. //
  650. // @Override
  651. // protected String getCenterTitle() {
  652. // return getString(R.string.upload_audio_title);
  653. // }
  654. //
  655. // @Override
  656. // protected void initViewsAndVariables() {
  657. // // 隐藏返回按钮
  658. // mIvLeft.setVisibility(View.GONE);
  659. // }
  660. //
  661. // @Override
  662. // protected void addListeners() {
  663. // mBtnRecord.setOnTouchListener(new View.OnTouchListener() {
  664. // @Override
  665. // public boolean onTouch(View v, MotionEvent event) {
  666. // // 检查权限
  667. // if (!checkPermissions()) {// 请求权限时不录音
  668. // switch (event.getAction()) {
  669. // case MotionEvent.ACTION_DOWN:// 按下时开始录音
  670. // mBtnRecord.setText(getString(R.string.loose_to_end));
  671. // showRecordPopupWindow(UploadAudioActivity.STATE.RECORDING);
  672. // startRecord();
  673. // mIsRecordCanceled = false;
  674. // LogUtil.e(LogUtil.TAG_ZL, "ACTION_DOWN start()");
  675. // break;
  676. //
  677. // case MotionEvent.ACTION_MOVE:
  678. // if (event.getY() < -100) { // 上滑取消发送
  679. // mIsRecordCanceled = true;
  680. // showRecordPopupWindow(UploadAudioActivity.STATE.CANCELED);
  681. // } else {
  682. // mIsRecordCanceled = false;
  683. // showRecordPopupWindow(UploadAudioActivity.STATE.RECORDING);
  684. // }
  685. // LogUtil.e(LogUtil.TAG_ZL, "ACTION_MOVE " + event.getY() + " mIsRecordCanceled = " + mIsRecordCanceled);
  686. // break;
  687. //
  688. // case MotionEvent.ACTION_UP:// 松开时停止录音并发送
  689. // mBtnRecord.setText(getString(R.string.press_to_speak));
  690. // if (mIsRecordCanceled) {
  691. // cancelRecord();
  692. // LogUtil.e(LogUtil.TAG_ZL, "ACTION_UP canceld()");
  693. // } else {
  694. // stopRecordAndSend();
  695. // LogUtil.e(LogUtil.TAG_ZL, "ACTION_UP stop()");
  696. // }
  697. // break;
  698. // }
  699. // }
  700. // return true;
  701. // }
  702. // });
  703. // mBtnUpload.setOnClickListener(new View.OnClickListener() {
  704. // @Override
  705. // public void onClick(View v) {
  706. // uploadAudio();
  707. // }
  708. // });
  709. // mBtnAudition.setOnClickListener(new View.OnClickListener() {
  710. // @Override
  711. // public void onClick(View v) {
  712. // if (mOutputPath != null) {
  713. // File file = new File(mOutputPath);
  714. // if (file != null && file.exists()) {
  715. // // 播放/暂停语音
  716. // RecordUtil.getInstance().toggle(mOutputPath, new RecordUtil.OnPlayerListener() {
  717. //
  718. // @Override
  719. // public void onCompleted() {
  720. // }
  721. //
  722. // @Override
  723. // public void onPaused() {
  724. // }
  725. // });
  726. // } else {
  727. // ToastUtil.showShortToast(UploadAudioActivity.this, getResources().getString(R.string.not_exist));
  728. // }
  729. // } else {
  730. // ToastUtil.showShortToast(UploadAudioActivity.this, getResources().getString(R.string.not_exist));
  731. // }
  732. // }
  733. // });
  734. // }
  735. //
  736. // @Override
  737. // public void onBackPressed() {
  738. // // 屏蔽返回键
  739. // }
  740. //
  741. // private boolean checkPermissions() {
  742. // boolean needRequestPermission = false;
  743. // if (ContextCompat.checkSelfPermission(UploadAudioActivity.this.getApplicationContext(),
  744. // Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_DENIED) {
  745. // needRequestPermission = true;
  746. // // 请求android.permission.RECORD_AUDIO权限
  747. // ActivityCompat.requestPermissions(UploadAudioActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, PERMISSION_CODE_RECORD_AUDIO);
  748. // }
  749. // if (ContextCompat.checkSelfPermission(UploadAudioActivity.this.getApplicationContext(),
  750. // Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
  751. // needRequestPermission = true;
  752. // // 请求android.permission.WRITE_EXTERNAL_STORAGE权限
  753. // ActivityCompat.requestPermissions(UploadAudioActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_CODE_WRITE_EXTERNAL_STORAGE);
  754. // }
  755. // return needRequestPermission;
  756. // }
  757. //
  758. // @Override
  759. // public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  760. // super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  761. // }
  762. //
  763. // /**
  764. // * 开始录音
  765. // */
  766. // private void startRecord() {
  767. // // 录音开始前震动一下
  768. // Vibrator vibrator = (Vibrator) UploadAudioActivity.this.getSystemService(Context.VIBRATOR_SERVICE);
  769. // vibrator.vibrate(88);
  770. // // 根据系统时间生成文件名
  771. // mRecordFileName = "DateLove" + ".mp3";
  772. // // 录音文件保存路径: 根目录/Amor/record/用户id/xxx.mp3
  773. // mOutputPath = FileUtil.RECORD_DIRECTORY_PATH + File.separator + UserInfoXml.getUID() + File.separator + mRecordFileName;
  774. // LogUtil.e(LogUtil.TAG_ZL, "录音存放路径:" + mOutputPath);
  775. // // 开始录音
  776. // RecordUtil.getInstance().startRecord(mOutputPath);
  777. // mIsRecording = true;
  778. // mRecordDuration = 0;
  779. // // 计时,间隔250毫秒刷新
  780. // mTimerHandler.sendEmptyMessageDelayed(MSG_TYPE_TIMER, mRefreshInterval);
  781. // }
  782. //
  783. // /**
  784. // * 停止录音并发送
  785. // */
  786. // private void stopRecordAndSend() {
  787. // if (mIsRecording) {
  788. // mIsRecording = false;
  789. // LogUtil.e(LogUtil.TAG_ZL, "录音时长(毫秒):" + mRecordDuration);
  790. // if (mRecordDuration < 1 * 1000) {// 录音时长小于1秒的不发送
  791. // showRecordPopupWindow(STATE.TOO_SHORT);
  792. // new Handler().postDelayed(new Runnable() {
  793. // @Override
  794. // public void run() {
  795. // // 延时500毫秒调用MediaRecorder#stop(),防止出现start()之后立马调用stop()的异常
  796. // RecordUtil.getInstance().stopRecord();
  797. // // 删除小于1秒的文件
  798. // FileUtil.deleteFile(mOutputPath);
  799. // }
  800. // }, 500);
  801. // } else {// 发送语音
  802. // RecordUtil.getInstance().stopRecord();
  803. // }
  804. // // 隐藏录音框
  805. // new Handler().postDelayed(new Runnable() {
  806. // @Override
  807. // public void run() {
  808. // if (mRecordPopupWindow != null && mRecordPopupWindow.isShowing()) {
  809. // mRecordPopupWindow.dismiss();
  810. // }
  811. // }
  812. // }, 500);
  813. // }
  814. // }
  815. //
  816. // /**
  817. // * 录音取消,删除已经录制的文件
  818. // */
  819. // private void cancelRecord() {
  820. // if (mIsRecordCanceled) {
  821. // RecordUtil.getInstance().stopRecord();
  822. // mIsRecordCanceled = false;
  823. // FileUtil.deleteFile(mOutputPath);
  824. //
  825. // // 隐藏录音框
  826. // new Handler().postDelayed(new Runnable() {
  827. // @Override
  828. // public void run() {
  829. // if (mRecordPopupWindow != null && mRecordPopupWindow.isShowing()) {
  830. // mRecordPopupWindow.dismiss();
  831. // }
  832. // }
  833. // }, 500);
  834. // }
  835. // }
  836. //
  837. // /**
  838. // * 弹出录音框
  839. // */
  840. // private void showRecordPopupWindow(STATE state) {
  841. // if (mRecordPopupWindow == null) {
  842. // View contentView = LayoutInflater.from(UploadAudioActivity.this).inflate(R.layout.popup_chat_record, null);
  843. // mRecordPopupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.WRAP_CONTENT,
  844. // WindowManager.LayoutParams.WRAP_CONTENT, true);
  845. // mRecordPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  846. // }
  847. //
  848. // View view = mRecordPopupWindow.getContentView();
  849. // if (view != null) {
  850. // RelativeLayout rlRecording = (RelativeLayout) view.findViewById(R.id.popup_recording_container);
  851. // ImageView ivRecording = (ImageView) view.findViewById(R.id.popup_record_anim);
  852. // // 设置动画
  853. // List<Drawable> drawableList = new ArrayList<Drawable>();
  854. // drawableList.add(getResources().getDrawable(R.drawable.voice1));
  855. // drawableList.add(getResources().getDrawable(R.drawable.voice2));
  856. // drawableList.add(getResources().getDrawable(R.drawable.voice3));
  857. // drawableList.add(getResources().getDrawable(R.drawable.voice4));
  858. // drawableList.add(getResources().getDrawable(R.drawable.voice5));
  859. // drawableList.add(getResources().getDrawable(R.drawable.voice6));
  860. // AnimationDrawable animationDrawable = Utils.getFrameAnim(drawableList, true, 150);
  861. // ivRecording.setImageDrawable(animationDrawable);
  862. //
  863. // ImageView ivCancel = (ImageView) view.findViewById(R.id.popup_cancel_record);
  864. // ImageView ivTooShort = (ImageView) view.findViewById(R.id.popup_record_too_short);
  865. // TextView tvState = (TextView) view.findViewById(R.id.popup_record_state);
  866. // switch (state) {
  867. // case RECORDING: // 正在录音
  868. // rlRecording.setVisibility(View.VISIBLE);
  869. // // 播放动画
  870. // animationDrawable.start();
  871. // ivCancel.setVisibility(View.GONE);
  872. // ivTooShort.setVisibility(View.GONE);
  873. // tvState.setText(getString(R.string.slideup_to_cancel));
  874. // break;
  875. //
  876. // case CANCELED: // 取消录音
  877. // rlRecording.setVisibility(View.GONE);
  878. // // 停止动画
  879. // if (animationDrawable.isRunning()) {
  880. // animationDrawable.stop();
  881. // }
  882. // ivCancel.setVisibility(View.VISIBLE);
  883. // ivTooShort.setVisibility(View.GONE);
  884. // tvState.setText(getString(R.string.loose_to_cancel));
  885. // break;
  886. //
  887. // case TOO_SHORT:// 录音时间太短
  888. // rlRecording.setVisibility(View.GONE);
  889. // // 停止动画
  890. // if (animationDrawable.isRunning()) {
  891. // animationDrawable.stop();
  892. // }
  893. // ivCancel.setVisibility(View.GONE);
  894. // ivTooShort.setVisibility(View.VISIBLE);
  895. // tvState.setText(getString(R.string.record_too_short));
  896. // break;
  897. // }
  898. // }
  899. // mRecordPopupWindow.showAtLocation(mBtnRecord, Gravity.CENTER, 0, -300);
  900. // }
  901. //
  902. // /**
  903. // * 上传语音
  904. // */
  905. // private void uploadAudio() {
  906. // showLoading();
  907. // OkHttpUtils.post()
  908. // .url(IUrlConstant.URL_UPLOAD_INTRODUCE_VOICE)
  909. // .addHeader("token", PlatformInfoXml.getToken())
  910. // .addHeader("platformInfo", PlatformInfoXml.getPlatformJsonString())
  911. // .addHeader("audioSecond", String.valueOf(Math.round(mRecordDuration / 1000)))
  912. // .addFile("request", "DateLove.mp3", new File(mOutputPath))
  913. // .build()
  914. // .execute(new Callback<BaseModel>() {
  915. // @Override
  916. // public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  917. // String resultJson = response.body().string();
  918. // if (!TextUtils.isEmpty(resultJson)) {
  919. // return JSON.parseObject(resultJson, BaseModel.class);
  920. // }
  921. // return null;
  922. // }
  923. //
  924. // @Override
  925. // public void onError(Call call, Exception e, int id) {
  926. // dismissLoading();
  927. // ToastUtil.showShortToast(UploadAudioActivity.this, getString(R.string.upload_audio_fail));
  928. // }
  929. //
  930. // @Override
  931. // public void onResponse(BaseModel response, int id) {
  932. // if (response != null) {
  933. // String isSucceed = response.getIsSucceed();
  934. // if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  935. // String message = response.getMsg();
  936. // if (!TextUtils.isEmpty(message)) {
  937. // ToastUtil.showShortToast(UploadAudioActivity.this, message);
  938. // }
  939. // dismissLoading();
  940. // Util.gotoActivity(UploadAudioActivity.this, MainActivity.class, true);
  941. // } else {
  942. // dismissLoading();
  943. // ToastUtil.showShortToast(UploadAudioActivity.this, getString(R.string.upload_audio_fail));
  944. // }
  945. // } else {
  946. // dismissLoading();
  947. // ToastUtil.showShortToast(UploadAudioActivity.this, getString(R.string.upload_audio_fail));
  948. // }
  949. // }
  950. // });
  951. // }
  952. //
  953. //}