MusicHandleActivity.java 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  1. package com.cooleshow.musicmerge.ui;
  2. import android.animation.ObjectAnimator;
  3. import android.content.DialogInterface;
  4. import android.content.Intent;
  5. import android.content.pm.ActivityInfo;
  6. import android.graphics.Color;
  7. import android.graphics.Rect;
  8. import android.graphics.SurfaceTexture;
  9. import android.graphics.drawable.Drawable;
  10. import android.media.MediaPlayer;
  11. import android.os.Bundle;
  12. import android.os.Handler;
  13. import android.os.Looper;
  14. import android.text.TextUtils;
  15. import android.util.Log;
  16. import android.view.Gravity;
  17. import android.view.KeyEvent;
  18. import android.view.Surface;
  19. import android.view.TextureView;
  20. import android.view.View;
  21. import android.view.animation.LinearInterpolator;
  22. import android.widget.FrameLayout;
  23. import android.widget.SeekBar;
  24. import com.alibaba.android.arouter.facade.annotation.Route;
  25. import com.alibaba.android.arouter.launcher.ARouter;
  26. import com.cooleshow.base.bean.ShareIntentBean;
  27. import com.cooleshow.base.common.WebConstants;
  28. import com.cooleshow.base.constanst.Constants;
  29. import com.cooleshow.base.constanst.ShareType;
  30. import com.cooleshow.base.constanst.UploadConstants;
  31. import com.cooleshow.base.router.RouterPath;
  32. import com.cooleshow.base.ui.activity.BaseMVPActivity;
  33. import com.cooleshow.base.utils.ClipboardUtils;
  34. import com.cooleshow.base.utils.FileUtils;
  35. import com.cooleshow.base.utils.GlideUtils;
  36. import com.cooleshow.base.utils.GsonUtils;
  37. import com.cooleshow.base.utils.JumpUtils;
  38. import com.cooleshow.base.utils.LOG;
  39. import com.cooleshow.base.utils.MyFileUtils;
  40. import com.cooleshow.base.utils.NumberUtils;
  41. import com.cooleshow.base.utils.SizeUtils;
  42. import com.cooleshow.base.utils.TimeUtils;
  43. import com.cooleshow.base.utils.ToastUtil;
  44. import com.cooleshow.base.utils.UrlUtils;
  45. import com.cooleshow.base.utils.helper.CommonShareHelper;
  46. import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
  47. import com.cooleshow.base.utils.helper.upload.UploadHelper;
  48. import com.cooleshow.base.widgets.dialog.CommonConfirmDialog;
  49. import com.cooleshow.base.widgets.dialog.CommonConfirmDialog2;
  50. import com.cooleshow.base.widgets.dialog.ShareDialog;
  51. import com.cooleshow.musicmerge.R;
  52. import com.cooleshow.musicmerge.bean.MusicDataBean;
  53. import com.cooleshow.musicmerge.bean.MusicInfoBean;
  54. import com.cooleshow.musicmerge.bean.MusicMergeConfigBean;
  55. import com.cooleshow.musicmerge.callback.ResultCallback;
  56. import com.cooleshow.musicmerge.contract.MusicFileHandleContract;
  57. import com.cooleshow.musicmerge.databinding.AcMusicHandleLayoutBinding;
  58. import com.cooleshow.musicmerge.helper.MixHelper;
  59. import com.cooleshow.musicmerge.player.CustomPlayer;
  60. import com.cooleshow.musicmerge.presenter.MusicFileHandlePresenter;
  61. import com.cooleshow.musicmerge.viewmodel.MusicMergeViewModel;
  62. import com.cooleshow.musicmerge.widget.MergeLoadingTipDialog;
  63. import com.luck.picture.lib.PictureSelector;
  64. import com.luck.picture.lib.entity.LocalMedia;
  65. import com.umeng.socialize.UMShareAPI;
  66. import com.umeng.socialize.UMShareListener;
  67. import com.umeng.socialize.bean.SHARE_MEDIA;
  68. import java.io.File;
  69. import java.util.List;
  70. import androidx.annotation.NonNull;
  71. import androidx.annotation.Nullable;
  72. import androidx.lifecycle.ViewModelProvider;
  73. /**
  74. * Author by pq, Date on 2023/8/28.
  75. */
  76. @Route(path = RouterPath.MusicTuner.MUSIC_MERGE_PAGE)
  77. public class MusicHandleActivity extends BaseMVPActivity<AcMusicHandleLayoutBinding, MusicFileHandlePresenter> implements View.OnClickListener, MusicFileHandleContract, TextureView.SurfaceTextureListener, UMShareListener {
  78. public static final int REQUEST_CODE_LOCAL = 0x19;
  79. public static final int REQUEST_CODE_LOCAL_VIDEO_COVER = 0x20;
  80. public static final int REQUEST_CODE_VIDEO_COVER = 0x29;
  81. public static final int MAX_ADJUSTMENT = 10;
  82. private CustomPlayer player1;
  83. private CustomPlayer player2;
  84. private String accompanyUrl;
  85. private String recordFilePath;
  86. private MusicHandleSettingFragment mSettingFragment;
  87. private boolean isVideo;
  88. private TextureView mSurfaceView;
  89. private SurfaceTexture mSurfaceTexture;
  90. private int videoWidth;
  91. private int videoHeight;
  92. private Handler mHandler = new Handler(Looper.getMainLooper());
  93. private String mRecordId;
  94. private String worksId;
  95. private String imgCover;
  96. private String des;
  97. private ObjectAnimator mRotateAnimation;
  98. private ObjectAnimator mAlbumRotationAnimator;
  99. private String originalFileUrl = "";
  100. private String mTitle;
  101. private MusicMergeViewModel mViewModel;
  102. private boolean isNeedFinishPage = false;
  103. private boolean isNeedResetScreenOrientation = true;
  104. private MergeLoadingTipDialog mLoadingTipDialog;
  105. private static int MAX_STEP = 3;
  106. private static final int MAX_PROGRESS = 100;
  107. private int currentStep = 0;
  108. private int firstPlayDuration = 0;
  109. private int secondPlayDuration = 0;
  110. private int defaultDelay = 0;//此字段用于录音文件前面缓冲以及设备延迟造成的空白延迟字段
  111. private int evaluateDelay = 0;//此字段记录给IOS使用
  112. @Override
  113. protected void onCreate(@Nullable Bundle savedInstanceState) {
  114. super.onCreate(savedInstanceState);
  115. QMUIStatusBarHelper.hideStatusBar(this);
  116. }
  117. @Override
  118. protected void initView() {
  119. mTitle = getIntent().getStringExtra("title");
  120. viewBinding.tvTitle.setText(mTitle);
  121. int c_orientation = getIntent().getIntExtra("c_orientation", ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  122. isNeedResetScreenOrientation = c_orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
  123. }
  124. private void initFragment() {
  125. mSettingFragment = new MusicHandleSettingFragment();
  126. Bundle bundle = new Bundle();
  127. bundle.putString("accompanyUrl", accompanyUrl);
  128. bundle.putInt("defaultDelay", defaultDelay);
  129. mSettingFragment.setArguments(bundle);
  130. getSupportFragmentManager().beginTransaction().replace(R.id.fl_setting, mSettingFragment).commitAllowingStateLoss();
  131. }
  132. private void initSurfaceView() {
  133. initVideoUIStyle();
  134. mViewModel.getVideoFilePath().setValue(recordFilePath);
  135. viewBinding.groupAudioView.setVisibility(View.GONE);
  136. viewBinding.flSurface.setVisibility(View.VISIBLE);
  137. viewBinding.viewVideoTopBg.setVisibility(View.VISIBLE);
  138. viewBinding.viewVideoBottomBg.setVisibility(View.VISIBLE);
  139. viewBinding.viewVideoBg.setVisibility(View.VISIBLE);
  140. mSurfaceView = new TextureView(MusicHandleActivity.this);
  141. mSurfaceView.setSurfaceTextureListener(this);
  142. FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  143. layoutParams.gravity = Gravity.CENTER;
  144. viewBinding.flSurface.addView(mSurfaceView, layoutParams);
  145. }
  146. private void initVideoUIStyle() {
  147. viewBinding.tvTitle.setTextColor(Color.WHITE);
  148. viewBinding.ivBack.setImageResource(com.cooleshow.base.R.drawable.ic_back_white);
  149. viewBinding.ivPlay.setImageResource(R.drawable.icon_music_merge_play_white);
  150. viewBinding.tvCurrentProgress.setTextColor(Color.WHITE);
  151. viewBinding.tvTotalProgress.setTextColor(Color.WHITE);
  152. viewBinding.seekPlay.setThumb(getResources().getDrawable(R.drawable.shape_volume_progreesbar_thumb2));
  153. setSeekBarProgressDrawable(viewBinding.seekPlay, getResources().getDrawable(R.drawable.shape_play_progress_seekbar_bg2));
  154. viewBinding.tvTotalProgress2.setTextColor(Color.WHITE);
  155. viewBinding.seekPlay2.setThumb(getResources().getDrawable(R.drawable.shape_volume_progreesbar_thumb2));
  156. viewBinding.tvCurrentProgress2.setTextColor(Color.WHITE);
  157. setSeekBarProgressDrawable(viewBinding.seekPlay2, getResources().getDrawable(R.drawable.shape_play_progress_seekbar_bg2));
  158. }
  159. private void setSeekBarProgressDrawable(SeekBar seekBarProgress, Drawable drawable) {
  160. Rect bounds = seekBarProgress.getProgressDrawable().getBounds();
  161. seekBarProgress.setProgressDrawable(drawable);
  162. seekBarProgress.getProgressDrawable().setBounds(bounds);
  163. }
  164. @Override
  165. public void initData() {
  166. super.initData();
  167. mRecordId = getIntent().getStringExtra("recordId");
  168. worksId = getIntent().getStringExtra("worksId");
  169. imgCover = getIntent().getStringExtra("coverImg");
  170. defaultDelay = getIntent().getIntExtra("defaultDelay", 0);
  171. evaluateDelay = getIntent().getIntExtra("evaluateDelay", 0);
  172. loadCover();
  173. if (TextUtils.isEmpty(mRecordId)) {
  174. ToastUtil.getInstance().showShort("作品生成失败");
  175. finish();
  176. return;
  177. }
  178. accompanyUrl = getIntent().getStringExtra("accompanyUrl");
  179. recordFilePath = getIntent().getStringExtra("recordFilePath");
  180. initLoadingDialog();
  181. initViewModel();
  182. initFragment();
  183. initListener();
  184. initPlayer();
  185. LOG.i("recordFilePath:" + recordFilePath);
  186. LOG.i("accompanyUrl:" + accompanyUrl);
  187. if (!checkRecordFile()) {
  188. if (!TextUtils.isEmpty(worksId)) {
  189. presenter.getDetail(worksId);
  190. } else {
  191. ToastUtil.getInstance().showShort("作品生成失败");
  192. finish();
  193. }
  194. } else {
  195. preLoad();
  196. }
  197. }
  198. private void initViewModel() {
  199. ViewModelProvider.AndroidViewModelFactory instance =
  200. ViewModelProvider.AndroidViewModelFactory
  201. .getInstance(getApplication());
  202. mViewModel = new ViewModelProvider(this, instance)
  203. .get(MusicMergeViewModel.class);
  204. mViewModel.getWorksId().setValue(worksId);
  205. refreshMusicInfo(imgCover);
  206. }
  207. private void preLoad() {
  208. isVideo = MyFileUtils.isVideo(recordFilePath);
  209. LOG.i("isVideo:" + isVideo);
  210. mViewModel.getIsVideoFile().setValue(isVideo);
  211. boolean b = checkAccompanimentMp3File();
  212. if (b) {
  213. preparePlay();
  214. }
  215. }
  216. private void preparePlay() {
  217. if (isVideo) {
  218. initSurfaceView();
  219. } else {
  220. toPlay(getAccompanyPath());
  221. }
  222. }
  223. private void loadCover() {
  224. GlideUtils.INSTANCE.loadImage(this, imgCover, viewBinding.ivCover, R.drawable.icon_default_music_song_cover);
  225. }
  226. @Override
  227. protected MusicFileHandlePresenter createPresenter() {
  228. return new MusicFileHandlePresenter();
  229. }
  230. private void initListener() {
  231. viewBinding.ivBack.setOnClickListener(this);
  232. viewBinding.ivPlay.setOnClickListener(this);
  233. viewBinding.ivUnfoldSentting.setOnClickListener(this);
  234. mSettingFragment.setEventListener(new MusicHandleSettingFragment.OnEventListener() {
  235. @Override
  236. public void onVolumeChange(int type, float value) {
  237. if (type == MusicHandleSettingFragment.RECORD_TYPE) {
  238. player1.setVolume(value);
  239. } else {
  240. player2.setVolume(value);
  241. }
  242. }
  243. @Override
  244. public void onOffsetValueChange(int value) {
  245. aligningAccompany(value);
  246. }
  247. @Override
  248. public void toMix(int offsetValue, float volume1, float volume2, boolean isNeedNotify) {
  249. MAX_STEP = 3;
  250. checkCoverToUpload();
  251. if (isVideo) {
  252. startMixForMp4(getAccompanyPath(), recordFilePath, offsetValue, volume1, volume2, isNeedNotify);
  253. } else {
  254. startMix(getAccompanyPath(), recordFilePath, offsetValue, volume1, volume2, isNeedNotify);
  255. }
  256. }
  257. @Override
  258. public void hideSetting() {
  259. handleSettingVisibility();
  260. }
  261. @Override
  262. public void saveDraft() {
  263. //上传原始视频 以及 相关调节参数
  264. toSaveDraft();
  265. }
  266. });
  267. viewBinding.seekPlay.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  268. @Override
  269. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  270. if (fromUser) {
  271. handleSeekEvent();
  272. }
  273. }
  274. @Override
  275. public void onStartTrackingTouch(SeekBar seekBar) {
  276. }
  277. @Override
  278. public void onStopTrackingTouch(SeekBar seekBar) {
  279. }
  280. });
  281. }
  282. private void checkCoverToUpload() {
  283. MusicInfoBean value = mViewModel.getMusicInfoLiveData().getValue();
  284. if (value != null) {
  285. String preCover = value.getPreCover();
  286. String videoCover = value.getVideoCover();
  287. LOG.i("preCover:" + preCover);
  288. LOG.i("videoCover:" + videoCover);
  289. if (!TextUtils.isEmpty(preCover)) {
  290. presenter.upLoadImage(null, preCover, false);
  291. }
  292. if (!TextUtils.isEmpty(videoCover)) {
  293. presenter.upLoadImage(null, videoCover, true);
  294. }
  295. }
  296. }
  297. private void toSaveDraft() {
  298. if (!TextUtils.isEmpty(recordFilePath)) {
  299. if (!TextUtils.isEmpty(originalFileUrl)) {
  300. MAX_STEP = 1;
  301. toShowLoading(0, getString(R.string.save_draft_tip));
  302. toNotifyDraft(originalFileUrl);
  303. } else {
  304. MAX_STEP = 2;
  305. uploadDraft(recordFilePath);
  306. }
  307. }
  308. }
  309. private void uploadDraft(String filePath) {
  310. currentStep = 0;
  311. File file = new File(filePath);
  312. if (!file.exists()) {
  313. return;
  314. }
  315. toSetLoadingCancelable(false);
  316. toShowLoading(getCurrentProgress(0), getString(R.string.updload_draft_tip));
  317. UploadHelper uploadHelper = new UploadHelper(null, UploadConstants.UPLOAD_TYPE_HOMEWORK);
  318. uploadHelper.setUpLoadCallBack(new UploadHelper.UpLoadCallBack() {
  319. @Override
  320. protected void onSuccess(String url) {
  321. toUpdateLoadingText(getCurrentProgress(100), getString(R.string.updload_draft_tip));
  322. toNotifyDraft(url);
  323. }
  324. @Override
  325. protected void onFailure() {
  326. runOnUiThread(new Runnable() {
  327. @Override
  328. public void run() {
  329. hideLoading();
  330. ToastUtil.getInstance().showShort("草稿上传失败,请重试");
  331. }
  332. });
  333. }
  334. @Override
  335. public void onUploadProgress(double v) {
  336. Log.i("pq", "onUploadProgress" + v);
  337. // UiUtils.convertDouble(v)
  338. toUpdateLoadingText(getCurrentProgress((int) v), getString(R.string.updload_draft_tip));
  339. }
  340. });
  341. uploadHelper.setLoadingTip(getString(R.string.updload_draft_tip));
  342. uploadHelper.uploadFile(file);
  343. }
  344. private void upload(String filePath) {
  345. currentStep = 1;
  346. File file = new File(filePath);
  347. if (!file.exists()) {
  348. return;
  349. }
  350. toUpdateLoadingText(getCurrentProgress(0), getString(R.string.upload_works_tip));
  351. UploadHelper uploadHelper = new UploadHelper(null, UploadConstants.UPLOAD_TYPE_HOMEWORK);
  352. uploadHelper.setUpLoadCallBack(new UploadHelper.UpLoadCallBack() {
  353. @Override
  354. protected void onSuccess(String url) {
  355. toUpdateLoadingText(getCurrentProgress(100), getString(R.string.upload_works_tip));
  356. toNotify(url);
  357. }
  358. @Override
  359. protected void onFailure() {
  360. runOnUiThread(new Runnable() {
  361. @Override
  362. public void run() {
  363. hideLoading();
  364. ToastUtil.getInstance().showShort("作品上传失败,请重试");
  365. }
  366. });
  367. }
  368. @Override
  369. public void onUploadProgress(double v) {
  370. toUpdateLoadingText(getCurrentProgress((int) v), getString(R.string.upload_works_tip));
  371. }
  372. });
  373. uploadHelper.setLoadingTip(getString(R.string.upload_works_tip));
  374. uploadHelper.uploadFile(file);
  375. }
  376. private void toNotify(String url) {
  377. String configJson = mSettingFragment.getConfigJson(defaultDelay, evaluateDelay);
  378. MusicInfoBean value = mViewModel.getMusicInfoLiveData().getValue();
  379. String videoCover = "";
  380. String cover = imgCover;
  381. if (value != null) {
  382. des = value.getDes();
  383. String videoCoverResult = value.getVideoCover();
  384. if (UrlUtils.isValidUrl(videoCoverResult)) {
  385. videoCover = videoCoverResult;
  386. }
  387. String cover1 = value.getCover();
  388. if (!TextUtils.isEmpty(cover1)) {
  389. cover = cover1;
  390. }
  391. }
  392. presenter.save(mRecordId, url, cover, videoCover, des, configJson);
  393. }
  394. private void toNotifyDraft(String url) {
  395. if (mSettingFragment != null) {
  396. String configJson = mSettingFragment.getConfigJson(defaultDelay, evaluateDelay);
  397. presenter.saveDraft(mRecordId, url, accompanyUrl, imgCover, configJson);
  398. }
  399. }
  400. public void startMixForMp4(String accompanimentMp3Path, String recordFilePath, int offsetValue, float recordFileVolume, float accompanyFileVolume, boolean isNeedNotify) {
  401. currentStep = 0;
  402. toSetLoadingCancelable(false);
  403. toShowLoading(getCurrentProgress(0), getString(R.string.video_merge_tip));
  404. MixHelper.getInstance().startMixForMp4(accompanimentMp3Path, recordFilePath, offsetValue, recordFileVolume, accompanyFileVolume, new ResultCallback<String>() {
  405. @Override
  406. public void onSuccess(String s) {
  407. if (!checkActivityExist()) {
  408. return;
  409. }
  410. runOnUiThread(new Runnable() {
  411. @Override
  412. public void run() {
  413. toUpdateLoadingText(getCurrentProgress(100), getString(R.string.video_merge_tip));
  414. if (!TextUtils.isEmpty(s)) {
  415. if (isNeedNotify) {
  416. upload(s);
  417. } else {
  418. FileUtils.notifySystemToScan(s);
  419. ToastUtil.getInstance().showShort("保存成功");
  420. }
  421. } else {
  422. hideLoading();
  423. ToastUtil.getInstance().showShort("mix onFail");
  424. }
  425. }
  426. });
  427. }
  428. @Override
  429. public void onProgress(int progressPercent) {
  430. if (!checkActivityExist()) {
  431. return;
  432. }
  433. runOnUiThread(new Runnable() {
  434. @Override
  435. public void run() {
  436. if (currentStep == 0) {//这个进度有延迟,可能不准确,如果到了下一个步骤了就不再触发更新
  437. LOG.i("progressPercent:" + progressPercent);
  438. toUpdateLoadingText(getCurrentProgress(progressPercent), getString(R.string.video_merge_tip));
  439. }
  440. }
  441. });
  442. }
  443. @Override
  444. public void onFail(int errorCode, String errorStr) {
  445. if (!checkActivityExist()) {
  446. return;
  447. }
  448. runOnUiThread(new Runnable() {
  449. @Override
  450. public void run() {
  451. hideLoading();
  452. ToastUtil.getInstance().showShort("mix onFail:" + errorCode + "--reason:" + errorStr);
  453. }
  454. });
  455. }
  456. });
  457. }
  458. private void startMix(String accompanimentMp3Path, String recordFilePath, int offsetValue, float recordFileVolume, float accompanyFileVolume, boolean isNeedNotify) {
  459. currentStep = 0;
  460. toSetLoadingCancelable(false);
  461. toShowLoading(getCurrentProgress(0), getString(R.string.audio_merge_tip));
  462. MixHelper.getInstance().startMix(accompanimentMp3Path, recordFilePath, offsetValue, recordFileVolume, accompanyFileVolume, new ResultCallback<String>() {
  463. @Override
  464. public void onSuccess(String s) {
  465. if (!checkActivityExist()) {
  466. return;
  467. }
  468. runOnUiThread(new Runnable() {
  469. @Override
  470. public void run() {
  471. toUpdateLoadingText(getCurrentProgress(100), getString(R.string.audio_merge_tip));
  472. if (!TextUtils.isEmpty(s)) {
  473. if (isNeedNotify) {
  474. upload(s);
  475. } else {
  476. FileUtils.notifySystemToScan(s);
  477. ToastUtil.getInstance().showShort("保存成功");
  478. }
  479. } else {
  480. hideLoading();
  481. ToastUtil.getInstance().showShort("mix onFail");
  482. }
  483. }
  484. });
  485. }
  486. @Override
  487. public void onProgress(int progressPercent) {
  488. if (!checkActivityExist()) {
  489. return;
  490. }
  491. runOnUiThread(new Runnable() {
  492. @Override
  493. public void run() {
  494. if (currentStep == 0) {//这个进度有延迟,可能不准确,如果到了下一个步骤了就不再触发更新
  495. LOG.i("progressPercent:" + progressPercent);
  496. toUpdateLoadingText(getCurrentProgress(progressPercent), getString(R.string.audio_merge_tip));
  497. }
  498. }
  499. });
  500. }
  501. @Override
  502. public void onFail(int errorCode, String errorStr) {
  503. if (!checkActivityExist()) {
  504. return;
  505. }
  506. runOnUiThread(new Runnable() {
  507. @Override
  508. public void run() {
  509. hideLoading();
  510. ToastUtil.getInstance().showShort("mix onFail:" + errorCode + "--reason:" + errorStr);
  511. }
  512. });
  513. }
  514. });
  515. }
  516. private void aligningAccompany(int value) {
  517. int cu = player1.getCu();
  518. LOG.i("pq", "cu:" + cu);
  519. LOG.i("pq", "currentOffsetValue:" + value);
  520. int seekResult2 = countAccompanyPosition(cu);
  521. LOG.i("pq", "seekResult2:" + seekResult2);
  522. player2.seekTo(seekResult2);
  523. }
  524. private void handleSeekEvent() {
  525. int progress = viewBinding.seekPlay.getProgress();
  526. float percent = progress * 1.0f / viewBinding.seekPlay.getMax();
  527. int seekResult = (int) (player1.getTotal() * percent);
  528. LOG.i("pq", "seekResult:" + seekResult);
  529. int seekResult2 = countAccompanyPosition(seekResult);
  530. LOG.i("pq", "seekResult2:" + seekResult2);
  531. player1.seekTo(seekResult);
  532. player2.seekTo(seekResult2);
  533. }
  534. private int countAccompanyPosition(int recordPosition) {
  535. int currentOffsetValue = mSettingFragment.getCurrentOffsetValue();
  536. LOG.i("pq", "currentOffsetValue:" + currentOffsetValue);
  537. int exceptPosition = recordPosition + currentOffsetValue;
  538. return exceptPosition;
  539. }
  540. private void initPlayer() {
  541. player1 = new CustomPlayer("luzhi");
  542. player2 = new CustomPlayer("伴奏");
  543. player1.setOnEventListener(new CustomPlayer.OnEventListener() {
  544. @Override
  545. public void onProgress(int progress) {
  546. if (!checkActivityExist()) {
  547. return;
  548. }
  549. toShowDelay();
  550. String s = TimeUtils.msToTime(progress);
  551. viewBinding.tvCurrentProgress.setText(s);
  552. int maxProgress = viewBinding.seekPlay.getMax();
  553. int percent = (int) ((progress * 1.0f / player1.getTotal()) * maxProgress);
  554. viewBinding.seekPlay.setProgress(percent);
  555. }
  556. @Override
  557. public void onPrepared(int duration) {
  558. if (!checkActivityExist()) {
  559. return;
  560. }
  561. firstPlayDuration = duration;
  562. String s = TimeUtils.msToTime(duration);
  563. viewBinding.tvTotalProgress.setText(s);
  564. toRealPlay();
  565. }
  566. @Override
  567. public void onCompleted() {
  568. if (!checkActivityExist()) {
  569. return;
  570. }
  571. viewBinding.seekPlay.setProgress(0);
  572. viewBinding.seekPlay2.setProgress(0);
  573. player2.pause();
  574. handleSeekEvent();
  575. updatePlayStatus();
  576. }
  577. @Override
  578. public void onError() {
  579. }
  580. @Override
  581. public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
  582. MusicHandleActivity.this.videoWidth = width;
  583. MusicHandleActivity.this.videoHeight = height;
  584. resetVideoSize(width, height);
  585. }
  586. });
  587. player2.setOnEventListener(new CustomPlayer.OnEventListener() {
  588. @Override
  589. public void onProgress(int progress) {
  590. String s = TimeUtils.msToTime(progress);
  591. viewBinding.tvCurrentProgress2.setText(s);
  592. int maxProgress = viewBinding.seekPlay2.getMax();
  593. int percent = (int) ((progress * 1.0f / player2.getTotal()) * maxProgress);
  594. viewBinding.seekPlay2.setProgress(percent);
  595. }
  596. @Override
  597. public void onPrepared(int duration) {
  598. secondPlayDuration = duration;
  599. String s = TimeUtils.msToTime(duration);
  600. viewBinding.tvTotalProgress2.setText(s);
  601. toRealPlay();
  602. }
  603. @Override
  604. public void onCompleted() {
  605. }
  606. @Override
  607. public void onError() {
  608. }
  609. @Override
  610. public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
  611. }
  612. });
  613. }
  614. private void toShowDelay() {
  615. int cu = player1.getCu();
  616. int cu1 = player2.getCu();
  617. int dif = cu1 - cu;
  618. String text = "演奏进度:" + cu + "\n伴奏进度:" + cu1 + "\n差值:" + dif;
  619. viewBinding.tvDelayText.setText(text);
  620. if (!player1.isPlaying()) {
  621. return;
  622. }
  623. LOG.i("pq", "dif:" + dif);
  624. int expectDelay = dif - mSettingFragment.getCurrentOffsetValue();
  625. LOG.i("pq", "expectDelay:" + expectDelay);
  626. int difAbs = Math.abs(expectDelay);
  627. if (difAbs > MAX_ADJUSTMENT) {
  628. float s = difAbs / 1000f;
  629. if (expectDelay > 0) {
  630. player1.setSpeed(1.0f + s);
  631. player2.setSpeed(1.0f - s);
  632. } else {
  633. player1.setSpeed(1.0f - s);
  634. player2.setSpeed(1.0f + s);
  635. }
  636. } else {
  637. player1.setSpeed(1.0f);
  638. player2.setSpeed(1.0f);
  639. }
  640. }
  641. private void toRealPlay() {
  642. if (firstPlayDuration != 0 && secondPlayDuration != 0) {
  643. player1.resetToPrepare();
  644. player2.resetToPrepare();
  645. player1.start();
  646. player2.start();
  647. setVolume();
  648. updatePlayStatus();
  649. }
  650. }
  651. private void setVolume() {
  652. if (mSettingFragment != null) {
  653. float originalVolume = mSettingFragment.getOriginalVolume();
  654. float accompanyVolume = mSettingFragment.getAccompanyVolume();
  655. player1.setVolume(originalVolume);
  656. player2.setVolume(accompanyVolume);
  657. }
  658. }
  659. private void resetVideoSize(int width, int height) {
  660. int surfaceWidth = viewBinding.flSurface.getWidth();
  661. int surfaceHeight = viewBinding.flSurface.getHeight();
  662. LOG.i("resetVideoSize", "surfaceWidth:" + surfaceWidth);
  663. LOG.i("resetVideoSize", "surfaceHeight:" + surfaceHeight);
  664. float containerAspectRatio = surfaceWidth * 1.0f / surfaceHeight;
  665. float videoAspectRatio = width * 1.0f / height;
  666. LOG.i("resetVideoSize", "containerAspectRatio:" + containerAspectRatio);
  667. LOG.i("resetVideoSize", "videoAspectRatio:" + videoAspectRatio);
  668. boolean b = NumberUtils.compareResult(containerAspectRatio, videoAspectRatio);
  669. LOG.i("resetVideoSize", "compareResult:" + b);
  670. if (b) {
  671. return;
  672. }
  673. LOG.i("resetVideoSize", "videoWidth:" + width);
  674. LOG.i("resetVideoSize", "videoHeight:" + height);
  675. int w;
  676. int h;
  677. if (containerAspectRatio > videoAspectRatio) {
  678. // 容器更宽,视频高度适应容器高度
  679. h = surfaceHeight;
  680. w = (int) (surfaceHeight * videoAspectRatio);
  681. } else {
  682. // 容器更高,视频宽度适应容器宽度
  683. w = surfaceWidth;
  684. h = (int) (surfaceWidth / videoAspectRatio);
  685. }
  686. LOG.i("resetVideoSize", "w:" + w);
  687. LOG.i("resetVideoSize", "h:" + h);
  688. if (w > surfaceWidth) {
  689. w = surfaceWidth;
  690. }
  691. if (h > surfaceHeight) {
  692. h = surfaceHeight;
  693. }
  694. LOG.i("pq", "w:" + w);
  695. LOG.i("pq", "h:" + h);
  696. FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mSurfaceView.getLayoutParams();
  697. layoutParams.width = w;
  698. layoutParams.height = h;
  699. mSurfaceView.setLayoutParams(layoutParams);
  700. // mSurfaceView.getHolder().setFixedSize(w, h);
  701. }
  702. private boolean checkRecordFile() {
  703. if (TextUtils.isEmpty(recordFilePath)) {
  704. return false;
  705. }
  706. File file = new File(recordFilePath);
  707. if (!file.exists()) {
  708. return false;
  709. }
  710. return true;
  711. }
  712. private void toPlay(String accompanyPath) {
  713. player1.play(recordFilePath);
  714. player2.play(accompanyPath);
  715. }
  716. private boolean checkRecordFile(String recordUrl) {
  717. String fileEndSuffix = MyFileUtils.getWAVOrMp4FileSuffix(recordUrl);
  718. String recordFileDownloadPath = getRecordFileDownloadPath(recordUrl, fileEndSuffix);
  719. recordFilePath = recordFileDownloadPath;
  720. Log.i("pq", "checkRecordFile:" + recordFilePath);
  721. File file = new File(recordFileDownloadPath);
  722. if (file.exists()) {
  723. return true;
  724. }
  725. boolean validDownloadUrl = UrlUtils.isValidDownloadUrl(recordUrl);
  726. if (!validDownloadUrl) {
  727. ToastUtil.getInstance().showShort("未找到演奏文件,请退出重试");
  728. return false;
  729. }
  730. Log.i("pq", "下载草稿");
  731. setLoadingCancelable(false);
  732. showLoading("草稿下载中");
  733. MixHelper.getInstance().download(recordUrl, fileEndSuffix, new ResultCallback<String>() {
  734. @Override
  735. public void onSuccess(String s) {
  736. if (!checkActivityExist()) {
  737. return;
  738. }
  739. runOnUiThread(new Runnable() {
  740. @Override
  741. public void run() {
  742. hideLoading();
  743. preLoad();
  744. }
  745. });
  746. }
  747. @Override
  748. public void onProgress(int progressPercent) {
  749. if (!checkActivityExist()) {
  750. return;
  751. }
  752. runOnUiThread(new Runnable() {
  753. @Override
  754. public void run() {
  755. updateLoadingText("草稿下载中" + progressPercent + "%");
  756. }
  757. });
  758. }
  759. @Override
  760. public void onFail(int errorCode, String errorStr) {
  761. if (!checkActivityExist()) {
  762. return;
  763. }
  764. runOnUiThread(new Runnable() {
  765. @Override
  766. public void run() {
  767. ToastUtil.getInstance().showShort("草稿下载失败,请重试");
  768. hideLoading();
  769. }
  770. });
  771. }
  772. });
  773. return false;
  774. }
  775. private boolean checkAccompanimentMp3File() {
  776. if (TextUtils.isEmpty(accompanyUrl)) {
  777. ToastUtil.getInstance().showShort("未找到伴奏文件,请退出重试");
  778. return false;
  779. }
  780. String accompanyPath = getAccompanyPath();
  781. File file = new File(accompanyPath);
  782. if (file.exists()) {
  783. return true;
  784. }
  785. boolean validDownloadUrl = UrlUtils.isValidDownloadUrl(accompanyUrl);
  786. if (!validDownloadUrl) {
  787. ToastUtil.getInstance().showShort("未找到伴奏文件,请退出重试");
  788. return false;
  789. }
  790. setLoadingCancelable(false);
  791. showLoading("伴奏下载中");
  792. MixHelper.getInstance().download(accompanyUrl, MyFileUtils.MP3_FILE_SUFFIX, new ResultCallback<String>() {
  793. @Override
  794. public void onSuccess(String s) {
  795. if (!checkActivityExist()) {
  796. return;
  797. }
  798. runOnUiThread(new Runnable() {
  799. @Override
  800. public void run() {
  801. preparePlay();
  802. hideLoading();
  803. }
  804. });
  805. }
  806. @Override
  807. public void onProgress(int progressPercent) {
  808. if (!checkActivityExist()) {
  809. return;
  810. }
  811. runOnUiThread(new Runnable() {
  812. @Override
  813. public void run() {
  814. updateLoadingText("伴奏下载中" + progressPercent + "%");
  815. }
  816. });
  817. }
  818. @Override
  819. public void onFail(int errorCode, String errorStr) {
  820. if (!checkActivityExist()) {
  821. return;
  822. }
  823. runOnUiThread(new Runnable() {
  824. @Override
  825. public void run() {
  826. ToastUtil.getInstance().showShort("伴奏下载失败,请重试");
  827. hideLoading();
  828. }
  829. });
  830. }
  831. });
  832. return false;
  833. }
  834. @Override
  835. public void hideLoading() {
  836. setLoadingCancelable(true);
  837. toSetLoadingCancelable(true);
  838. if (mLoadingTipDialog != null) {
  839. mLoadingTipDialog.hide();
  840. }
  841. super.hideLoading();
  842. }
  843. private String getAccompanyPath() {
  844. String accompanyPath = MixHelper.getInstance().getDownloadSavePath(accompanyUrl, MyFileUtils.MP3_FILE_SUFFIX);
  845. return accompanyPath;
  846. }
  847. private String getRecordFileDownloadPath(String recordFileUrl, String fileEndSuffix) {
  848. String recordFilePath;
  849. if (MyFileUtils.isVideoFromUrl(recordFileUrl)) {
  850. recordFilePath = MixHelper.getInstance().getDownloadSavePathForMp4(recordFileUrl);
  851. } else {
  852. recordFilePath = MixHelper.getInstance().getDownloadSavePath(recordFileUrl, fileEndSuffix);
  853. }
  854. return recordFilePath;
  855. }
  856. @Override
  857. protected AcMusicHandleLayoutBinding getLayoutView() {
  858. return AcMusicHandleLayoutBinding.inflate(getLayoutInflater());
  859. }
  860. @Override
  861. public void onClick(View v) {
  862. int id = v.getId();
  863. if (id == R.id.iv_play) {
  864. if (player1.isPlaying()) {
  865. pausePlay();
  866. } else {
  867. player1.resume();
  868. player2.resume();
  869. updatePlayStatus();
  870. }
  871. return;
  872. }
  873. if (id == R.id.iv_unfold_sentting) {
  874. handleSettingVisibility();
  875. return;
  876. }
  877. if (id == R.id.iv_back) {
  878. checkTipToFinish();
  879. return;
  880. }
  881. }
  882. @Override
  883. protected void onStop() {
  884. super.onStop();
  885. pausePlay();
  886. }
  887. private void pausePlay() {
  888. if (player1.isPlaying()) {
  889. player1.pause();
  890. player2.pause();
  891. //暂停的时候需要校准一次,防止定时器触发
  892. aligningAccompany(mSettingFragment.getCurrentOffsetValue());
  893. }
  894. updatePlayStatus();
  895. }
  896. private void checkTipToFinish() {
  897. boolean hasUpdate = mViewModel.isHasUpdate();
  898. if (hasUpdate) {
  899. showSaveTipDialog();
  900. } else {
  901. finish();
  902. }
  903. }
  904. private void showSaveTipDialog() {
  905. CommonConfirmDialog2 commonConfirmDialog = new CommonConfirmDialog2(this);
  906. commonConfirmDialog.setWidth(SizeUtils.dp2px(387));
  907. commonConfirmDialog.show();
  908. commonConfirmDialog.setTitle("提示");
  909. commonConfirmDialog.setContent("是否将本次录制的作品保存为草稿?");
  910. commonConfirmDialog.setOnConfirmClickListener(new View.OnClickListener() {
  911. @Override
  912. public void onClick(View v) {
  913. commonConfirmDialog.dismiss();
  914. isNeedFinishPage = true;
  915. toSaveDraft();
  916. }
  917. });
  918. commonConfirmDialog.setOnCancelClickListener(new View.OnClickListener() {
  919. @Override
  920. public void onClick(View v) {
  921. commonConfirmDialog.dismiss();
  922. finish();
  923. }
  924. });
  925. }
  926. /**
  927. * 监听返回键
  928. *
  929. * @param keyCode
  930. * @param event
  931. * @return
  932. */
  933. @Override
  934. public boolean onKeyDown(int keyCode, KeyEvent event) {
  935. if (keyCode == KeyEvent.KEYCODE_BACK) {
  936. checkTipToFinish();
  937. return true;
  938. }
  939. return super.onKeyDown(keyCode, event);
  940. }
  941. private void handleSettingVisibility() {
  942. int visibility = viewBinding.flSetting.getVisibility();
  943. if (visibility == View.VISIBLE) {
  944. viewBinding.groupSetting.setVisibility(View.GONE);
  945. viewBinding.ivUnfoldSentting.setVisibility(View.VISIBLE);
  946. } else {
  947. viewBinding.groupSetting.setVisibility(View.VISIBLE);
  948. viewBinding.ivUnfoldSentting.setVisibility(View.GONE);
  949. }
  950. mHandler.postDelayed(new Runnable() {
  951. @Override
  952. public void run() {
  953. resetVideoSize(videoWidth, videoHeight);
  954. }
  955. }, 50);
  956. }
  957. private void updatePlayStatus() {
  958. if (player1.isPlaying()) {
  959. viewBinding.ivPlayPointer.setRotation(0);
  960. handleAnim(true);
  961. viewBinding.ivPlay.setImageResource(isVideo ? R.drawable.icon_music_merge_pause_white : R.drawable.icon_music_merge_pause);
  962. } else {
  963. handleAnim(false);
  964. viewBinding.ivPlayPointer.setRotation(92);
  965. viewBinding.ivPlay.setImageResource(isVideo ? R.drawable.icon_music_merge_play_white : R.drawable.icon_music_merge_play);
  966. }
  967. }
  968. private void handleAnim(boolean isPlay) {
  969. if (isVideo) {
  970. return;
  971. }
  972. if (player1 != null && player1.getPlayer() != null && isPlay) {
  973. viewBinding.musicFrequencyView.setMediaPlayer(player1.getPlayer());
  974. }
  975. rotation(isPlay);
  976. rotationAlbum(isPlay);
  977. }
  978. private void rotation(boolean isPlay) {
  979. float from = isPlay ? 92 : 0;
  980. float to = isPlay ? 0 : 92;
  981. viewBinding.ivPlayPointer.clearAnimation();
  982. float rotation = viewBinding.ivPlayPointer.getRotation();
  983. LOG.i("rotation:" + rotation);
  984. if (mRotateAnimation == null) {
  985. mRotateAnimation = ObjectAnimator.ofFloat(viewBinding.ivPlayPointer, "rotation", from, to);
  986. mRotateAnimation.setInterpolator(new LinearInterpolator());//不停顿
  987. mRotateAnimation.setDuration(300);
  988. mRotateAnimation.setRepeatCount(0);
  989. }
  990. mRotateAnimation.setFloatValues(from, to);
  991. mRotateAnimation.start();
  992. }
  993. private void rotationAlbum(boolean isPlay) {
  994. float rotation = viewBinding.flAblum.getRotation();
  995. if (mAlbumRotationAnimator == null) {
  996. mAlbumRotationAnimator = ObjectAnimator.ofFloat(viewBinding.flAblum, "rotation", 0f, 360f);
  997. mAlbumRotationAnimator.setDuration(5000); // 设置动画持续时间为1秒
  998. mAlbumRotationAnimator.setRepeatCount(ObjectAnimator.INFINITE); // 设置无限循环
  999. mAlbumRotationAnimator.setInterpolator(new LinearInterpolator()); // 设置动画插值器,这里使用线性插值器
  1000. }
  1001. if (isPlay) {
  1002. if (mAlbumRotationAnimator.isPaused()) {
  1003. mAlbumRotationAnimator.resume();
  1004. } else {
  1005. mAlbumRotationAnimator.start(); // 启动动画
  1006. }
  1007. } else {
  1008. mAlbumRotationAnimator.pause();//
  1009. }
  1010. }
  1011. @Override
  1012. public void saveWorksSuccess(Object data, String des,String imgCover) {
  1013. if (!checkActivityExist()) {
  1014. return;
  1015. }
  1016. currentStep = 2;
  1017. // showToastViewAndFinish("发布成功", true);
  1018. String worksId = null;
  1019. if (data instanceof String) {
  1020. worksId = (String) data;
  1021. }
  1022. if (!TextUtils.isEmpty(worksId)) {
  1023. ShareIntentBean shareIntentBean = buildShareData(worksId, des,imgCover);
  1024. toShowShareDialog(shareIntentBean, true);
  1025. } else {
  1026. showToastViewAndFinish(getString(R.string.publish_success), true);
  1027. }
  1028. }
  1029. private ShareIntentBean buildShareData(String worksId,String shareDes,String imgCover){
  1030. ShareIntentBean bean = new ShareIntentBean();
  1031. String url = String.format(WebConstants.MY_WORKS_SHARE, worksId);
  1032. bean.setLinkUrl(url);
  1033. bean.setTitle(getString(R.string.share_works_title));
  1034. bean.setDes(shareDes);
  1035. bean.setThumb(imgCover);
  1036. return bean;
  1037. }
  1038. private void toShowShareDialog(ShareIntentBean intentBean, boolean isReCallBack) {
  1039. toUpdateLoadingText(getCurrentProgress(100), getString(R.string.publish_success));
  1040. mHandler.postDelayed(new Runnable() {
  1041. @Override
  1042. public void run() {
  1043. hideLoading();
  1044. showShareDialog(intentBean, isReCallBack);
  1045. }
  1046. }, 1500);
  1047. }
  1048. private void showShareDialog(ShareIntentBean intentBean, boolean isReCallBack) {
  1049. ShareDialog shareDialog = new ShareDialog(this);
  1050. shareDialog.setOnEventListener(new ShareDialog.OnEventListener() {
  1051. @Override
  1052. public void toShare(ShareType shareType) {
  1053. goShare(intentBean, shareType);
  1054. }
  1055. });
  1056. shareDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
  1057. @Override
  1058. public void onDismiss(DialogInterface dialog) {
  1059. toFinish(isReCallBack);
  1060. }
  1061. });
  1062. shareDialog.show();
  1063. }
  1064. private void goShare(ShareIntentBean intentBean, ShareType shareType) {
  1065. if (shareType == ShareType.COPY_LINK) {
  1066. ClipboardUtils.copyText(intentBean.getLinkUrl());
  1067. ToastUtil.getInstance().showShort("复制成功");
  1068. return;
  1069. } else if (shareType == ShareType.WEIXIN) {
  1070. intentBean.setShare_media(SHARE_MEDIA.WEIXIN);
  1071. } else if (shareType == ShareType.WEIXIN_CIRCLE) {
  1072. intentBean.setShare_media(SHARE_MEDIA.WEIXIN_CIRCLE);
  1073. }
  1074. if (intentBean.getShare_media() != null) {
  1075. CommonShareHelper.toShareUrl(MusicHandleActivity.this, intentBean, this);
  1076. }
  1077. }
  1078. private void toFinish(boolean isReCallBack) {
  1079. Intent intent = new Intent();
  1080. if (isReCallBack) {
  1081. intent.putExtra("saveWorksStatus", 1);
  1082. }
  1083. setResult(RESULT_OK, intent);
  1084. finish();
  1085. }
  1086. @Override
  1087. public void getDetailSuccess(MusicDataBean data) {
  1088. if (!checkActivityExist()) {
  1089. return;
  1090. }
  1091. if (data != null) {
  1092. this.originalFileUrl = data.getVideoUrl();
  1093. String jsonConfig = data.getJsonConfig();
  1094. accompanyUrl = data.getAccompanyUrl();
  1095. if (mSettingFragment != null) {
  1096. if (!TextUtils.isEmpty(jsonConfig)) {
  1097. toApplyConfig(jsonConfig);
  1098. }
  1099. mSettingFragment.setAccompanyUrl(accompanyUrl);
  1100. }
  1101. //这里为了兼容IOS录制的wav音频文件格式不正确 导致合成失败的问题Failed to read frame size: Could not seek to 1026.
  1102. //取服务端存储的文件
  1103. boolean isVideo = MyFileUtils.isVideoFromUrl(data.getVideoUrl());
  1104. String fileUrl = isVideo ? data.getVideoUrl() : data.getRecordFilePath();
  1105. boolean b = checkRecordFile(fileUrl);
  1106. if (b) {
  1107. preLoad();
  1108. }
  1109. }
  1110. }
  1111. private void toApplyConfig(String jsonConfig) {
  1112. try {
  1113. MusicMergeConfigBean musicMergeConfigBean = GsonUtils.fromJson(jsonConfig, MusicMergeConfigBean.class);
  1114. defaultDelay = musicMergeConfigBean.getDefaultDelay();
  1115. evaluateDelay = musicMergeConfigBean.getEvaluateDelay();
  1116. mSettingFragment.applyConfig(musicMergeConfigBean);
  1117. } catch (Exception e) {
  1118. e.printStackTrace();
  1119. }
  1120. }
  1121. @Override
  1122. public void saveWorksDraftSuccess() {
  1123. if (!checkActivityExist()) {
  1124. return;
  1125. }
  1126. currentStep = 1;
  1127. mViewModel.getUpdateEvent().setValue(false);
  1128. if (isNeedFinishPage) {
  1129. showToastViewAndFinish("保存成功", false);
  1130. } else {
  1131. toUpdateLoadingText(getCurrentProgress(100), "保存成功");
  1132. mHandler.postDelayed(new Runnable() {
  1133. @Override
  1134. public void run() {
  1135. hideLoading();
  1136. if (isNeedTip()) {
  1137. showOtherTipDialog();
  1138. }
  1139. }
  1140. }, 500);
  1141. }
  1142. }
  1143. private boolean isNeedTip() {
  1144. //云教练进来的需要提示 作品草稿过来的不需要
  1145. return TextUtils.isEmpty(worksId);
  1146. }
  1147. private void showOtherTipDialog() {
  1148. CommonConfirmDialog2 commonConfirmDialog = new CommonConfirmDialog2(this);
  1149. commonConfirmDialog.setWidth(SizeUtils.dp2px(387));
  1150. commonConfirmDialog.show();
  1151. commonConfirmDialog.setTitle("提示");
  1152. commonConfirmDialog.setContent("已成功保存到草稿,草稿7天未发布\n将自动删除。");
  1153. commonConfirmDialog.setCancelText("确认");
  1154. commonConfirmDialog.setConfirmText("查看草稿");
  1155. commonConfirmDialog.setOnCancelClickListener(new View.OnClickListener() {
  1156. @Override
  1157. public void onClick(View v) {
  1158. commonConfirmDialog.dismiss();
  1159. }
  1160. });
  1161. commonConfirmDialog.setOnConfirmClickListener(new View.OnClickListener() {
  1162. @Override
  1163. public void onClick(View v) {
  1164. commonConfirmDialog.dismiss();
  1165. //从首页个人中心跳转我的作品
  1166. JumpUtils.jumpMain(4);
  1167. goMyWorks();
  1168. }
  1169. });
  1170. }
  1171. private void goMyWorks() {
  1172. ARouter.getInstance().build(RouterPath.Homework.MY_WORK)
  1173. .withInt(Constants.MAIN_PAGE_SELECT_POTION_KEY, 1)
  1174. .navigation();
  1175. finish();
  1176. }
  1177. private void showToastViewAndFinish(String tip, boolean isReCallBack) {
  1178. //测试lyr提出提示要在当前面提示,所以给出延迟finish
  1179. // viewBinding.tvToastView.setText(tip);
  1180. // viewBinding.tvToastView.setVisibility(View.VISIBLE);
  1181. toUpdateLoadingText(getCurrentProgress(100), tip);
  1182. mHandler.postDelayed(new Runnable() {
  1183. @Override
  1184. public void run() {
  1185. hideLoading();
  1186. toFinish(isReCallBack);
  1187. }
  1188. }, 1500);
  1189. }
  1190. @Override
  1191. public void upLoadImageSuccess(String url, boolean isVideoCover) {
  1192. if (!checkActivityExist()) {
  1193. return;
  1194. }
  1195. if (mViewModel != null) {
  1196. if (isVideoCover) {
  1197. mViewModel.refreshMusicVideoCover(url);
  1198. } else {
  1199. mViewModel.refreshMusicWorksCover(url);
  1200. }
  1201. }
  1202. }
  1203. private void refreshMusicInfoPreCover(String imgCover) {
  1204. mViewModel.refreshMusicPreCover(imgCover);
  1205. }
  1206. private void refreshMusicInfoVideoCover(String imgCover) {
  1207. mViewModel.refreshMusicVideoCover(imgCover);
  1208. }
  1209. private void refreshMusicInfo(String imgCover) {
  1210. MusicInfoBean bean = new MusicInfoBean();
  1211. bean.setCover(imgCover);
  1212. bean.setMusicTitle(mTitle);
  1213. mViewModel.getMusicInfoLiveData().setValue(bean);
  1214. }
  1215. private int getCurrentProgress(int progress) {
  1216. float singleStepMaxProgress = MAX_PROGRESS * 1.0f / MAX_STEP;
  1217. float progressPercent = progress * 1.0f / 100;
  1218. int result = (int) (singleStepMaxProgress * currentStep + singleStepMaxProgress * progressPercent);
  1219. LOG.i("progress:" + progress + "--currentStep:" + currentStep + "--currentStepProgress" + singleStepMaxProgress * currentStep + "--progressPercent:" + progressPercent + "--result:" + result);
  1220. return result;
  1221. }
  1222. @Override
  1223. public void upLoadImageFailure() {
  1224. ToastUtil.getInstance().showShort("封面上传失败");
  1225. }
  1226. @Override
  1227. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  1228. super.onActivityResult(requestCode, resultCode, data);
  1229. UMShareAPI.get(MusicHandleActivity.this).onActivityResult(requestCode, resultCode, data);
  1230. if (resultCode == RESULT_OK) {
  1231. if (requestCode == REQUEST_CODE_LOCAL || requestCode == REQUEST_CODE_LOCAL_VIDEO_COVER) {
  1232. if (data != null) {
  1233. // 图片、视频、音频选择结果回调
  1234. List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
  1235. String v_path = null;
  1236. if (selectList != null && selectList.size() > 0) {
  1237. v_path = selectList.get(0).getCompressPath();
  1238. }
  1239. if (!TextUtils.isEmpty(v_path)) {
  1240. boolean isImg = MyFileUtils.isImg(v_path);
  1241. if (isImg) {
  1242. if (requestCode == REQUEST_CODE_LOCAL_VIDEO_COVER) {
  1243. refreshMusicInfoVideoCover(v_path);
  1244. } else {
  1245. refreshMusicInfoPreCover(v_path);
  1246. }
  1247. } else {
  1248. ToastUtil.getInstance().showShort("请选择图片类型文件");
  1249. }
  1250. }
  1251. }
  1252. }
  1253. if (requestCode == REQUEST_CODE_VIDEO_COVER) {
  1254. if (data != null) {
  1255. String imgPath = data.getStringExtra(Constants.COMMON_EXTRA_KEY);
  1256. refreshMusicInfoVideoCover(imgPath);
  1257. return;
  1258. }
  1259. }
  1260. }
  1261. }
  1262. private void toShowLoading(int progress, String text) {
  1263. if (mLoadingTipDialog != null) {
  1264. mLoadingTipDialog.showLoading(progress, text);
  1265. }
  1266. }
  1267. private void toSetLoadingCancelable(boolean flag) {
  1268. if (mLoadingTipDialog != null) {
  1269. mLoadingTipDialog.setLoadingCancelable(flag);
  1270. }
  1271. }
  1272. private void toUpdateLoadingText(int progress, String text) {
  1273. if (mLoadingTipDialog != null) {
  1274. mLoadingTipDialog.updateLoadingText(progress, text);
  1275. }
  1276. }
  1277. private void initLoadingDialog() {
  1278. if (mLoadingTipDialog == null) {
  1279. mLoadingTipDialog = new MergeLoadingTipDialog(this);
  1280. }
  1281. }
  1282. @Override
  1283. public void onDestroy() {
  1284. releaseAnim();
  1285. super.onDestroy();
  1286. UMShareAPI.get(MusicHandleActivity.this).release();
  1287. if (mHandler != null) {
  1288. mHandler.removeCallbacksAndMessages(null);
  1289. }
  1290. if (mSurfaceView != null && mSurfaceView.getSurfaceTexture() != null) {
  1291. mSurfaceView.getSurfaceTexture().release();
  1292. }
  1293. if (player1 != null) {
  1294. player1.release();
  1295. }
  1296. if (player2 != null) {
  1297. player2.release();
  1298. }
  1299. }
  1300. private void releaseAnim() {
  1301. if (viewBinding != null && viewBinding.musicFrequencyView != null) {
  1302. viewBinding.musicFrequencyView.release();
  1303. }
  1304. if (mRotateAnimation != null) {
  1305. mRotateAnimation.cancel();
  1306. mRotateAnimation = null;
  1307. }
  1308. if (mAlbumRotationAnimator != null) {
  1309. mAlbumRotationAnimator.cancel();
  1310. mAlbumRotationAnimator = null;
  1311. }
  1312. }
  1313. @Override
  1314. public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
  1315. if (mSurfaceTexture == null) {
  1316. mSurfaceTexture = surface;
  1317. } else {
  1318. mSurfaceView.setSurfaceTexture(mSurfaceTexture);
  1319. }
  1320. if (player1 != null) {
  1321. toPlay(getAccompanyPath());
  1322. player1.setSurface(new Surface(mSurfaceTexture));
  1323. }
  1324. }
  1325. @Override
  1326. public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {
  1327. }
  1328. @Override
  1329. public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
  1330. return false;
  1331. }
  1332. @Override
  1333. public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
  1334. }
  1335. @Override
  1336. public void finish() {
  1337. //为了适配华为mata40曲面屏,此页面是横屏,前一页面是竖屏,返回回去的时候会有UI闪动,所以这样处理
  1338. //观察发现横屏模式时候没有达到曲面最大效果(竖屏可以达到),猜测横屏模式就不是曲面模式了,这个时候回到前面(竖屏)页面,屏幕的最外层的容器可能会变化导致UI闪动
  1339. checkScreenOrientation();
  1340. super.finish();
  1341. }
  1342. @Override
  1343. public void onBackPressed() {
  1344. //为了适配华为mata40曲面屏,此页面是横屏,前一页面是竖屏,返回回去的时候会有UI闪动,所以这样处理
  1345. //观察发现横屏模式时候没有达到曲面最大效果(竖屏可以达到),猜测横屏模式就不是曲面模式了,这个时候回到前面(竖屏)页面,屏幕的最外层的容器可能会变化导致UI闪动
  1346. checkScreenOrientation();
  1347. super.onBackPressed();
  1348. }
  1349. private void checkScreenOrientation() {
  1350. LOG.i("isNeedResetScreenOrientation:" + isNeedResetScreenOrientation);
  1351. if (isNeedResetScreenOrientation) {
  1352. if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
  1353. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  1354. }
  1355. }
  1356. }
  1357. @Override
  1358. public void onStart(SHARE_MEDIA shareMedia) {
  1359. }
  1360. @Override
  1361. public void onResult(SHARE_MEDIA shareMedia) {
  1362. ToastUtil.getInstance().showShort("分享成功啦");
  1363. }
  1364. @Override
  1365. public void onError(SHARE_MEDIA shareMedia, Throwable throwable) {
  1366. ToastUtil.getInstance().showShort("分享失败啦");
  1367. }
  1368. @Override
  1369. public void onCancel(SHARE_MEDIA shareMedia) {
  1370. ToastUtil.getInstance().showShort("分享取消了");
  1371. }
  1372. }