From 7d994c1937aa4fe104945e0a3508705921ffc186 Mon Sep 17 00:00:00 2001 From: liujing Date: Tue, 23 Feb 2021 19:46:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=B4=E6=92=AD=E6=8B=89=E6=B5=81=E6=8D=A2ze?= =?UTF-8?q?go->view=E6=B7=BB=E5=8A=A0=20=20=E5=BE=85=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/mogo-module-v2x/build.gradle | 1 + .../module/v2x/view/CarZegoLiveVideoView.java | 272 ++++++++++++++++++ .../res/layout/view_video_layout_see_live.xml | 10 +- 3 files changed, 281 insertions(+), 2 deletions(-) create mode 100644 modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/view/CarZegoLiveVideoView.java diff --git a/modules/mogo-module-v2x/build.gradle b/modules/mogo-module-v2x/build.gradle index 2eaadb3a28..b84e556603 100644 --- a/modules/mogo-module-v2x/build.gradle +++ b/modules/mogo-module-v2x/build.gradle @@ -69,6 +69,7 @@ dependencies { kapt rootProject.ext.dependencies.roomAnnotationProcessor kapt rootProject.ext.dependencies.aroutercompiler + api rootProject.ext.dependencies.mogoaicloudtrafficlive if (Boolean.valueOf(RELEASE)) { implementation rootProject.ext.dependencies.mogomap diff --git a/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/view/CarZegoLiveVideoView.java b/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/view/CarZegoLiveVideoView.java new file mode 100644 index 0000000000..99e264edaf --- /dev/null +++ b/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/view/CarZegoLiveVideoView.java @@ -0,0 +1,272 @@ +package com.mogo.module.v2x.view; + +import android.content.Context; +import android.content.Intent; +import android.graphics.PorterDuff; +import android.text.TextUtils; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.SurfaceView; +import android.view.View; +import android.widget.ProgressBar; +import android.widget.TextView; +import androidx.constraintlayout.widget.ConstraintLayout; +import androidx.core.content.ContextCompat; + +import com.mogo.cloud.trafficlive.api.MoGoAiCloudTrafficLive; +import com.mogo.commons.AbsMogoApplication; +import com.mogo.commons.voice.AIAssist; +import com.mogo.module.common.entity.MarkerCarInfo; +import com.mogo.module.v2x.R; +import com.mogo.module.v2x.V2XServiceManager; +import com.mogo.module.v2x.entity.net.V2XLivePushVoRes; +import com.mogo.module.v2x.network.V2XRefreshCallback; +import com.mogo.module.v2x.voice.V2XVoiceCallbackListener; +import com.mogo.module.v2x.voice.V2XVoiceConstants; +import com.mogo.module.v2x.voice.V2XVoiceManager; +import com.mogo.utils.logger.Logger; + +import static com.mogo.module.v2x.V2XConst.MODULE_NAME; + +/** + * @author liujing + * @description 描述 + * @since: 2021/2/23 + */ +public class CarZegoLiveVideoView extends RoundLayout { + private SurfaceView mTxcVideoView; + private ProgressBar mLoading; + // private TXLivePlayer mLivePlayer; + private ConstraintLayout mClLoadError; + private TextView mTvRefreshButton; + + private MarkerCarInfo.CarLiveInfo mCarLiveInfo; + // 重新刷新直播流 + private V2XVoiceCallbackListener v2XVoiceCallbackRefreshListener = new V2XVoiceCallbackListener() { + @Override + public void onCallback(String command, Intent intent) { + mLoading.setVisibility(VISIBLE); + mClLoadError.setVisibility(GONE); + if (mCarLiveInfo != null) { + startLive(mCarLiveInfo); + } + } + }; + + public CarZegoLiveVideoView(Context context) { + this(context, null); + } + + public CarZegoLiveVideoView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public CarZegoLiveVideoView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + initView(context); + } + + private void initView(Context context) { + LayoutInflater.from(context) + .inflate(R.layout.view_video_layout_see_live, this); + //mPlayerView 即 step1 中添加的界面 view + mTxcVideoView = findViewById(R.id.surface); +// //创建 player 对象 +// mLivePlayer = new TXLivePlayer(context); +// //关键 player 对象与界面 view +// mLivePlayer.setPlayerView(mTxcVideoView); +// mLivePlayer.setMute(true); +// mLivePlayer.enableHardwareDecode(true); + + mLoading = findViewById(R.id.loading); + mLoading.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(context, R.color.live_video_progress_bar_loading_color), PorterDuff.Mode.MULTIPLY); + + mClLoadError = findViewById(R.id.clLoadError); + mTvRefreshButton = findViewById(R.id.tvRefreshButton); + mTvRefreshButton.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + mLoading.setVisibility(VISIBLE); + mClLoadError.setVisibility(GONE); + if (mCarLiveInfo != null) { + startLive(mCarLiveInfo); + } + } + }); + } + + /** + * 设置直播信息 + */ + public void setCarLiveInfo(MarkerCarInfo.CarLiveInfo carLiveInfo) { + mCarLiveInfo = carLiveInfo; + } + + /** + * 开始直播 + * + * @param carLiveInfo 要直播的车机,如果没有直播的地址需要重新获取最新的直播地址 + */ + public void startLive(MarkerCarInfo.CarLiveInfo carLiveInfo) { + // 进行直播播放 + if (mTxcVideoView != null + && carLiveInfo != null) { + if (!TextUtils.isEmpty(carLiveInfo.getVideoUrl())) { + AIAssist.getInstance(AbsMogoApplication.getApp()).speakTTSVoice(AbsMogoApplication.getApp().getString(R.string.v2x_voice_see_front_car_live)); + setCarLiveInfo(carLiveInfo); + playLiveVideo(carLiveInfo); + } + // 根据SN重新获取直播流地址 + else { + V2XServiceManager + .getV2XRefreshModel() + .livePush(new V2XRefreshCallback() { + @Override + public void onSuccess(V2XLivePushVoRes result) { + AIAssist.getInstance(AbsMogoApplication.getApp()).speakTTSVoice(AbsMogoApplication.getApp().getString(R.string.v2x_voice_see_front_car_live)); + mClLoadError.setVisibility(GONE); + mClLoadError.setVisibility(GONE); + try { + MarkerCarInfo.CarLiveInfo carRealLiveInfo = new MarkerCarInfo.CarLiveInfo(); + carRealLiveInfo.setVideoUrl(result.getResult().getPlayUrl().getRtmp()); + carRealLiveInfo.setVideoSn(carLiveInfo.getVideoSn()); + carRealLiveInfo.setVideoChannel(result.getResult().getVideoChannel()); + setCarLiveInfo(carLiveInfo); + playLiveVideo(carRealLiveInfo); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + public void onFail(String msg) { + Logger.e(MODULE_NAME, "播放器:" + msg); + AIAssist.getInstance(AbsMogoApplication.getApp()).speakTTSVoice(AbsMogoApplication.getApp().getString(R.string.v2x_voice_see_front_car_live_error)); + mLoading.setVisibility(GONE); + mClLoadError.setVisibility(VISIBLE); + } + }, carLiveInfo.getVideoSn(), 0); + } + } + } + + /** + * 播放直播流,且开始心跳 + */ + private void playLiveVideo(MarkerCarInfo.CarLiveInfo carLiveInfo) { + startHeartLive(carLiveInfo); + if (mTxcVideoView != null) { +// MoGoAiCloudTrafficLive.viewVehicleHeadLive(this.getApplication(), liveSn, surfaceView, this); +// mLivePlayer.startPlay(carLiveInfo.getVideoUrl(), TXLivePlayer.PLAY_TYPE_LIVE_RTMP); +// mLivePlayer.setPlayListener(new ITXLivePlayListener() { +// @Override +// public void onPlayEvent(int event, Bundle bundle) { +// Logger.w(MODULE_NAME, "播放器:onPlayEvent==" + event + "===bundle===" + bundle); +// if (event == TXLiveConstants.PLAY_EVT_PLAY_LOADING) { +// mLoading.setVisibility(VISIBLE); +// mClLoadError.setVisibility(GONE); +// } else if (event == TXLiveConstants.PLAY_EVT_PLAY_BEGIN) { +// refreshStatusToListener(true); +// mLoading.setVisibility(GONE); +// mClLoadError.setVisibility(GONE); +// } else if (event < 0) { +// refreshStatusToListener(false); +// AIAssist.getInstance(V2XUtils.getApp()).speakTTSVoice("直播获取识败,可以对我说重试", null); +// stopLive(mCarLiveInfo); +// mLoading.setVisibility(GONE); +// mClLoadError.setVisibility(VISIBLE); +// // 注册语音交互 +// V2XVoiceManager.INSTANCE +// .registerWakeCmd(V2XVoiceConstants.COMMAND_ZHIDAO_V2X_REFRESH_CAR_LIVE, +// v2XVoiceCallbackRefreshListener) +// .registerUnWakeVoice(V2XVoiceConstants.COMMAND_ZHIDAO_V2X_REFRESH_LIVE_UN_WAKEUP, +// v2XVoiceCallbackRefreshListener); +// } +// } +// +// @Override +// public void onNetStatus(Bundle bundle) { +// //Logger.w(MODULE_NAME, "播放器:onNetStatus===bundle===" + bundle); +// } +// }); + } + } + + // 刷新直播心跳 + private void startHeartLive(MarkerCarInfo.CarLiveInfo carLiveInfo) { + try { + if (!TextUtils.isEmpty(carLiveInfo.getVideoSn()) + && !TextUtils.isEmpty(carLiveInfo.getVideoChannel())) { + V2XServiceManager + .getV2XRefreshModel() + .refreshHeartBeat(carLiveInfo.getVideoSn(), + carLiveInfo.getVideoChannel(), + null); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void stopLive(MarkerCarInfo.CarLiveInfo carLiveInfo) { + try { + Logger.w(MODULE_NAME, "心跳:关闭直播..."); + // 暂停 +// mLivePlayer.pause(); +// // true 代表清除最后一帧画面 +// mLivePlayer.stopPlay(true); +// mTxcVideoView.onDestroy(); + // 停止推流 + V2XServiceManager + .getV2XRefreshModel() + .livePush(new V2XRefreshCallback() { + @Override + public void onSuccess(V2XLivePushVoRes result) { + Logger.d(MODULE_NAME, "播放器:" + result); + } + + @Override + public void onFail(String msg) { + Logger.e(MODULE_NAME, "播放器:" + msg); + } + }, carLiveInfo.getVideoSn(), 1); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + if (mCarLiveInfo != null) { + startLive(mCarLiveInfo); + } + } + + @Override + protected void onDetachedFromWindow() { + stopLive(mCarLiveInfo); + mLoading.setVisibility(VISIBLE); + // 反注册语音交互 + V2XVoiceManager.INSTANCE + .unRegisterWakeCmd(V2XVoiceConstants.COMMAND_ZHIDAO_V2X_REFRESH_CAR_LIVE) + .unRegisterUnWakeVoice(V2XVoiceConstants.COMMAND_ZHIDAO_V2X_REFRESH_LIVE_UN_WAKEUP); + super.onDetachedFromWindow(); + } + + private void refreshStatusToListener(boolean videoPlaying) { + if (onVideoStatusChange != null) { + onVideoStatusChange.videoPlaying(videoPlaying); + } + } + + private V2XCarLiveVideoView.OnVideoStatusChange onVideoStatusChange; + + public void addOnVideoStatusChangeListener(V2XCarLiveVideoView.OnVideoStatusChange onVideoStatusChange) { + this.onVideoStatusChange = onVideoStatusChange; + } + + public interface OnVideoStatusChange { + void videoPlaying(boolean videoPlaying); + } +} diff --git a/modules/mogo-module-v2x/src/main/res/layout/view_video_layout_see_live.xml b/modules/mogo-module-v2x/src/main/res/layout/view_video_layout_see_live.xml index 0ad6257f83..303b339fe0 100644 --- a/modules/mogo-module-v2x/src/main/res/layout/view_video_layout_see_live.xml +++ b/modules/mogo-module-v2x/src/main/res/layout/view_video_layout_see_live.xml @@ -7,8 +7,14 @@ android:background="@color/live_video_background_color" app:roundLayoutRadius="@dimen/dp_20"> - + + + + + +