MusicHandleActivity.java 56 KB

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