99 lines
3.2 KiB
Java
99 lines
3.2 KiB
Java
package com.mogo.cloud;
|
||
|
||
import android.os.Bundle;
|
||
import android.util.Log;
|
||
import android.view.SurfaceView;
|
||
import android.widget.EditText;
|
||
import android.widget.Toast;
|
||
import android.widget.ToggleButton;
|
||
|
||
import androidx.appcompat.app.AppCompatActivity;
|
||
|
||
import com.mogo.cloud.live.listener.ILiveProgressListener;
|
||
import com.mogo.cloud.live.manager.MoGoLiveManager;
|
||
import com.mogo.cloud.util.Devices;
|
||
|
||
|
||
public class LivePlayActivity extends AppCompatActivity {
|
||
private String TAG = "LiveActivity";
|
||
|
||
private SurfaceView surfaceView;
|
||
private ToggleButton liveToggleBtn;
|
||
private EditText etLookRoomId;
|
||
|
||
private boolean isLoginSuccess = false;
|
||
private String mStreamId = "STREAM_ID_";
|
||
|
||
@Override
|
||
protected void onCreate(Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
setContentView(R.layout.activity_live_play);
|
||
surfaceView = findViewById(R.id.surfaceView);
|
||
etLookRoomId = findViewById(R.id.etLookRoomId);
|
||
liveToggleBtn = findViewById(R.id.liveToggleBtn);
|
||
liveToggleBtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||
Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
|
||
if (isChecked) {
|
||
String roomId = etLookRoomId.getText().toString().trim();
|
||
MoGoLiveManager.getInstance().init(this.getApplication(), null);
|
||
MoGoLiveManager.getInstance().loginRoom(Devices.getSn(), roomId);
|
||
MoGoLiveManager.getInstance().setLiveProgressListener(listener);
|
||
} else {
|
||
MoGoLiveManager.getInstance().stopLive();
|
||
}
|
||
});
|
||
}
|
||
|
||
private ILiveProgressListener listener = new ILiveProgressListener() {
|
||
|
||
@Override
|
||
public void onConnecting() {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onConnected(String roomId) {
|
||
Log.i(TAG, "onConnected:" + roomId);
|
||
isLoginSuccess = true;
|
||
toggleLive(true);
|
||
}
|
||
|
||
@Override
|
||
public void onDisConnect() {
|
||
Log.i(TAG, "onDisConnect:");
|
||
isLoginSuccess = false;
|
||
toggleLive(false);
|
||
}
|
||
|
||
@Override
|
||
public void onDebugError(int errorCode, String funcName, String errorInfo) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onRoomStreamUpdate(String streamId, boolean isLive) {
|
||
Log.i(TAG, "onRoomStreamUpdate:" + streamId);
|
||
if (streamId != null && isLive) {
|
||
Toast.makeText(LivePlayActivity.this, "主播开始直播了", Toast.LENGTH_SHORT).show();
|
||
mStreamId = streamId;
|
||
} else {
|
||
Toast.makeText(LivePlayActivity.this, "主播已离线", Toast.LENGTH_SHORT).show();
|
||
}
|
||
}
|
||
};
|
||
|
||
private void toggleLive(boolean isChecked) {
|
||
if (isChecked) {
|
||
mStreamId = MoGoLiveManager.getInstance().startLive(surfaceView);
|
||
} else {
|
||
MoGoLiveManager.getInstance().stopLive();
|
||
}
|
||
Log.i(TAG, "toggleLive status : " + isChecked + " , mStreamId : " + mStreamId);
|
||
}
|
||
|
||
@Override
|
||
protected void onDestroy() {
|
||
super.onDestroy();
|
||
MoGoLiveManager.getInstance().onDestroyLive();
|
||
}
|
||
} |