完成了直播推流demo

This commit is contained in:
董宏宇
2021-02-03 17:15:09 +08:00
parent cb4b3e4bb5
commit 533f4df3b9
3 changed files with 9 additions and 88 deletions

View File

@@ -139,10 +139,4 @@ public abstract class BaseLiveActivity extends AppCompatActivity {
*/
public abstract void toggleLive(boolean isLive);
/**
* 是否是静音模式
*
* @param isMute true-静音false-非静音
*/
public abstract void toggleMute(boolean isMute);
}

View File

@@ -1,14 +1,10 @@
package com.mogo.cloud;
import android.media.AudioFormat;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import com.mogo.cloud.live.listener.ILiveProgressListener;
import com.mogo.cloud.live.manager.MGLivePushConfig;
import com.mogo.cloud.live.manager.ZeGoLiveManager;
import com.mogo.cloud.live.utils.ByteUtils;
import com.mogo.cloud.live.manager.CameraFrameManager;
import com.mogo.cloud.live.server.PushService;
import com.mogo.cloud.util.Devices;
@@ -18,103 +14,37 @@ import com.mogo.cloud.util.Devices;
public class PushActivity extends BaseLiveActivity {
public static final String TAG = "PushActivity";
private boolean isLoginSuccess = false;
private boolean isLive = false;
private boolean isOnStart = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MGLivePushConfig mLivePushConfig = new MGLivePushConfig();
mLivePushConfig.setWidth(1280);
mLivePushConfig.setHeight(720);
mLivePushConfig.setVideoBitrate(1500);
mLivePushConfig.setVideoFPS(15);
mLivePushConfig.setAudioChannels(2);
mLivePushConfig.setAudioSampleRate(44100);
mLivePushConfig.setAudioFormat(AudioFormat.ENCODING_PCM_16BIT);
mLivePushConfig.setMute(true);
ZeGoLiveManager.getInstance().init(this.getApplication(), mLivePushConfig);
ZeGoLiveManager.getInstance().setLiveProgressListener(new ILiveProgressListener() {
@Override
public void onStart() {
Log.i(TAG, "onStart");
isOnStart = true;
}
@Override
public void onStop() {
Log.i(TAG, "onStop");
isOnStart = false;
}
@Override
public void onConnecting() {
Log.i(TAG, "onConnecting");
}
@Override
public void onConnected(String roomId) {
Log.i(TAG, "onConnected");
isLoginSuccess = true;
}
@Override
public void onDisConnect() {
Log.i(TAG, "onDisConnect");
isLoginSuccess = false;
}
@Override
public void onDebugError(int errorCode, String funcName, String errorInfo) {
Log.i(TAG, "errorCode : " + errorCode + " , funcName : " + funcName + " , errorInfo : " + errorInfo);
}
});
}
@Override
public void onVideoFrame(byte[] bytes, int bytesLength) {
if (!isLoginSuccess) {
// Log.i(TAG, "还未进房成功");
return;
}
if (!isOnStart) {
// Log.i(TAG, "还未执行onStart回调");
return;
}
if (!isLive) {
return;
}
Log.i(TAG, "onVideoFrame byte length: " + bytesLength);
ZeGoLiveManager.getInstance().startPublishingStream(ByteUtils.getBuffer(bytes, bytesLength), bytesLength, SystemClock.elapsedRealtime());
//Log.i(TAG, "onVideoFrame byte length: " + bytesLength);
CameraFrameManager.getInstance().notifyYUVData(bytes, 1280, 720, 3);
}
@Override
public void toggleLive(boolean isLive) {
this.isLive = isLive;
if (!isLoginSuccess) {
Log.i(TAG, "toggleLive isLive : " + isLive);
btnLive.setChecked(!isLive);
return;
}
Log.i(TAG, "toggleLive : " + isLive);
this.isLive = isLive;
if (isLive) {
ZeGoLiveManager.getInstance().startPush(Devices.getSn());
PushService.startService(this, PushService.ACTION_START_RTMP_PUSH, Devices.getSn());
} else {
ZeGoLiveManager.getInstance().stopPublish();
PushService.startService(this, PushService.ACTION_STOP_RTMP_PUSH, null);
}
}
@Override
public void toggleMute(boolean isMute) {
}
@Override
protected void onDestroy() {
super.onDestroy();
ZeGoLiveManager.getInstance().onDestroyPublish();
PushService.startService(this, PushService.ACTION_STOP_RTMP_PUSH, null);
}
}