PlayerController.java
31.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
/*
* Copyright 2018-2019 KunMinX
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.people.basemusic;
import android.animation.ValueAnimator;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.constraintlayout.widget.Group;
import com.airbnb.lottie.LottieAnimationView;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.hjq.shape.layout.ShapeConstraintLayout;
import com.people.basemusic.contract.ICacheProxy;
import com.people.basemusic.contract.IServiceNotifier;
import com.people.basemusic.domain.PlayerEvent;
import com.people.basemusic.domain.PlayerInfoDispatcher;
import com.people.basemusic.utils.AdaptScreenUtils;
import com.people.basemusic.utils.TimeUtils;
import com.wd.base.log.Logger;
import com.wd.capability.network.utils.NetworkUtil;
import com.wd.common.base.BaseApplication;
import com.wd.common.floatingview.FloatWindow;
import com.wd.common.floatingview.IFloatingView;
import com.wd.common.utils.ProcessUtils;
import com.wd.common.viewclick.BaseClickListener;
import com.wd.common.widget.ForbidDragSeekBar;
import com.wd.common.widget.MarqueeNormalTextView;
import com.wd.foundation.bean.analytics.ActionConstants;
import com.wd.foundation.bean.analytics.TrackContentBean;
import com.wd.foundation.bean.music.bean.VoicePlayerBean;
import com.wd.foundation.bean.music.bean.base.BaseAlbumItem;
import com.wd.foundation.bean.music.bean.base.BaseArtistItem;
import com.wd.foundation.bean.music.bean.base.BaseMusicItem;
import com.wd.foundation.bean.music.bean.dto.ChangeMusic;
import com.wd.foundation.bean.music.bean.dto.PlayingMusic;
import com.wd.foundation.wdkit.json.GsonUtils;
import com.wd.foundation.wdkit.utils.AnimUtil;
import com.wd.foundation.wdkit.utils.ToastNightUtil;
import com.wd.foundation.wdkit.utils.UiUtils;
import com.wd.foundation.wdkitcore.constant.BaseConstants;
import com.wd.foundation.wdkitcore.tools.AppContext;
import com.wd.foundation.wdkitcore.tools.StringUtils;
import java.security.SecureRandom;
import java.util.List;
/**
* 音频悬浮窗布局操作
* Create by KunMinX at 18/9/25
*/
public class PlayerController<B extends BaseAlbumItem<M, A>, M extends BaseMusicItem<A>, A extends BaseArtistItem> {
//开始播放时间
private long startTime;
//开始加载时间
private long loadTime;
private int mtimingMode;
//播放完当前数据
private VoicePlayerBean timingModeVoicePlayerBean;
private static final String TAG = "PlayerController";
private float mSpeed = 1.0f;
// 根布局
private ShapeConstraintLayout clRoot;
// 动画
private LottieAnimationView lavSoundWave;
// 标题
private MarqueeNormalTextView mtvTitle;
// 当前时间和总时间
private TextView tvTime;
// 播放、暂停按钮
private ImageView ivPlay;
// 关闭按钮
private ImageView ivClose;
// 进度条
private ForbidDragSeekBar seekBar;
// 收起的视图集合
private Group gPackUp;
// 展开的视图集合
private Group gUnfold;
private final PlayingInfoManager<B, M, A> mPlayingInfoManager = new PlayingInfoManager<>();
// 是否在切换播放的歌曲
private boolean mIsChangingPlayingMusic;
// 暂时不知道这干嘛的
private ICacheProxy mICacheProxy;
private IServiceNotifier mIServiceNotifier;
private final PlayerInfoDispatcher mDispatcher = new PlayerInfoDispatcher();
private final PlayingMusic<B, M, A> mCurrentPlay = new PlayingMusic<>("00:00", "00:00");
private final ChangeMusic<B, M, A> mChangeMusic = new ChangeMusic<>();
// 播放器
private ExoPlayer mPlayer;
// 由于ExoPlayer没有进度回调,所以使用Handler更新播放进度
private final static Handler mTimerHandler = new Handler();
// 用于更新进度相关的东西
private final Runnable mTimerRunnable = this::oneSecondUpdateAudioProgress;
//播放完成暂停播放
Player.Listener playerListener = new Player.Listener() {
@Override
public void onPlaybackStateChanged(int playbackState) {
Logger.t(TAG).i("onPlaybackStateChanged");
if (playbackState == Player.STATE_ENDED && isTimingMode()) {
//进度拉满
playEnd();
}
}
@Override
public void onPlayerError(PlaybackException error) {
//播放异常埋点
// TrackContentBean trackContentBean = new TrackContentBean();
// trackContentBean.setError_information(error.getMessage());
// initCommonBurialPoint(trackContentBean);
// CommonTrack.getInstance().audioErrorTrack(trackContentBean);
}
};
public void init(Context context, IServiceNotifier iServiceNotifier, ICacheProxy iCacheProxy) {
mIServiceNotifier = iServiceNotifier;
mICacheProxy = iCacheProxy;
//停用声道数量限制
DefaultTrackSelector trackSelector = new DefaultTrackSelector(context);
trackSelector.setParameters(
trackSelector.buildUponParameters().setConstrainAudioChannelCountToDeviceCapabilities(false).build());
mPlayer = new ExoPlayer.Builder(context).setTrackSelector(trackSelector).build();
}
public boolean isInit() {
return mPlayingInfoManager.isInit();
}
/**
* 音频悬浮数据展示更新
*/
public void changeMusicTextFloatWindow() {
// 初始化悬浮窗视图
initView();
// 初始化数据
initData();
}
// 初始化布局
private void initView() {
IFloatingView iFloatingView = FloatWindow.get();
if (iFloatingView == null) {
return;
}
View view = iFloatingView.getView();
if (view == null) {
return;
}
clRoot = view.findViewById(R.id.clRoot);
lavSoundWave = view.findViewById(R.id.lavSoundWave);
mtvTitle = view.findViewById(R.id.mtvTitle);
tvTime = view.findViewById(R.id.tvTime);
ivPlay = view.findViewById(R.id.ivPlay);
ivClose = view.findViewById(R.id.ivClose);
seekBar = view.findViewById(R.id.seekBar);
gPackUp = view.findViewById(R.id.gPackUp);
gUnfold = view.findViewById(R.id.gUnfold);
// // 3 秒后发射,收起浮窗
// threeSecondLaterEmit();
// 根布局点击事件
clRoot.setOnClickListener(new BaseClickListener() {
@Override
public void onNoDoubleClick(View v) {
// 当前是收起
if (gPackUp.getVisibility() == View.VISIBLE) {
// 展开动画
unfoldAnimation();
// // 3 秒后发射,收起浮窗
// threeSecondLaterEmit();
} else if (gUnfold.getVisibility() == View.VISIBLE) { // 当前是展开
// 当前播放音频
VoicePlayerBean voicePlayerBean = (VoicePlayerBean) getCurrentPlayingMusic();
if (voicePlayerBean == null) {
return;
}
// 跳转到详情页
ProcessUtils.goToMusicDetailPageFromMiniPlayer(GsonUtils.objectToJson(voicePlayerBean));
}
}
});
// 播放暂停按钮点击事件
ivPlay.setOnClickListener(new BaseClickListener() {
@Override
public void onNoDoubleClick(View v) {
// 处理播放暂停状态
if (isPaused()) {
// 当前音频总进度
long playDuration = mPlayer.getDuration();
if (playDuration < 0) {
// 没有加载完成,可能是网络问题,重新加载
playPrevious();
}else {
// 播放音频
playAudio();
// 处理播放暂停状态
lavSoundWave.pauseAnimation();
}
} else {
// 暂停音频
pauseAudio();
// 处理播放暂停状态
lavSoundWave.resumeAnimation();
}
// // 播放、暂停按钮更新
// if (SpUtils.isNightMode()) { // 夜间
// // 播放暂停按钮
// if (isPaused()) { // 暂停播放状态
// ivPlay.setBackgroundResource(R.mipmap.voice_pause_white);
// } else {
// ivPlay.setBackgroundResource(R.mipmap.voice_play_white);
// }
// } else { // 日间
// 播放暂停按钮
if (isPaused()) { // 暂停播放状态
ivPlay.setBackgroundResource(R.mipmap.voice_pause_black);
} else {
ivPlay.setBackgroundResource(R.mipmap.voice_play_black);
}
// }
//
// // 3 秒后发射,收起浮窗
// threeSecondLaterEmit();
}
});
// 关闭按钮点击事件
ivClose.setOnClickListener(new BaseClickListener() {
@Override
public void onNoDoubleClick(View v) {
// 清除一些东西,没有细看里面代码
clear();
// 关闭悬浮窗
iFloatingView.setColseed(true);
//隐藏
iFloatingView.hide();
iFloatingView.setAudioFloatingIsShow(false);
}
});
}
// 初始化数据
private void initData() {
// 当前播放音频
VoicePlayerBean voicePlayerBean = (VoicePlayerBean) getCurrentPlayingMusic();
if (voicePlayerBean == null) {
return;
}
// 需要更新的内容,如标题、进度等
needRefreshContent(voicePlayerBean);
// 开始 1 秒 1 秒更新进度
oneSecondLaterEmit();
}
// 所有需要刷新的东西放这儿
private void needRefreshContent(VoicePlayerBean voicePlayerBean) {
// 碟片机动画
if (isPaused()) { // 处理播放暂停状态
// 处理播放暂停状态
lavSoundWave.pauseAnimation();
}else {
// 播放动画
AnimUtil.showLocalLottieEffects(lavSoundWave, "voice_anim.json");
}
// 播放暂停按钮
if (isPaused()) { // 暂停播放状态
ivPlay.setBackgroundResource(R.mipmap.voice_pause_black);
} else {
ivPlay.setBackgroundResource(R.mipmap.voice_play_black);
}
// 标题
String title = voicePlayerBean.getTitle();
// 已经设置的标题
if (mtvTitle != null) {
CharSequence curTitleCharSeq = mtvTitle.getText();
if (curTitleCharSeq != null) {
String curTitle = curTitleCharSeq.toString();
if (!TextUtils.isEmpty(title)) {
//去除搜索产生的em标签
title = title.replaceAll("<\\/?em>", "");
if (!title.equalsIgnoreCase(curTitle)) {
mtvTitle.isFocused();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mtvTitle.setText(Html.fromHtml(title, Html.FROM_HTML_MODE_LEGACY));
} else {
mtvTitle.setText(Html.fromHtml(title));
}
}
}
}
}
// 播放时间
tvTime.setText(String.format(AppContext.getContext().getResources().getString(R.string.float_audio_time), mCurrentPlay.getNowTime(), mCurrentPlay.getAllTime()));
// 进度条
if (mCurrentPlay.getDuration() != 0) {
if(seekBar.getProgress() == 100){
return;
}
if(StringUtils.isEqual(mCurrentPlay.getNowTime(),mCurrentPlay.getAllTime())){
seekBar.setProgress(100);
return;
}
int progress = mCurrentPlay.getPlayerPosition() * 100 / mCurrentPlay.getDuration();
if(progress != seekBar.getProgress()){
seekBar.setProgress(progress);
}
}else{
seekBar.setProgress(0);
}
}
// 一秒更新一次进度
private void oneSecondUpdateAudioProgress() {
// 当前音频总进度
long playDuration = mPlayer.getDuration();
// 音频当前进度
long currentPosition = mPlayer.getCurrentPosition();
// 全部播放时,没有播放第一条内容bug,播放资源没准备好获取为负数,就不走下面流程
if (playDuration < 0) {
if(NetworkUtil.isNetAvailable()){
// 一秒刷新一次
oneSecondLaterEmit();
}else if(!isPaused()){
//无网,暂停,toast提示
ToastNightUtil.showShort(R.string.tips_check_network);
pauseAudio();
}
return;
}
// 播放结束了,看看接下来怎么播放下一曲
if (currentPosition >= playDuration) {
seekBar.setProgress(100);
tvTime.setText(String.format(AppContext.getContext().getResources().getString(R.string.float_audio_time), mCurrentPlay.getAllTime(), mCurrentPlay.getAllTime()));
if(getTimingMode() == 1){
//回到初始位置
// playAgain();
restoreTimingMode();
return;
}
// 根据循环模式,决定下一首播放什么音频
if (getRepeatMode() == PlayingInfoManager.RepeatMode.SINGLE_CYCLE) { // 单曲播放
// 再播放一次
playAgain();
} else if (getRepeatMode() == PlayingInfoManager.RepeatMode.LIST_CYCLE) { // 列表播放
// 播放下一曲
playNext(true);
} else if (getRepeatMode() == PlayingInfoManager.RepeatMode.RANDOM) { // 随机播放
// 随机播放
playNext(true);
}else if(getRepeatMode() == PlayingInfoManager.RepeatMode.SINGLE){
//单曲播放,单次播放不循环
pauseAudio();
}
// 一秒刷新一次
oneSecondLaterEmit();
return;
}
// 一秒刷新一次
oneSecondLaterEmit();
// 毫秒数转化为"00:00"时间格式
String nowTime = TimeUtils.convertMillisecondsToTime(currentPosition / 1000);
// 更新信息,暂时不知道做什么的
mCurrentPlay.setNowTime(nowTime);
mCurrentPlay.setAllTime(TimeUtils.convertMillisecondsToTime(playDuration / 1000));
mCurrentPlay.setDuration((int) playDuration);
mCurrentPlay.setPlayerPosition((int) currentPosition);
// 暂时不知道做什么的
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PROGRESS, mCurrentPlay));
// 当前播放音频
VoicePlayerBean voicePlayerBean = (VoicePlayerBean) getCurrentPlayingMusic();
// 所有需要刷新的东西放这儿
needRefreshContent(voicePlayerBean);
}
// // 3s 后,收起浮窗
// private void threeSecondLaterEmit(){
// //如果已经是收起状态,不需要再次收起
// if(gUnfold.getVisibility() == View.VISIBLE){
// mPackUpHandler.removeMessages(PACK_UP_FLOAT_WINDOW);
// mPackUpHandler.sendEmptyMessageDelayed(PACK_UP_FLOAT_WINDOW , 3000);
// }
// }
// 1秒刷新一次,更新一下需要更新的东西
private void oneSecondLaterEmit() {
mTimerHandler.removeCallbacks(mTimerRunnable);
//计算更新间隔,和ios保持一致
mTimerHandler.postDelayed(mTimerRunnable, (long) (1000/mSpeed));
}
// 收起动画
public void packUpAnimation() {
if(FloatWindow.get() != null){
FloatWindow.get().packUpAnimation();
}
}
// 展开动画
public void unfoldAnimation() {
if(clRoot == null){
return;
}
// 根布局宽设为 60dp
int viewWidth = (int) AppContext.getContext().getResources().getDimension(R.dimen.rmrb_dp84);
// 屏幕宽度
float realWidth = (int) AppContext.getContext().getResources().getDimension(R.dimen.rmrb_dp267);
// 通过属性动画,模拟0-200毫秒
ValueAnimator valueAnimator = ValueAnimator.ofFloat(viewWidth, realWidth);
valueAnimator.setDuration(150);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
// 动画值
float animatedValue = (float) valueAnimator.getAnimatedValue();
// 根布局宽度
UiUtils.setWH(clRoot, (int) animatedValue, viewWidth);
}
});
valueAnimator.start();
// 展示展开布局
new Handler().postDelayed(() -> {
// 展示展开布局
gPackUp.setVisibility(View.GONE);
gUnfold.setVisibility(View.VISIBLE);
if (FloatWindow.get() != null) {
FloatWindow.get().setExpand(true);
}
}, 150);
}
// 加载合集
public void loadAlbum(B musicAlbum) {
setAlbum(musicAlbum, 0);
}
// 加载合集
public void loadAlbum(B musicAlbum, int albumIndex) {
setAlbum(musicAlbum, albumIndex);
playAudio();
}
// 设置合集
private void setAlbum(B musicAlbum, int albumIndex) {
mPlayingInfoManager.setMusicAlbum(musicAlbum);
mPlayingInfoManager.setAlbumIndex(albumIndex);
setChangingPlayingMusic(true);
}
// 是否播放中
public boolean isPlaying() {
return mPlayer.isPlaying();
}
// 是否暂停
public boolean isPaused() {
return !mPlayer.getPlayWhenReady();
}
// 播放音频
public void playAudio(int albumIndex) {
if (isPlaying() && albumIndex == mPlayingInfoManager.getAlbumIndex()) {
return;
}
mPlayingInfoManager.setAlbumIndex(albumIndex);
setChangingPlayingMusic(true);
playAudio();
}
// 播放音频
public void playAudio() {
startTime = System.currentTimeMillis();
if(getRepeatMode() == PlayingInfoManager.RepeatMode.SINGLE && mPlayer != null && mPlayer.getCurrentPosition()>=mPlayer.getDuration()){
playAgain();
}
if (mIsChangingPlayingMusic) {
getUrlAndPlay();
} else if (isPaused()) {
resumeAudio();
}
BaseConstants.isCloseMusicDialog = false;
}
// 获取音频地址并播放
private void getUrlAndPlay() {
long startLoadTime = System.currentTimeMillis();
String url;
M freeMusic;
freeMusic = mPlayingInfoManager.getCurrentPlayingMusic();
if (freeMusic == null) {
pauseAudio();
return;
}
url = freeMusic.getUrl();
AdaptScreenUtils.addMusicIdOld(freeMusic.getObjectId());
if (TextUtils.isEmpty(url)) {
pauseAudio();
} else {
MediaItem item;
if ((url.contains("http:") || url.contains("ftp:") || url.contains("https:"))) {
item = MediaItem.fromUri(mICacheProxy.getCacheUrl(url));
} else if (url.contains("storage")) {
item = MediaItem.fromUri(url);
} else {
item = MediaItem.fromUri(Uri.parse("file:///android_asset/" + url));
}
mPlayer.setMediaItem(item, true);
mPlayer.prepare();
mPlayer.play();
afterPlay();
}
loadTime = System.currentTimeMillis()- startLoadTime;
}
// 播放后要处理的东西
private void afterPlay() {
if(FloatWindow.get() != null){
FloatWindow.get().setColseed(false);
}
setChangingPlayingMusic(false);
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS, false));
if (mIServiceNotifier != null) {
mIServiceNotifier.notifyService(true);
}
}
// 请求最后播放信息
public void requestLastPlayingInfo() {
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PROGRESS, mCurrentPlay));
if(mtimingMode == 1){
restoreTimingMode();
}
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_CHANGE_MUSIC, mChangeMusic));
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS, isPaused()));
}
public void requestDownCount() {
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_COUNTDOWN));
}
public void setSeek(int progress) {
mPlayer.seekTo(progress);
}
public String getTrackTime(int progress) {
return TimeUtils.convertMillisecondsToTime(progress / 1000);
}
public void playNext(boolean auto) {
burialPointNormal(auto);
mPlayingInfoManager.countNextIndex();
setChangingPlayingMusic(true);
playAudio();
}
public void playPrevious() {
mPlayingInfoManager.countPreviousIndex();
setChangingPlayingMusic(true);
playAudio();
}
public void playAgain() {
burialPointNormal(true);
mCurrentPlay.setNowTime("00:00");
mCurrentPlay.setPlayerPosition(0);
if(seekBar != null){
seekBar.setProgress(0);
}
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PROGRESS, mCurrentPlay));
mPlayer.seekTo(0);
}
// 暂停音频
public void pauseAudio() {
mPlayer.pause();
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS, true));
if (mIServiceNotifier != null) {
mIServiceNotifier.notifyService(true);
}
burialPointNormal(false);
}
private VoicePlayerBean initCommonBurialPoint(TrackContentBean trackContentBean){
VoicePlayerBean voicePlayerBean = (VoicePlayerBean) mPlayingInfoManager.getCurrentPlayingMusic();
if(voicePlayerBean == null){
return null;
}
//其余非推荐字段同内容曝光
trackContentBean.setPage_name(voicePlayerBean.pageName);
trackContentBean.setPage_id(voicePlayerBean.pageId);
trackContentBean.setContent_name(voicePlayerBean.getTitle());
trackContentBean.setContent_type(voicePlayerBean.getContentType());
trackContentBean.setContent_id(voicePlayerBean.getObjectId());
if(voicePlayerBean.getTraceBean() != null) {
trackContentBean.setTraceBean(voicePlayerBean.getTraceBean());
}
trackContentBean.channelSourceId = voicePlayerBean.getChannelId();
return voicePlayerBean;
}
/**
* 正常埋点,正片播放和播放结束
* */
private void burialPointNormal(boolean isComplete) {
// long duration = (System.currentTimeMillis()-startTime)/1000;
// TrackContentBean trackContentBean = new TrackContentBean();
// //加载时间
// trackContentBean.setTime_consuming((int) loadTime);
// //行为类型 点击
// trackContentBean.setAction(ActionConstants.DETAIL_PAGE_SHOW);
// //分享渠道
// trackContentBean.shareChannel = "";
// //浏览时长,精确到秒
// trackContentBean.setDuration(duration);
// initCommonBurialPoint(trackContentBean);
// //其它推荐字段同内容曝光
// if(duration >= 5){
// //正片播放、播放结束埋点
// CommonTrack.getInstance().audioPlayTrack(trackContentBean);
// }
//
// trackContentBean.setComplet_rate(isComplete?1:0);
//
// //播放中断或完成
// CommonTrack.getInstance().audioEndTrack(trackContentBean);
}
public void playEnd(){
mPlayer.pause();
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS, true, true, mCurrentPlay));
if (mIServiceNotifier != null) {
mIServiceNotifier.notifyService(true);
}
burialPointNormal(true);
}
public void resumeAudio() {
mPlayer.play();
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS, false));
if (mIServiceNotifier != null) {
mIServiceNotifier.notifyService(true);
}
}
public void clear() {
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS, true));
//存在音频详情页的情况下,不清理那么干净
if(!BaseApplication.getInstance().getLifecycleCallbacks().hasActivity("com.people.musicplayer.ui.activity.MusicNewPlayerActivity")){
mPlayer.stop();
mPlayer.clearMediaItems();
mTimerHandler.removeCallbacksAndMessages(null);
resetIsChangingPlayingChapter();
if (mIServiceNotifier != null) {
mIServiceNotifier.notifyService(false);
}
// 主动关闭音频浮窗后,专辑置为空
mPlayingInfoManager.setMusicAlbum(null);
}else{
mPlayer.pause();
}
//主动关闭音频浮窗
BaseConstants.isCloseMusicDialog = true;
}
public void resetIsChangingPlayingChapter() {
mIsChangingPlayingMusic = true;
setChangingPlayingMusic(true);
}
public void changeMode() {
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_REPEAT_MODE, mPlayingInfoManager.changeMode()));
}
/**
* 切换模式
* */
public void onlyChangeMode(){
mPlayingInfoManager.changeMode();
}
public B getAlbum() {
return mPlayingInfoManager.getMusicAlbum();
}
public List<M> getAlbumMusics() {
return mPlayingInfoManager.getOriginPlayingList();
}
// 设置是否在切换播放的歌曲
public void setChangingPlayingMusic(boolean changingPlayingMusic) {
// 是否在切换播放的歌曲
mIsChangingPlayingMusic = changingPlayingMusic;
// 切换音频,音频信息全部重置
if (mIsChangingPlayingMusic) {
mChangeMusic.setBaseInfo(mPlayingInfoManager.getMusicAlbum(), getCurrentPlayingMusic());
if(mtimingMode == 1){
restoreTimingMode();
}
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_CHANGE_MUSIC, mChangeMusic));
mCurrentPlay.setBaseInfo(mPlayingInfoManager.getMusicAlbum(), getCurrentPlayingMusic());
mCurrentPlay.setNowTime("00:00");
mCurrentPlay.setAllTime("00:00");
mCurrentPlay.setPlayerPosition(0);
mCurrentPlay.setDuration(0);
}
}
public int getAlbumIndex() {
return mPlayingInfoManager.getAlbumIndex();
}
public Enum<PlayingInfoManager.RepeatMode> getRepeatMode() {
return mPlayingInfoManager.getRepeatMode();
}
public void togglePlay() {
if (isPlaying()) {
pauseAudio();
} else {
playAudio();
}
}
public M getCurrentPlayingMusic() {
return mPlayingInfoManager.getCurrentPlayingMusic();
}
public PlayerInfoDispatcher getDispatcher() {
return mDispatcher;
}
public String getNowTime() {
long currentPositon = mPlayer.getCurrentPosition();
return TimeUtils.convertMillisecondsToTime(currentPositon / 1000);
}
public String getAllTime() {
return TimeUtils.convertMillisecondsToTime(mPlayer.getDuration() / 1000);
}
public long getResidueDuration(){
return mPlayer.getDuration() - mPlayer.getCurrentPosition();
}
public long getCurrentProcess(){
return mPlayer.getCurrentPosition();
}
//speed 参数表示要设置的播放速度比例,例如 2f 表示两倍速度。
public void switchPlaybackSpeed(float speed) {
mSpeed = speed;
PlaybackParameters playbackParameters = new PlaybackParameters(speed, 1f);
mPlayer.setPlaybackParameters(playbackParameters);
}
//播放完当前 暂替
public void afterPlayingPause(int timingMode) {
if(timingMode == 0){
mPlayer.removeListener(playerListener);
}else{
mPlayer.addListener(playerListener);
}
}
//随机播放
public void randomPlay() {
mPlayingInfoManager.changeRANDOM();
int min = 0;
int max = getAlbumMusics().size();
if (max == 0) {
return;
}
SecureRandom random = new SecureRandom();
int num = random.nextInt(max) % (max - min + 1) + min;
playAudio(num);
}
//单首播放
public void singleCyclechange() {
mPlayingInfoManager.changeSINGLE_CYCLE();
}
//列表播放
public void singleListChange() {
mPlayingInfoManager.changeLIST_CYCLE();
}
//单曲播放,不循环
public void single() {
mPlayingInfoManager.changeSingle();
}
public void setFirstPlayAudio() {
if (FloatWindow.get() != null) {
FloatWindow.get().setFirst(true);
}
}
public int getTotalTime() {
return (int) mPlayer.getDuration();
}
// 播放完当前 暂替
public void timingMode(int timingMode) {
//增加当前音乐信息,切换其他音乐需恢复默认状态。
mtimingMode = timingMode;
VoicePlayerBean bean = (VoicePlayerBean) getCurrentPlayingMusic();
if(bean != null && timingMode == 1){
timingModeVoicePlayerBean = new VoicePlayerBean();
timingModeVoicePlayerBean.contentBean = bean.contentBean;
timingModeVoicePlayerBean.setUrl(bean.getUrl());
timingModeVoicePlayerBean.setObjectId(bean.getObjectId());
timingModeVoicePlayerBean.setRelType(bean.getRelType());
timingModeVoicePlayerBean.setContentRelId(bean.getContentRelId());
}else{
timingModeVoicePlayerBean = null;
}
afterPlayingPause(timingMode);
}
public int getTimingMode(){
return mtimingMode;
}
public void restoreTimingMode(){
mtimingMode = 0;
timingModeVoicePlayerBean = null;
}
public boolean isTimingMode(){
boolean result = mtimingMode == 1 && timingModeVoicePlayerBean != null && timingModeVoicePlayerBean.isSameObject(getCurrentPlayingMusic());
// if(!result && mtimingMode == 1){
// //重置
// restoreTimingMode();
// }
return result;
}
}