代码完善

nx_mplive
liuguijing 2 weeks ago
parent 7c2b5e0911
commit f7da5c3f6f

@ -2,7 +2,6 @@ package com.xypower.mplive;
import android.Manifest; import android.Manifest;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Configuration; import android.content.res.Configuration;
@ -48,14 +47,12 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
private static final String TAG = "MpLive"; private static final String TAG = "MpLive";
public final static int RC_CAMERA = 100; public final static int RC_CAMERA = 100;
private Button btnPublish; private Button btnPublish;
private Button btnSwitchCamera; private Button btnSwitchCamera;
private Button btnRecord; private Button btnRecord;
private Button btnSwitchEncoder; private Button btnSwitchEncoder;
private Button btnPause; private Button btnPause;
private SharedPreferences sp;
private String rtmpUrl = "rtmp://127.0.0.1/live/0"; private String rtmpUrl = "rtmp://127.0.0.1/live/0";
private String recPath = Environment.getExternalStorageDirectory().getPath() + "/test.mp4"; private String recPath = Environment.getExternalStorageDirectory().getPath() + "/test.mp4";
@ -69,6 +66,15 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
private Handler mHandler; private Handler mHandler;
private int cameraId = 0; private int cameraId = 0;
private List<CameraItemData> cameraData; private List<CameraItemData> cameraData;
private int rotation;
private int autoClose;
private int autoStart;
private int netCamera;
private EditText efu;
public static final String CUSTOM_ACTION = "com.xypower.mpapp.ACT_TP_M";
private int channel;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -85,6 +91,7 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
} }
// response screen rotation event // response screen rotation event
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
requestPermission();
} }
@ -108,6 +115,12 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
} }
} }
private void init() {
initView();
initIntent();
initEvent();
}
//3. 接收申请成功或者失败回调 //3. 接收申请成功或者失败回调
@Override @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
@ -128,20 +141,9 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
} }
} }
private void init() { private void initIntent() {
Intent intent = getIntent(); Intent intent = getIntent();
final int autoStart = intent.getIntExtra("autoStart", 1);// 1--代表只开启rtmpserver 2--代表开启rtmpserver同时打开摄像头推流 其他状态表示只开启摄像头 autoStart = intent.getIntExtra("autoStart", 1);
// restore data.
sp = getSharedPreferences("MpLive", MODE_PRIVATE);
// initialize url.
final EditText efu = (EditText) findViewById(R.id.url);
btnPublish = (Button) findViewById(R.id.publish);
btnSwitchCamera = (Button) findViewById(R.id.swCam);
btnRecord = (Button) findViewById(R.id.record);
btnSwitchEncoder = (Button) findViewById(R.id.swEnc);
btnPause = (Button) findViewById(R.id.pause);
btnPause.setEnabled(false);
String url = intent.getStringExtra("url"); String url = intent.getStringExtra("url");
if (!TextUtils.isEmpty(url)) { if (!TextUtils.isEmpty(url)) {
rtmpUrl = url; rtmpUrl = url;
@ -149,9 +151,30 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
if (TextUtils.isEmpty(rtmpUrl)) { if (TextUtils.isEmpty(rtmpUrl)) {
rtmpUrl = "rtmp://127.0.0.1/live/0"; rtmpUrl = "rtmp://127.0.0.1/live/0";
} }
rotation = intent.getIntExtra("rotation", -1);
cameraId = intent.getIntExtra("cameraId", 0);
autoClose = intent.getIntExtra("autoClose", 0);
netCamera = intent.getIntExtra("netCamera", 0);
channel = intent.getIntExtra("channel", 0);
}
private void initView() {
btnPublish = (Button) findViewById(R.id.publish);
btnSwitchCamera = (Button) findViewById(R.id.swCam);
btnRecord = (Button) findViewById(R.id.record);
btnSwitchEncoder = (Button) findViewById(R.id.swEnc);
btnPause = (Button) findViewById(R.id.pause);
btnPause.setEnabled(false);
efu = (EditText) findViewById(R.id.url);
efu.setText(rtmpUrl); efu.setText(rtmpUrl);
mCameraView = (SrsCameraView) findViewById(R.id.glsurfaceview_camera); mCameraView = (SrsCameraView) findViewById(R.id.glsurfaceview_camera);
final int rotation = intent.getIntExtra("rotation", -1);
if (rotation != -1) { if (rotation != -1) {
//设置图像显示方向 //设置图像显示方向
mCameraView.setPreviewOrientation(rotation); mCameraView.setPreviewOrientation(rotation);
@ -171,8 +194,6 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
mPublisher.setVideoHDMode(); mPublisher.setVideoHDMode();
cameraId = intent.getIntExtra("cameraId", 0);
mCameraView.setCameraCallbacksHandler(new SrsCameraView.CameraCallbacksHandler() { mCameraView.setCameraCallbacksHandler(new SrsCameraView.CameraCallbacksHandler() {
@Override @Override
public void onCameraParameters(Camera.Parameters params) { public void onCameraParameters(Camera.Parameters params) {
@ -180,46 +201,6 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
} }
}); });
if (autoStart == 1) {
startRTMPServer();
} else if (autoStart == 2) {
startRTMPServer();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
SharedPreferences.Editor editor = sp.edit();
editor.putString("rtmpUrl", rtmpUrl);
editor.apply();
efu.setText(rtmpUrl + "rotation= " + rotation + " cameraid=" + cameraId + " auto=" + autoStart);
efu.setText(rtmpUrl);
mPublisher.startPublish(rtmpUrl);
if (btnSwitchEncoder.getText().toString().contentEquals("soft encoder")) {
// Toast.makeText(getApplicationContext(), "Use hard encoder", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Use soft encoder", Toast.LENGTH_SHORT).show();
}
btnPublish.setText("stop");
btnSwitchEncoder.setEnabled(false);
btnPause.setEnabled(true);
mPublisher.switchCameraFace(cameraId, rotation);
}
}, 500);
} else {
mPublisher.switchCameraFace(cameraId, rotation);
}
int autoClose = intent.getIntExtra("autoClose", 0);
if (autoClose != 0) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Log.w(TAG, "Close self automatically");
finish();
System.exit(0);
}
}, 100000);
}
btnPublish.setOnClickListener(new View.OnClickListener() { btnPublish.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@ -228,9 +209,6 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
Toast.makeText(getApplicationContext(), "没有正确的推送地址", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), "没有正确的推送地址", Toast.LENGTH_SHORT).show();
return; return;
} }
SharedPreferences.Editor editor = sp.edit();
editor.putString("rtmpUrl", rtmpUrl);
editor.apply();
efu.setText(rtmpUrl); efu.setText(rtmpUrl);
mPublisher.startPublish(rtmpUrl); mPublisher.startPublish(rtmpUrl);
// mPublisher.startCamera(); // mPublisher.startCamera();
@ -309,6 +287,64 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
}); });
} }
private void initEvent() {
if (autoStart == 1) {
startRTMPServer();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// rtmpUrl = "rtsp://127.0.0.1:8554/live/7";
efu.setText(rtmpUrl + "rotation= " + rotation + " cameraid=" + cameraId + " auto=" + autoStart);
if (netCamera == 0) {
mPublisher.startPublish(rtmpUrl);
if (btnSwitchEncoder.getText().toString().contentEquals("soft encoder")) {
// Toast.makeText(getApplicationContext(), "Use hard encoder", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Use soft encoder", Toast.LENGTH_SHORT).show();
}
btnPublish.setText("stop");
btnSwitchEncoder.setEnabled(false);
btnPause.setEnabled(true);
mPublisher.switchCameraFace(cameraId, rotation);
} else {
sendStartPushBroadcast();
}
}
}, 500);
} else {
mPublisher.switchCameraFace(cameraId, rotation);
}
if (autoClose != 0) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Log.w(TAG, "Close self automatically");
finish();
System.exit(0);
}
}, 100000);
}
}
private void sendStartPushBroadcast() {
Log.d("开始发送广播", rtmpUrl);
Intent intent = new Intent(CUSTOM_ACTION);
intent.putExtra("Channel", channel);
intent.putExtra("MediaType", 16);
intent.putExtra("Url", rtmpUrl);
sendBroadcast(intent);
}
private void sendStopPushBroadcast() {
Intent intent = new Intent(CUSTOM_ACTION);
intent.putExtra("Channel", channel);
intent.putExtra("MediaType", 17);
intent.putExtra("Url", rtmpUrl);
sendBroadcast(intent);
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present. // Inflate the menu; this adds items to the action bar if it is present.
@ -393,10 +429,6 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
Log.d("com.xypower.mplive", "进入resume");
requestPermission();
final Button btn = (Button) findViewById(R.id.publish);
btn.setEnabled(true);
mPublisher.resumeRecord(); mPublisher.resumeRecord();
} }
@ -409,6 +441,7 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
sendStopPushBroadcast();
stopRTMPServer(); stopRTMPServer();
mPublisher.stopPublish(); mPublisher.stopPublish();
mPublisher.stopRecord(); mPublisher.stopRecord();
@ -428,7 +461,10 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
} }
private void startRTMPServer() { private void startRTMPServer() {
File streamingDir = new File(getDataDir(), "streaming"); File streamingDir = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
streamingDir = new File(getDataDir(), "streaming");
}
if (!streamingDir.exists()) { if (!streamingDir.exists()) {
streamingDir.mkdirs(); streamingDir.mkdirs();
} }

Loading…
Cancel
Save