增加了状态回碉
增加了如果直播间没有观众的时候自动关闭直播
This commit is contained in:
@@ -33,7 +33,7 @@ public abstract class BaseLiveActivity extends AppCompatActivity {
|
||||
// 设置摄像头与状态
|
||||
protected ToggleButton btnChangeCameraState;
|
||||
// 当前直播的状态
|
||||
protected ToggleButton tbLiveStatus;
|
||||
protected TextView tvLiveStatus;
|
||||
// 相机数据预览
|
||||
protected SurfaceView surfacePreviewView;
|
||||
|
||||
@@ -77,7 +77,7 @@ public abstract class BaseLiveActivity extends AppCompatActivity {
|
||||
toggleCameraState(isChecked);
|
||||
});
|
||||
|
||||
tbLiveStatus = findViewById(R.id.tbLiveStatus);
|
||||
tvLiveStatus = findViewById(R.id.tvLiveStatus);
|
||||
|
||||
surfacePlayView = findViewById(R.id.surfacePlayView);
|
||||
etLookRoomId = findViewById(R.id.etLookRoomId);
|
||||
|
||||
@@ -38,15 +38,18 @@ public class LivePlayAndPushActivity extends BaseLiveActivity
|
||||
liveStreamManager.setLiveStatusChangeCallback(new ILiveStatusListener() {
|
||||
@Override
|
||||
public void onChange(String camId, int status) {
|
||||
tbLiveStatus.post(new Runnable() {
|
||||
tvLiveStatus.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (status == 0) {
|
||||
tbLiveStatus.setTextColor(getResources().getColor(R.color.colorStartLive));
|
||||
tbLiveStatus.setChecked(true);
|
||||
} else {
|
||||
tbLiveStatus.setTextColor(getResources().getColor(R.color.colorStopLive));
|
||||
tbLiveStatus.setChecked(false);
|
||||
tvLiveStatus.setTextColor(getResources().getColor(R.color.colorStartLive));
|
||||
tvLiveStatus.setText("正在直播中");
|
||||
} else if (status == 1) {
|
||||
tvLiveStatus.setTextColor(getResources().getColor(R.color.colorStopLive));
|
||||
tvLiveStatus.setText("直播已停止");
|
||||
} else if (status == 2) {
|
||||
tvLiveStatus.setTextColor(getResources().getColor(R.color.colorForcedStopLive));
|
||||
tvLiveStatus.setText("直播强制结束");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -28,15 +28,13 @@ public class LivePushActivity extends BaseLiveActivity {
|
||||
liveStreamManager.setLiveStatusChangeCallback(new ILiveStatusListener() {
|
||||
@Override
|
||||
public void onChange(String camId, int status) {
|
||||
tbLiveStatus.post(new Runnable() {
|
||||
tvLiveStatus.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (status == 0) {
|
||||
tbLiveStatus.setTextColor(getResources().getColor(R.color.colorStartLive));
|
||||
tbLiveStatus.setChecked(true);
|
||||
tvLiveStatus.setTextColor(getResources().getColor(R.color.colorStartLive));
|
||||
} else {
|
||||
tbLiveStatus.setTextColor(getResources().getColor(R.color.colorStopLive));
|
||||
tbLiveStatus.setChecked(false);
|
||||
tvLiveStatus.setTextColor(getResources().getColor(R.color.colorStopLive));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
android:text="本机预览"
|
||||
android:textColor="#FFEB3B"
|
||||
android:textSize="25dp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintEnd_toEndOf="@+id/surfacePreviewView"
|
||||
app:layout_constraintStart_toStartOf="@+id/surfacePreviewView"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
@@ -43,19 +44,21 @@
|
||||
android:text="观看指定车机"
|
||||
android:textColor="#FF5722"
|
||||
android:textSize="25dp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintEnd_toEndOf="@+id/surfacePlayView"
|
||||
app:layout_constraintStart_toStartOf="@+id/surfacePlayView"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
|
||||
<ToggleButton
|
||||
android:id="@+id/tbLiveStatus"
|
||||
<TextView
|
||||
android:id="@+id/tvLiveStatus"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="#FFFF"
|
||||
android:padding="10dp"
|
||||
android:textOff="直播已停止"
|
||||
android:textOn="直播进行中"
|
||||
android:text="直播已停止"
|
||||
android:textSize="18dp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
|
||||
@@ -5,4 +5,5 @@
|
||||
<color name="colorAccent">#03DAC5</color>
|
||||
<color name="colorStartLive">#4CAF50</color>
|
||||
<color name="colorStopLive">#F44336</color>
|
||||
<color name="colorForcedStopLive">#9C27B0</color>
|
||||
</resources>
|
||||
@@ -4,7 +4,6 @@ import android.app.Application;
|
||||
import android.media.AudioFormat;
|
||||
import android.os.Handler;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mogo.cloud.live.listener.ILiveStatusListener;
|
||||
import com.mogo.cloud.live.model.CommandModel;
|
||||
@@ -29,7 +28,7 @@ public class LiveStreamManagerImpl implements ILiveStreamManager {
|
||||
private final Application mApplication;
|
||||
|
||||
// 循环上报摄像头可直播状态间隔时间
|
||||
private static final int PUSH_CAM_TIME = 10 * 30 * 1000;
|
||||
private static final int PUSH_CAM_TIME = 30 * 1000;
|
||||
private static volatile int sCam1LiveStatus = 0, sCam2LiveStatus = 0;
|
||||
private static volatile int sCam1AvailableStatus = 0, sCam2AvailableStatus = 0;
|
||||
private static final int PUSH_START = 0; // 开始
|
||||
@@ -74,7 +73,7 @@ public class LiveStreamManagerImpl implements ILiveStreamManager {
|
||||
mLivePushConfig.setMute(true);
|
||||
mLivePushConfig.setDevicesId(devicesId);
|
||||
// 初始化直播
|
||||
mLivePusher = MoGoLiveManager.getInstance().init(mApplication, mLivePushConfig);
|
||||
mLivePusher = MoGoLiveManager.getInstance().init(mApplication, mLivePushConfig);
|
||||
|
||||
// 初始化Socket长连接通道
|
||||
mSocketMsgUtils = SocketMsgUtils.getInstance(mApplication,
|
||||
@@ -110,19 +109,19 @@ public class LiveStreamManagerImpl implements ILiveStreamManager {
|
||||
|
||||
@Override
|
||||
public void startLiveStream() {
|
||||
Logger.d(TAG, "startLiveStream");
|
||||
Logger.i(TAG, "startLiveStream");
|
||||
PushService.startService(mApplication, PushService.ACTION_START_RTMP_PUSH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopLiveStream() {
|
||||
Logger.d(TAG, "stopLiveStream");
|
||||
Logger.i(TAG, "stopLiveStream");
|
||||
PushService.startService(mApplication, PushService.ACTION_STOP_RTMP_PUSH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void uploadCamStatus(int frontStatus, int backStatus) {
|
||||
Logger.d(TAG, "uploadCamStatus frontStatus is:" + frontStatus + " backStatus is:" + backStatus);
|
||||
Logger.i(TAG, "uploadCamStatus frontStatus is:" + frontStatus + " backStatus is:" + backStatus);
|
||||
sCam1AvailableStatus = frontStatus;
|
||||
sCam2AvailableStatus = backStatus;
|
||||
MsgBody msgBody = new MsgBody();
|
||||
@@ -163,7 +162,7 @@ public class LiveStreamManagerImpl implements ILiveStreamManager {
|
||||
* 开启摄像头状态循环
|
||||
*/
|
||||
private void restartCamStatusLoop() {
|
||||
Log.i(TAG, "开启摄像头状态循环");
|
||||
Logger.i(TAG, "摄像头状态循环");
|
||||
if (mHandler != null) {
|
||||
mHandler.removeCallbacks(mCamStatusRun);
|
||||
mHandler.postDelayed(mCamStatusRun, PUSH_CAM_TIME);
|
||||
@@ -174,9 +173,13 @@ public class LiveStreamManagerImpl implements ILiveStreamManager {
|
||||
* 循环同步摄像头状态
|
||||
*/
|
||||
private Runnable mCamStatusRun = () -> {
|
||||
Log.i(TAG, "循环同步摄像头状态");
|
||||
Logger.i(TAG, "循环同步摄像头状态");
|
||||
uploadCamStatus(sCam1AvailableStatus, sCam2AvailableStatus);
|
||||
restartCamStatusLoop();
|
||||
// 判断当前观众的个数,如果没人观看了的情况则停止直播
|
||||
if (mLivePusher.getOnlineNumber() <= 1) {
|
||||
livePushHandler(PUSH_STOP, C1);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -207,9 +210,8 @@ public class LiveStreamManagerImpl implements ILiveStreamManager {
|
||||
// 向外同步状态
|
||||
liveStatusCallbackOnChange(C1, sCam1LiveStatus);
|
||||
} else {
|
||||
Log.d(TAG, "目前只支持前置行车记录仪");
|
||||
Logger.e(TAG, "目前只支持前置行车记录仪");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -198,7 +198,7 @@ public class MoGoLiveManager {
|
||||
*/
|
||||
private final IZegoEventHandler mEventHandler = new IZegoEventHandler() {
|
||||
|
||||
// 调试错误信息回调
|
||||
// 调试错误信息回调
|
||||
@Override
|
||||
public void onDebugError(int errorCode, String funcName, String info) {
|
||||
super.onDebugError(errorCode, funcName, info);
|
||||
@@ -236,6 +236,7 @@ public class MoGoLiveManager {
|
||||
} else if (state == ZegoRoomState.CONNECTED) {
|
||||
mProgressListener.onConnected(roomID);
|
||||
} else {
|
||||
onlineNumber = 0;
|
||||
mProgressListener.onDisConnect();
|
||||
}
|
||||
}
|
||||
@@ -245,8 +246,8 @@ public class MoGoLiveManager {
|
||||
@Override
|
||||
public void onRoomOnlineUserCountUpdate(String roomID, int count) {
|
||||
super.onRoomOnlineUserCountUpdate(roomID, count);
|
||||
Logger.i(TAG, "onRoomOnlineUserCountUpdate roomID : " + roomID + " , count : " + count);
|
||||
onlineNumber = count;
|
||||
Logger.i(TAG, "onRoomOnlineUserCountUpdate roomID : " + roomID + " , online user number : " + onlineNumber);
|
||||
if (mRoomStatusListener != null) {
|
||||
mRoomStatusListener.onRoomOnlineUserCountUpdate(count);
|
||||
}
|
||||
@@ -256,8 +257,8 @@ public class MoGoLiveManager {
|
||||
@Override
|
||||
public void onRoomUserUpdate(String roomID, ZegoUpdateType updateType, ArrayList<ZegoUser> userList) {
|
||||
super.onRoomUserUpdate(roomID, updateType, userList);
|
||||
Logger.i(TAG, "onRoomUserUpdate roomId : " + roomID + " , updateType : " + updateType.name());
|
||||
onlineNumber = userList.size();
|
||||
Logger.i(TAG, "onRoomUserUpdate roomId : " + roomID + " , updateType : " + updateType.name() + " , online user number : " + onlineNumber);
|
||||
if (mRoomStatusListener != null) {
|
||||
mRoomStatusListener.onRoomUserUpdate(updateType, userList);
|
||||
}
|
||||
@@ -312,7 +313,7 @@ public class MoGoLiveManager {
|
||||
*/
|
||||
private void destroyEngine() {
|
||||
ZegoExpressEngine.destroyEngine(() -> {
|
||||
Logger.d(TAG, "销毁ZeGo引擎");
|
||||
Logger.i(TAG, "销毁ZeGo引擎");
|
||||
});
|
||||
}
|
||||
|
||||
@@ -465,7 +466,7 @@ public class MoGoLiveManager {
|
||||
!multiStreamId.equals(STREAM_ID_PREFIX)) {
|
||||
ZegoCanvas zegoCanvas = new ZegoCanvas(surfaceView);
|
||||
zegoCanvas.viewMode = ZegoViewMode.SCALE_TO_FILL;
|
||||
Logger.d(TAG, "startLive multiStreamId: " + multiStreamId);
|
||||
Logger.i(TAG, "startLive multiStreamId: " + multiStreamId);
|
||||
mExpressEngine.startPlayingStream(multiStreamId, zegoCanvas);
|
||||
} else {
|
||||
Logger.e(TAG, "直播ID有误,请重试");
|
||||
@@ -477,7 +478,7 @@ public class MoGoLiveManager {
|
||||
* 停止播放直播
|
||||
*/
|
||||
public void stopLive() {
|
||||
Logger.d(TAG, "stopLive multiStreamId: " + multiStreamId);
|
||||
Logger.i(TAG, "stopLive multiStreamId: " + multiStreamId);
|
||||
if (!TextUtils.isEmpty(multiStreamId) && mExpressEngine != null) {
|
||||
mExpressEngine.stopPlayingStream(multiStreamId);
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ public class PushService extends Service
|
||||
if (intent != null) {
|
||||
// 开启直播
|
||||
if (ACTION_START_RTMP_PUSH.equals(intent.getAction())) {
|
||||
Logger.i(TAG, "开启直播推送");
|
||||
try {
|
||||
startPush();
|
||||
} catch (Exception e) {
|
||||
@@ -56,6 +57,7 @@ public class PushService extends Service
|
||||
}
|
||||
// 关闭直播,如果还有人观看不关闭
|
||||
else if (ACTION_STOP_RTMP_PUSH.equals(intent.getAction())) {
|
||||
Logger.i(TAG, "关闭直播推送");
|
||||
if (mLivePusher.getOnlineNumber() <= 1) {
|
||||
stopPush();
|
||||
} else {
|
||||
@@ -64,6 +66,7 @@ public class PushService extends Service
|
||||
}
|
||||
// 强制关闭直播
|
||||
else if (ACTION_FORCED_STOP_RTMP_PUSH.equals(intent.getAction())) {
|
||||
Logger.i(TAG, "强制关闭直播推送");
|
||||
stopPush();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user