MusicHandleSettingFragment.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. package com.cooleshow.musicmerge.ui;
  2. import android.Manifest;
  3. import android.annotation.SuppressLint;
  4. import android.content.pm.ActivityInfo;
  5. import android.os.Handler;
  6. import android.text.TextUtils;
  7. import android.view.Gravity;
  8. import android.view.View;
  9. import android.widget.ImageView;
  10. import android.widget.SeekBar;
  11. import android.widget.TextView;
  12. import com.alibaba.android.arouter.launcher.ARouter;
  13. import com.cooleshow.base.router.RouterPath;
  14. import com.cooleshow.base.ui.fragment.BaseFragment;
  15. import com.cooleshow.base.utils.FileUtils;
  16. import com.cooleshow.base.utils.MyFileUtils;
  17. import com.cooleshow.base.utils.PermissionUtils;
  18. import com.cooleshow.base.utils.ToastUtil;
  19. import com.cooleshow.base.utils.helper.GlideEngine;
  20. import com.cooleshow.base.widgets.DialogUtil;
  21. import com.cooleshow.musicmerge.R;
  22. import com.cooleshow.musicmerge.constants.MusicMergeConfig;
  23. import com.cooleshow.musicmerge.databinding.FgMusicHandleSettingLayoutBinding;
  24. import com.cooleshow.musicmerge.helper.MixHelper;
  25. import com.cooleshow.musicmerge.viewmodel.MusicMergeViewModel;
  26. import com.cooleshow.musicmerge.widget.UploadCoverTipDialog;
  27. import com.luck.picture.lib.PictureSelector;
  28. import com.luck.picture.lib.config.PictureConfig;
  29. import com.luck.picture.lib.config.PictureMimeType;
  30. import com.tbruyelle.rxpermissions3.RxPermissions;
  31. import org.json.JSONException;
  32. import org.json.JSONObject;
  33. import androidx.constraintlayout.widget.ConstraintLayout;
  34. import androidx.lifecycle.ViewModelProvider;
  35. /**
  36. * Author by pq, Date on 2023/8/28.
  37. */
  38. public class MusicHandleSettingFragment extends BaseFragment<FgMusicHandleSettingLayoutBinding> implements View.OnClickListener {
  39. public static final int MAX_OFFSET_SECTION = 1200;
  40. public static final int MIN_OFFSET_SECTION = MAX_OFFSET_SECTION / 10;
  41. public static final int ACCOMPANY_TYPE = 1;
  42. public static final int RECORD_TYPE = 2;
  43. private String accompanyUrl;
  44. private String worksId;
  45. private OnEventListener mEventListener;
  46. private MusicMergeViewModel mViewModel;
  47. private Runnable mRunnable = new Runnable() {
  48. @Override
  49. public void run() {
  50. if (mViewBinding != null) {
  51. mViewBinding.tvRecordVolumeValue.setVisibility(View.GONE);
  52. }
  53. }
  54. };
  55. private Runnable mRunnable2 = new Runnable() {
  56. @Override
  57. public void run() {
  58. if (mViewBinding != null) {
  59. mViewBinding.tvAccompanyVolumeValue.setVisibility(View.GONE);
  60. }
  61. }
  62. };
  63. @Override
  64. protected FgMusicHandleSettingLayoutBinding getLayoutView() {
  65. return FgMusicHandleSettingLayoutBinding.inflate(getLayoutInflater());
  66. }
  67. @Override
  68. protected void initView(View rootView) {
  69. mViewBinding.seekBarOffset.setMax(MAX_OFFSET_SECTION);
  70. mViewBinding.seekBarOffset.setProgress(MAX_OFFSET_SECTION / 2);
  71. formatProgress();
  72. }
  73. @Override
  74. protected void initData() {
  75. accompanyUrl = getArguments().getString("accompanyUrl");
  76. initViewModel();
  77. initListener();
  78. }
  79. private void initViewModel() {
  80. ViewModelProvider.AndroidViewModelFactory instance =
  81. ViewModelProvider.AndroidViewModelFactory
  82. .getInstance(getActivity().getApplication());
  83. mViewModel = new ViewModelProvider(getActivity(), instance)
  84. .get(MusicMergeViewModel.class);
  85. worksId = mViewModel.getWorksId().getValue();
  86. }
  87. private void initListener() {
  88. mViewBinding.ivAdd.setOnClickListener(this);
  89. mViewBinding.ivReduce.setOnClickListener(this);
  90. mViewBinding.tvSaveWorks.setOnClickListener(this);
  91. mViewBinding.tvRecord.setOnClickListener(this);
  92. mViewBinding.tvSave.setOnClickListener(this);
  93. mViewBinding.ivShrinkArrow.setOnClickListener(this);
  94. mViewBinding.seekVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  95. @Override
  96. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  97. if (mEventListener != null) {
  98. float value = mViewBinding.seekVolume.getProgress() * 1.0f / mViewBinding.seekVolume.getMax();
  99. mEventListener.onVolumeChange(RECORD_TYPE, value);
  100. if (fromUser) {
  101. setUpdateStatus();
  102. if (mViewBinding.tvRecordVolumeValue.getVisibility() == View.GONE) {
  103. mViewBinding.tvRecordVolumeValue.setVisibility(View.VISIBLE);
  104. }
  105. ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) mViewBinding.viewLine.getLayoutParams();
  106. layoutParams.horizontalBias = value;
  107. mViewBinding.viewLine.setLayoutParams(layoutParams);
  108. mViewBinding.tvRecordVolumeValue.setText(String.valueOf(mViewBinding.seekVolume.getProgress()));
  109. }
  110. }
  111. }
  112. @Override
  113. public void onStartTrackingTouch(SeekBar seekBar) {
  114. }
  115. @Override
  116. public void onStopTrackingTouch(SeekBar seekBar) {
  117. mViewBinding.tvRecordVolumeValue.postDelayed(mRunnable, 200);
  118. }
  119. });
  120. mViewBinding.seekVolume2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  121. @Override
  122. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  123. if (mEventListener != null) {
  124. float value = mViewBinding.seekVolume2.getProgress() * 1.0f / mViewBinding.seekVolume2.getMax();
  125. mEventListener.onVolumeChange(ACCOMPANY_TYPE, value);
  126. if (fromUser) {
  127. setUpdateStatus();
  128. if (mViewBinding.tvAccompanyVolumeValue.getVisibility() == View.GONE) {
  129. mViewBinding.tvAccompanyVolumeValue.setVisibility(View.VISIBLE);
  130. }
  131. ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) mViewBinding.viewLine2.getLayoutParams();
  132. layoutParams.horizontalBias = value;
  133. mViewBinding.viewLine2.setLayoutParams(layoutParams);
  134. mViewBinding.tvAccompanyVolumeValue.setText(String.valueOf(mViewBinding.seekVolume2.getProgress()));
  135. }
  136. }
  137. }
  138. @Override
  139. public void onStartTrackingTouch(SeekBar seekBar) {
  140. }
  141. @Override
  142. public void onStopTrackingTouch(SeekBar seekBar) {
  143. mViewBinding.tvAccompanyVolumeValue.postDelayed(mRunnable2, 200);
  144. }
  145. });
  146. mViewBinding.seekBarOffset.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  147. @Override
  148. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  149. if (roundOffProgress(progress)) {
  150. formatProgress();
  151. }
  152. if (fromUser) {
  153. setUpdateStatus();
  154. }
  155. }
  156. @Override
  157. public void onStartTrackingTouch(SeekBar seekBar) {
  158. }
  159. @Override
  160. public void onStopTrackingTouch(SeekBar seekBar) {
  161. }
  162. });
  163. }
  164. private void setUpdateStatus() {
  165. mViewModel.getUpdateEvent().postValue(true);
  166. }
  167. @Override
  168. public void onClick(View view) {
  169. int id = view.getId();
  170. if (id == R.id.tv_save_works) {
  171. showUploadCoverImgDialog();
  172. // if (checkUploadCover()) {
  173. // showUploadCoverImgDialog();
  174. // } else {
  175. // toCheckDownload(true);
  176. // }
  177. return;
  178. }
  179. if (id == R.id.iv_reduce) {
  180. changeOffsetValue(false);
  181. return;
  182. }
  183. if (id == R.id.iv_add) {
  184. changeOffsetValue(true);
  185. return;
  186. }
  187. if (id == R.id.tv_record) {
  188. if (getActivity() != null) {
  189. getActivity().finish();
  190. }
  191. return;
  192. }
  193. if (id == R.id.tv_save) {
  194. if (mEventListener != null) {
  195. mEventListener.saveDraft();
  196. }
  197. // toCheckDownload(false);
  198. return;
  199. }
  200. if (id == R.id.iv_shrink_arrow) {
  201. if (mEventListener != null) {
  202. mEventListener.hideSetting();
  203. }
  204. return;
  205. }
  206. }
  207. private void changeOffsetValue(boolean isAdd) {
  208. int value = isAdd ? MAX_OFFSET_SECTION / MIN_OFFSET_SECTION : -MAX_OFFSET_SECTION / MIN_OFFSET_SECTION;
  209. int currentValue = mViewBinding.seekBarOffset.getProgress();
  210. currentValue += value;
  211. if (currentValue < 0) {
  212. currentValue = 0;
  213. }
  214. if (currentValue > MAX_OFFSET_SECTION) {
  215. currentValue = MAX_OFFSET_SECTION;
  216. }
  217. mViewBinding.seekBarOffset.setProgress(currentValue);
  218. }
  219. @SuppressLint("CheckResult")
  220. private void toCheckDownload(boolean isNeedNotify) {
  221. new RxPermissions(MusicHandleSettingFragment.this)
  222. .request(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
  223. .subscribe(permission -> {
  224. if (permission) {
  225. checkDownload(isNeedNotify);
  226. } else {
  227. DialogUtil.showInCenter(MusicHandleSettingFragment.this.getChildFragmentManager(), com.cooleshow.base.R.layout.accompany_permissions_popu, (holder, dialog) -> {
  228. TextView tvTitle = holder.getView(com.cooleshow.base.R.id.tv_title);
  229. TextView tvContent = holder.getView(com.cooleshow.base.R.id.tv_content);
  230. ImageView btncancel = holder.getView(com.cooleshow.base.R.id.btn_cancel);
  231. ImageView btnCommit = holder.getView(com.cooleshow.base.R.id.btn_commit);
  232. tvTitle.setText("提示");
  233. tvContent.setText("请开储存访问权限");
  234. btncancel.setOnClickListener(view1 -> {
  235. dialog.dismiss();
  236. });
  237. btnCommit.setOnClickListener(view1 -> {
  238. PermissionUtils.toSelfSetting(getContext());
  239. dialog.dismiss();
  240. });
  241. });
  242. }
  243. });
  244. }
  245. private boolean checkUploadCover() {
  246. return TextUtils.isEmpty(worksId);
  247. }
  248. private void showUploadCoverImgDialog() {
  249. UploadCoverTipDialog coverTipDialog = new UploadCoverTipDialog();
  250. coverTipDialog.setGravity(Gravity.CENTER);
  251. coverTipDialog.setHeight(-1);
  252. coverTipDialog.setWidth(316);
  253. coverTipDialog.setEventListener(new UploadCoverTipDialog.OnEventListener() {
  254. @Override
  255. public void onUploadCover() {
  256. toAlbum(1, 1, MusicHandleActivity.REQUEST_CODE_LOCAL);
  257. }
  258. @Override
  259. public void onPublish(String des) {
  260. mViewModel.refreshMusicDes(des);
  261. toCheckDownload(true);
  262. }
  263. @Override
  264. public void onSelectVideoCover(boolean isByAlbum) {
  265. if (isByAlbum) {
  266. toAlbum(16, 9, MusicHandleActivity.REQUEST_CODE_LOCAL_VIDEO_COVER);
  267. } else {
  268. String value = mViewModel.getVideoFilePath().getValue();
  269. ARouter.getInstance().build(RouterPath.MusicTuner.MUSIC_SELECT_VIDEO_FRAME)
  270. .withString("videoFilePath", value)
  271. .withInt("c_orientation", ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
  272. .navigation(getActivity(), MusicHandleActivity.REQUEST_CODE_VIDEO_COVER);
  273. }
  274. }
  275. });
  276. coverTipDialog.show(getChildFragmentManager(), "");
  277. }
  278. private void toAlbum(int aspect_ratio_x, int aspect_ratio_y, int requestCode) {
  279. new RxPermissions(this)
  280. .request(Manifest.permission.CAMERA,
  281. Manifest.permission.READ_EXTERNAL_STORAGE,
  282. Manifest.permission.WRITE_EXTERNAL_STORAGE)
  283. .subscribe(granted -> {
  284. if (granted) {
  285. goAlbum(aspect_ratio_x, aspect_ratio_y, requestCode);
  286. } else {
  287. ToastUtil.getInstance().show(getContext(), "请打开存储和相机权限!");
  288. }
  289. });
  290. }
  291. private void goAlbum(int aspect_ratio_x, int aspect_ratio_y, int requestCode) {
  292. PictureSelector.create(getActivity())
  293. .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
  294. .loadImageEngine(GlideEngine.createGlideEngine())
  295. .theme(com.cooleshow.base.R.style.picture_daya_style)// 主题样式设置 具体参考 values/styles 用法:R .style.picture.white.style
  296. .selectionMode(PictureConfig.SINGLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
  297. .enableCrop(true)// 是否裁剪 true or false
  298. .withAspectRatio(aspect_ratio_x, aspect_ratio_y)
  299. .cutOutQuality(100)
  300. .showCropGrid(false)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false true or false
  301. .compress(true)// 是否压缩 true or false
  302. .circleDimmedLayer(false)// 是否圆形裁剪 true or false
  303. .forResult(requestCode);
  304. }
  305. private void checkDownload(boolean isNeedNotify) {
  306. String downloadSavePath = MixHelper.getInstance().getDownloadSavePath(accompanyUrl, MyFileUtils.MP3_FILE_SUFFIX);
  307. boolean fileExists = FileUtils.isFileExists(downloadSavePath);
  308. if (!fileExists) {
  309. return;
  310. }
  311. toMix(isNeedNotify);
  312. }
  313. private float getSeekVolume() {
  314. return mViewBinding.seekVolume.getProgress() * 1.0f / mViewBinding.seekVolume.getMax();
  315. }
  316. private float getAccompanySeekVolume() {
  317. return mViewBinding.seekVolume2.getProgress() * 1.0f / mViewBinding.seekVolume2.getMax();
  318. }
  319. private void toMix(boolean isNeedNotify) {
  320. int i = getOffsetValue();
  321. float recordFileVolume = getSeekVolume();
  322. float accompanyFileVolume = getAccompanySeekVolume();
  323. if (mEventListener != null) {
  324. mEventListener.toMix(i, recordFileVolume, accompanyFileVolume, isNeedNotify);
  325. }
  326. }
  327. private int formatProgress() {
  328. int lastValue = getOffsetValue();
  329. if (lastValue > 0) {
  330. mViewBinding.tvOffsetResult.setText("已为您对齐演奏伴奏,演奏提前" + lastValue + "毫秒");
  331. } else if (lastValue < 0) {
  332. mViewBinding.tvOffsetResult.setText("已为您对齐演奏伴奏,演奏延后" + Math.abs(lastValue) + "毫秒");
  333. } else {
  334. mViewBinding.tvOffsetResult.setText("演奏伴奏没有对齐?试试调整这里");
  335. }
  336. if (mEventListener != null) {
  337. mEventListener.onOffsetValueChange(lastValue);
  338. }
  339. return lastValue;
  340. }
  341. private int getOffsetValue() {
  342. if (mViewBinding != null) {
  343. int progress = mViewBinding.seekBarOffset.getProgress();
  344. int lastValue = progress - MAX_OFFSET_SECTION / 2;
  345. return lastValue;
  346. }
  347. return 0;
  348. }
  349. private int getProgressFromOffsetValue(int offset) {
  350. int progress = offset + MAX_OFFSET_SECTION / 2;
  351. return progress;
  352. }
  353. private boolean roundOffProgress(int progress) {
  354. int minSection = MAX_OFFSET_SECTION / MIN_OFFSET_SECTION;
  355. int result = progress % minSection;
  356. if (result == 0) {
  357. return true;
  358. }
  359. mViewBinding.seekBarOffset.setProgress(progress - result);
  360. return false;
  361. }
  362. public int getCurrentOffsetValue() {
  363. return getOffsetValue();
  364. }
  365. public int getOriginalVolume() {
  366. int originalVolume = mViewBinding.seekVolume.getProgress();
  367. return originalVolume;
  368. }
  369. public int getAccompanyVolume() {
  370. int accompanyVolume = mViewBinding.seekVolume2.getProgress();
  371. return accompanyVolume;
  372. }
  373. public String getConfigJson() {
  374. if (isDetached()) {
  375. return "";
  376. }
  377. if (!isAdded()) {
  378. return "";
  379. }
  380. int currentOffsetValue = getCurrentOffsetValue();
  381. int originalVolume = mViewBinding.seekVolume.getProgress();
  382. int accompanyVolume = mViewBinding.seekVolume2.getProgress();
  383. JSONObject jsonObject = new JSONObject();
  384. try {
  385. jsonObject.put(MusicMergeConfig.OFFSET_KEY, currentOffsetValue);
  386. jsonObject.put(MusicMergeConfig.ORIGINALVOLUME_KEY, originalVolume);
  387. jsonObject.put(MusicMergeConfig.ACCOMPANYVOLUME_KEY, accompanyVolume);
  388. return jsonObject.toString();
  389. } catch (JSONException e) {
  390. e.printStackTrace();
  391. }
  392. return "";
  393. }
  394. public void setEventListener(OnEventListener eventListener) {
  395. mEventListener = eventListener;
  396. }
  397. public void applyConfig(String jsonConfig) {
  398. try {
  399. JSONObject jsonObject = new JSONObject(jsonConfig);
  400. int offset = jsonObject.optInt(MusicMergeConfig.OFFSET_KEY, 0);
  401. int originalVolume = jsonObject.optInt(MusicMergeConfig.ORIGINALVOLUME_KEY, 100);
  402. int accompanyVolume = jsonObject.optInt(MusicMergeConfig.ACCOMPANYVOLUME_KEY, 100);
  403. int progressFromOffsetValue = getProgressFromOffsetValue(offset);
  404. mViewBinding.seekBarOffset.setProgress(progressFromOffsetValue);
  405. mViewBinding.seekVolume.setProgress(originalVolume);
  406. mViewBinding.seekVolume2.setProgress(accompanyVolume);
  407. mViewBinding.tvRecord.setVisibility(View.GONE);
  408. } catch (Exception e) {
  409. e.printStackTrace();
  410. }
  411. }
  412. public void setAccompanyUrl(String accompanyUrl) {
  413. this.accompanyUrl = accompanyUrl;
  414. }
  415. public interface OnEventListener {
  416. void onVolumeChange(int type, float value);
  417. void onOffsetValueChange(int value);
  418. void toMix(int offsetValue, float volume1, float volume2, boolean isNeedNotify);
  419. void hideSetting();
  420. void saveDraft();
  421. }
  422. @Override
  423. public void onDestroy() {
  424. removeCallBack(mViewBinding.tvRecordVolumeValue, mRunnable);
  425. removeCallBack(mViewBinding.tvAccompanyVolumeValue, mRunnable2);
  426. super.onDestroy();
  427. }
  428. private void removeCallBack(View view, Runnable runnable) {
  429. Handler handler = view.getHandler();
  430. if (handler != null) {
  431. handler.removeCallbacks(runnable);
  432. }
  433. }
  434. }