|
@@ -1,19 +1,71 @@
|
|
|
package com.daya.live_teaching.widget;
|
|
|
|
|
|
import android.content.Context;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Looper;
|
|
|
+import android.os.Message;
|
|
|
+import android.text.TextUtils;
|
|
|
import android.util.AttributeSet;
|
|
|
+import android.util.Log;
|
|
|
import android.view.LayoutInflater;
|
|
|
+import android.view.View;
|
|
|
import android.widget.FrameLayout;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.TextView;
|
|
|
|
|
|
+import com.cooleshow.base.utils.DateUtil;
|
|
|
import com.daya.live_teaching.R;
|
|
|
+import com.daya.live_teaching.rtc.RtcAudioMixerControl;
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
import androidx.annotation.Nullable;
|
|
|
+import androidx.appcompat.widget.AppCompatSeekBar;
|
|
|
+import androidx.constraintlayout.widget.Group;
|
|
|
|
|
|
/**
|
|
|
* Author by pq, Date on 2022/11/15.
|
|
|
*/
|
|
|
-public class CourseWarePlayView extends FrameLayout {
|
|
|
+public class CourseWarePlayView extends FrameLayout implements View.OnClickListener {
|
|
|
+ public static final int PLAY_STATUS_LOADING = 0;//加载中
|
|
|
+ public static final int PLAY_STATUS_PLAYING = 1;//播放中
|
|
|
+ public static final int PLAY_STATUS_PAUSE = 2;//暂停
|
|
|
+ public static final int PLAY_STATUS_STOP = 4;//停止播放
|
|
|
+ public static final int PLAY_STATUS_FAIL = 3;//播放失败
|
|
|
+ public static final int MAX_SEEKBAR_PROGRESS = 100;//进度条最大值
|
|
|
+
|
|
|
+
|
|
|
+ public static final int UPDATE_PROGRESS_TYPE = 1001;
|
|
|
+ public static final int UPDATE_PLAY_STATUS = 1002;
|
|
|
+
|
|
|
+ private TextView mTvTitle;
|
|
|
+ private Group mGroupPlayInfo;
|
|
|
+ private ImageView mIvStatusIcon;
|
|
|
+ private TextView mTvProgress;
|
|
|
+ private TextView mTvStatusText;
|
|
|
+ private AppCompatSeekBar mSeekbar;
|
|
|
+ private ImageView mIvPlay;
|
|
|
+ private int coursewareTotalTime = 0;
|
|
|
+ private int playStatus = PLAY_STATUS_LOADING;
|
|
|
+ private RtcAudioMixerControl mRtcAudioMixerControl;
|
|
|
+ private Handler mHandler = new Handler(Looper.getMainLooper()) {
|
|
|
+ @Override
|
|
|
+ public void handleMessage(@NonNull Message msg) {
|
|
|
+ super.handleMessage(msg);
|
|
|
+ if (msg.what == UPDATE_PROGRESS_TYPE) {
|
|
|
+ float progress = (float) msg.obj;
|
|
|
+ setPlayProgress(progress);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (msg.what == UPDATE_PLAY_STATUS) {
|
|
|
+ int playStatus = (int) msg.obj;
|
|
|
+ setPlayStatus(playStatus);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
public CourseWarePlayView(@NonNull Context context) {
|
|
|
this(context, null);
|
|
|
}
|
|
@@ -29,6 +81,168 @@ public class CourseWarePlayView extends FrameLayout {
|
|
|
|
|
|
private void init() {
|
|
|
LayoutInflater.from(getContext()).inflate(R.layout.view_course_ware_play_layout, this);
|
|
|
+ mTvTitle = findViewById(R.id.tv_title);
|
|
|
+ mGroupPlayInfo = findViewById(R.id.group_play_info);
|
|
|
+ mIvPlay = findViewById(R.id.iv_play);
|
|
|
+ mSeekbar = findViewById(R.id.seekbar);
|
|
|
+ mTvProgress = findViewById(R.id.tv_progress);
|
|
|
+ mTvStatusText = findViewById(R.id.tv_status_text);
|
|
|
+ mIvStatusIcon = findViewById(R.id.iv_status_icon);
|
|
|
+ mIvPlay.setOnClickListener(this);
|
|
|
+ mIvStatusIcon.setOnClickListener(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setName(String name) {
|
|
|
+ if (mTvTitle != null) {
|
|
|
+ mTvTitle.setText(name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setTotalProgress(int totalTime) {
|
|
|
+ this.coursewareTotalTime = totalTime;
|
|
|
+ Log.i("pq", "total Time:" + coursewareTotalTime);
|
|
|
+ String s = DateUtil.dateFormatmm_ss(totalTime);
|
|
|
+ mTvProgress.setText("00:00/" + s);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Added from 5.1.4
|
|
|
+ * 混音播放进度,默认 200 毫秒回调一次
|
|
|
+ *
|
|
|
+ * @param progress 播放进度 [0,1]
|
|
|
+ */
|
|
|
+ public void setPlayProgress(float progress) {
|
|
|
+ Log.i("pq", "progress:" + progress);
|
|
|
+ int currentProgress = (int) (MAX_SEEKBAR_PROGRESS * progress);
|
|
|
+ Log.i("pq", "currentProgress:" + currentProgress);
|
|
|
+ if (mSeekbar != null) {
|
|
|
+ mSeekbar.setProgress(currentProgress);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setPlayStatus(int playStatus) {
|
|
|
+ this.playStatus = playStatus;
|
|
|
+ if (playStatus == PLAY_STATUS_LOADING) {
|
|
|
+ mGroupPlayInfo.setVisibility(View.GONE);
|
|
|
+ mIvStatusIcon.setVisibility(View.GONE);
|
|
|
+ mTvStatusText.setText("正在加载...");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (playStatus == PLAY_STATUS_FAIL) {
|
|
|
+ //加载失败
|
|
|
+ mGroupPlayInfo.setVisibility(View.GONE);
|
|
|
+ mIvStatusIcon.setVisibility(View.VISIBLE);
|
|
|
+ mIvStatusIcon.setImageResource(R.drawable.icon_play_courseware_reload);
|
|
|
+ mTvStatusText.setText("重新加载");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (playStatus == PLAY_STATUS_PLAYING) {
|
|
|
+ //播放中
|
|
|
+ mGroupPlayInfo.setVisibility(View.VISIBLE);
|
|
|
+ mIvStatusIcon.setVisibility(View.VISIBLE);
|
|
|
+ mIvStatusIcon.setImageResource(R.drawable.icon_stop_play_course_ware);
|
|
|
+ mTvStatusText.setText("正在播放");
|
|
|
+ mIvPlay.setImageResource(R.drawable.icon_play_course_ware);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (playStatus == PLAY_STATUS_PAUSE || playStatus == PLAY_STATUS_STOP) {
|
|
|
+ //播放中
|
|
|
+ mGroupPlayInfo.setVisibility(View.VISIBLE);
|
|
|
+ mIvStatusIcon.setVisibility(View.VISIBLE);
|
|
|
+ mIvStatusIcon.setImageResource(R.drawable.icon_stop_play_course_ware);
|
|
|
+ mTvStatusText.setText("播放暂停");
|
|
|
+ mIvPlay.setImageResource(R.drawable.icon_play_course_ware);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ int id = v.getId();
|
|
|
+ if (id == R.id.iv_play) {
|
|
|
+ if (playStatus == PLAY_STATUS_PLAYING) {
|
|
|
+ getAudioMixerManager().pause();
|
|
|
+ }
|
|
|
+ if (playStatus == PLAY_STATUS_PAUSE) {
|
|
|
+ getAudioMixerManager().resume();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (id == R.id.iv_status_icon) {
|
|
|
+ if (playStatus == PLAY_STATUS_FAIL) {
|
|
|
+ //刷新
|
|
|
+ } else {
|
|
|
+ stop();
|
|
|
+ setVisibility(View.GONE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private RtcAudioMixerControl getAudioMixerManager() {
|
|
|
+ if (mRtcAudioMixerControl == null) {
|
|
|
+ mRtcAudioMixerControl = new RtcAudioMixerControl(new RtcAudioMixerControl.OnEventListener() {
|
|
|
+ @Override
|
|
|
+ public void onProgress(float progress) {
|
|
|
+ Message obtain = Message.obtain();
|
|
|
+ obtain.what = UPDATE_PROGRESS_TYPE;
|
|
|
+ obtain.obj = progress;
|
|
|
+ mHandler.sendMessage(obtain);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onStartPlay() {
|
|
|
+ updatePlayCoursewareStatus(CourseWarePlayView.PLAY_STATUS_PLAYING);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPausePlay() {
|
|
|
+ updatePlayCoursewareStatus(CourseWarePlayView.PLAY_STATUS_PAUSE);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onResumePlay() {
|
|
|
+ updatePlayCoursewareStatus(CourseWarePlayView.PLAY_STATUS_PLAYING);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onStop() {
|
|
|
+ updatePlayCoursewareStatus(CourseWarePlayView.PLAY_STATUS_STOP);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return mRtcAudioMixerControl;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updatePlayCoursewareStatus(int playStatus) {
|
|
|
+ Message obtain = Message.obtain();
|
|
|
+ obtain.what = UPDATE_PLAY_STATUS;
|
|
|
+ obtain.obj = playStatus;
|
|
|
+ mHandler.sendMessage(obtain);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void stop() {
|
|
|
+ getAudioMixerManager().stop();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void release() {
|
|
|
+ if (mRtcAudioMixerControl != null) {
|
|
|
+ mRtcAudioMixerControl.stop();
|
|
|
+ }
|
|
|
+ if (mHandler != null) {
|
|
|
+ mHandler.removeCallbacksAndMessages(null);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ public void start(String fileUrl) {
|
|
|
+ if (TextUtils.isEmpty(fileUrl)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ setPlayStatus(PLAY_STATUS_LOADING);
|
|
|
+ getAudioMixerManager().startMix(fileUrl);
|
|
|
}
|
|
|
}
|