完成了直播播放的拉流demo
This commit is contained in:
101
app/src/main/java/com/mogo/cloud/LivePlayActivity.java
Normal file
101
app/src/main/java/com/mogo/cloud/LivePlayActivity.java
Normal file
@@ -0,0 +1,101 @@
|
||||
package com.mogo.cloud;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
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.ZeGoLiveManager;
|
||||
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();
|
||||
mStreamId = ZeGoLiveManager.STREAM_ID_PREFIX + roomId;
|
||||
ZeGoLiveManager.getInstance().init(this.getApplication(), null);
|
||||
ZeGoLiveManager.getInstance().loginRoom("F803EB2046PZD00140", roomId);
|
||||
ZeGoLiveManager.getInstance().setLiveProgressListener(listener);
|
||||
} else {
|
||||
ZeGoLiveManager.getInstance().stopLive(mStreamId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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) {
|
||||
Log.i(TAG, "toggleLive status : " + isChecked + " , mStreamId : " + mStreamId);
|
||||
if (isChecked) {
|
||||
ZeGoLiveManager.getInstance().startLive(mStreamId, surfaceView);
|
||||
} else {
|
||||
ZeGoLiveManager.getInstance().stopLive(mStreamId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
ZeGoLiveManager.getInstance().onDestroyLive();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user