@@ -1,272 +0,0 @@
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 < V2XLivePushVoRes > ( ) {
@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 < V2XLivePushVoRes > ( ) {
@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 ) ;
}
}