121 lines
4.2 KiB
Java
121 lines
4.2 KiB
Java
|
|
package com.xscm.moduleutil.widget;
|
||
|
|
|
||
|
|
import android.content.Context;
|
||
|
|
import android.view.View;
|
||
|
|
|
||
|
|
import com.petterp.floatingx.listener.control.IFxControl;
|
||
|
|
import com.xscm.moduleutil.R;
|
||
|
|
import com.xscm.moduleutil.databinding.RoomDialogMusicWindowOpenBinding;
|
||
|
|
import com.xscm.moduleutil.interfaces.OnMusicItemClickListener;
|
||
|
|
import com.xscm.moduleutil.utils.ImageUtils;
|
||
|
|
|
||
|
|
import io.agora.musiccontentcenter.Music;
|
||
|
|
|
||
|
|
public class CustomMusicFloatingView {
|
||
|
|
private final IFxControl fxControl;
|
||
|
|
private final Context context;
|
||
|
|
private RoomDialogMusicWindowOpenBinding binding;
|
||
|
|
private OnMusicItemClickListener onItemClickListener;
|
||
|
|
private boolean isInitialized = false;
|
||
|
|
|
||
|
|
public CustomMusicFloatingView(Context context, IFxControl fxControl) {
|
||
|
|
this.context = context;
|
||
|
|
this.fxControl = fxControl;
|
||
|
|
// this.binding = DataBindingUtil.inflate(LayoutInflater.from(context),
|
||
|
|
// R.layout.room_dialog_music_window_open, null, false);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void initView() {
|
||
|
|
View floatingView = fxControl.getView();
|
||
|
|
if (floatingView == null || isInitialized) return;
|
||
|
|
|
||
|
|
// 替换为绑定方式获取控件
|
||
|
|
binding = RoomDialogMusicWindowOpenBinding.bind(floatingView);
|
||
|
|
|
||
|
|
// 设置点击事件
|
||
|
|
binding.ivMinx.setOnClickListener(v -> {
|
||
|
|
if (onItemClickListener != null) {
|
||
|
|
onItemClickListener.onMinimize();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
binding.ivMusicPlayState.setOnClickListener(v -> {
|
||
|
|
if (onItemClickListener != null) {
|
||
|
|
if (binding.ivMusicPlayState.getTag() instanceof Boolean) {
|
||
|
|
boolean isPlaying = (boolean) binding.ivMusicPlayState.getTag();
|
||
|
|
if (isPlaying) {
|
||
|
|
onItemClickListener.onPause();
|
||
|
|
} else {
|
||
|
|
onItemClickListener.onResume();
|
||
|
|
}
|
||
|
|
binding.ivMusicPlayState.setImageResource(isPlaying ?
|
||
|
|
R.mipmap.room_music_win_puase : R.mipmap.room_music_win_start);
|
||
|
|
binding.ivMusicPlayState.setTag(!isPlaying);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
binding.ivList.setOnClickListener(v -> {
|
||
|
|
if (onItemClickListener != null) {
|
||
|
|
onItemClickListener.onOpenList();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
binding.ivNext.setOnClickListener(v -> {
|
||
|
|
if (onItemClickListener != null) {
|
||
|
|
onItemClickListener.onNext();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
isInitialized = true; // 只绑定一次
|
||
|
|
}
|
||
|
|
|
||
|
|
public void show() {
|
||
|
|
fxControl.show();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void hide() {
|
||
|
|
fxControl.hide();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void destroy() {
|
||
|
|
fxControl.cancel();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void setOnItemClickListener(OnMusicItemClickListener listener) {
|
||
|
|
this.onItemClickListener = listener;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void updatePlayState(boolean isPlaying) {
|
||
|
|
// if (fxControl.getView() == null) return;
|
||
|
|
// binding = RoomDialogMusicWindowOpenBinding.bind(fxControl.getView());
|
||
|
|
binding.ivMusicPlayState.setImageResource(isPlaying ?
|
||
|
|
R.mipmap.room_music_win_start : R.mipmap.room_music_win_puase);
|
||
|
|
binding.ivMusicPlayState.setTag(isPlaying);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void updateTitle(String title, String singer) {
|
||
|
|
if (fxControl.getView() == null) return;
|
||
|
|
binding = RoomDialogMusicWindowOpenBinding.bind(fxControl.getView());
|
||
|
|
binding.tvMusicTitle.setText(title);
|
||
|
|
binding.tvSinger.setText(singer);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void onMusicEvent(Music event) {
|
||
|
|
// updatePlayState(true);
|
||
|
|
ImageUtils.loadHeadCC(event.getPoster(), binding.musicImg);
|
||
|
|
binding.tvMusicTitle.setText(event.getName());
|
||
|
|
binding.tvSinger.setText(event.getSinger());
|
||
|
|
binding.musicM.setText("/" + formatSecondsToMinutes(event.getDurationS()));
|
||
|
|
}
|
||
|
|
|
||
|
|
public void updateProgress(int progress) {
|
||
|
|
updatePlayState(true);
|
||
|
|
binding.musicT.setText(formatSecondsToMinutes(progress/1000));
|
||
|
|
}
|
||
|
|
public static String formatSecondsToMinutes(int totalSeconds) {
|
||
|
|
int minutes = totalSeconds / 60;
|
||
|
|
int seconds = totalSeconds % 60;
|
||
|
|
return String.format("%02d:%02d", minutes, seconds);
|
||
|
|
}
|
||
|
|
}
|