MusicHandleActivity.java 51 KB

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