|
|
|
@ -1,10 +1,7 @@
|
|
|
|
|
package com.xypower.mplive;
|
|
|
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
|
import android.content.pm.ActivityInfo;
|
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
@ -74,9 +71,6 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
|
|
|
|
|
private Handler mHandler;
|
|
|
|
|
private int cameraId = 0;
|
|
|
|
|
private List<CameraItemData> cameraData;
|
|
|
|
|
private int autoStart;
|
|
|
|
|
private EditText efu;
|
|
|
|
|
private int rotation;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
@ -86,17 +80,14 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
|
|
|
|
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
|
|
|
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
|
|
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
// 关键代码:声明为前台可见(Android 10+)
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
|
|
|
setTurnScreenOn(true); // 唤醒屏幕(即使无物理屏)
|
|
|
|
|
setShowWhenLocked(true); // 允许锁屏显示
|
|
|
|
|
}
|
|
|
|
|
// response screen rotation event
|
|
|
|
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
|
|
|
|
|
registerReceiver(updateReceiver, new IntentFilter("ACTION_UPDATE_UI"));
|
|
|
|
|
// 启动前台服务
|
|
|
|
|
Intent serviceIntent = new Intent(this, CameraForegroundService.class);
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
startForegroundService(serviceIntent);
|
|
|
|
|
} else {
|
|
|
|
|
startService(serviceIntent);
|
|
|
|
|
}
|
|
|
|
|
requestPermission();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void requestPermission() {
|
|
|
|
@ -138,27 +129,18 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private BroadcastReceiver updateReceiver = new BroadcastReceiver() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
String action = intent.getAction();
|
|
|
|
|
if ("ACTION_UPDATE_UI".equals(action)) {
|
|
|
|
|
String data = intent.getStringExtra("key");
|
|
|
|
|
extracted(autoStart, efu, rotation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private void init() {
|
|
|
|
|
|
|
|
|
|
Intent intent = getIntent();
|
|
|
|
|
autoStart = intent.getIntExtra("autoStart", 1);
|
|
|
|
|
final int autoStart = intent.getIntExtra("autoStart", 1);
|
|
|
|
|
|
|
|
|
|
// restore data.
|
|
|
|
|
sp = getSharedPreferences("MpLive", MODE_PRIVATE);
|
|
|
|
|
// rtmpUrl = sp.getString("rtmpUrl", rtmpUrl);
|
|
|
|
|
|
|
|
|
|
// initialize url.
|
|
|
|
|
efu = (EditText) findViewById(R.id.url);
|
|
|
|
|
final EditText efu = (EditText) findViewById(R.id.url);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
btnPublish = (Button) findViewById(R.id.publish);
|
|
|
|
@ -178,7 +160,7 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
|
|
|
|
|
}
|
|
|
|
|
efu.setText(rtmpUrl);
|
|
|
|
|
mCameraView = (SrsCameraView) findViewById(R.id.glsurfaceview_camera);
|
|
|
|
|
rotation = intent.getIntExtra("rotation", -1);
|
|
|
|
|
final int rotation = intent.getIntExtra("rotation", -1);
|
|
|
|
|
if (rotation != -1) {
|
|
|
|
|
//设置图像显示方向
|
|
|
|
|
mCameraView.setPreviewOrientation(rotation);
|
|
|
|
@ -211,7 +193,30 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (autoStart != 0) {
|
|
|
|
|
// extracted(autoStart, efu, rotation);
|
|
|
|
|
|
|
|
|
|
startRTMPServer();
|
|
|
|
|
mHandler.postDelayed(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
// rtmpUrl = "rtmp://61.169.135.146/live/0";
|
|
|
|
|
SharedPreferences.Editor editor = sp.edit();
|
|
|
|
|
editor.putString("rtmpUrl", rtmpUrl);
|
|
|
|
|
editor.apply();
|
|
|
|
|
efu.setText(rtmpUrl + "rotation= " + rotation + " cameraid=" + cameraId + " auto=" + autoStart);
|
|
|
|
|
// efu.setText(rtmpUrl + " 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);
|
|
|
|
|
}
|
|
|
|
@ -242,7 +247,7 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
|
|
|
|
|
efu.setText(rtmpUrl);
|
|
|
|
|
mPublisher.startPublish(rtmpUrl);
|
|
|
|
|
// mPublisher.startCamera();
|
|
|
|
|
mPublisher.switchCameraFace(cameraId, rotation);
|
|
|
|
|
mPublisher.switchCameraFace(cameraId,rotation);
|
|
|
|
|
|
|
|
|
|
if (btnSwitchEncoder.getText().toString().contentEquals("soft encoder")) {
|
|
|
|
|
Toast.makeText(getApplicationContext(), "Use hard encoder", Toast.LENGTH_SHORT).show();
|
|
|
|
@ -281,7 +286,7 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
|
|
|
|
|
int size = cameraData.size();
|
|
|
|
|
if (size > 0) {
|
|
|
|
|
int i = (++cameraId) % size;
|
|
|
|
|
mPublisher.switchCameraFace(cameraId, rotation);
|
|
|
|
|
mPublisher.switchCameraFace(cameraId,rotation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
@ -317,32 +322,6 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void extracted(final int autoStart, final EditText efu, final int rotation) {
|
|
|
|
|
startRTMPServer();
|
|
|
|
|
mPublisher.switchCameraFace(cameraId, rotation);
|
|
|
|
|
mHandler.postDelayed(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
// rtmpUrl = "rtmp://61.169.135.146/live/0";
|
|
|
|
|
SharedPreferences.Editor editor = sp.edit();
|
|
|
|
|
editor.putString("rtmpUrl", rtmpUrl);
|
|
|
|
|
editor.apply();
|
|
|
|
|
efu.setText(rtmpUrl + "rotation= " + rotation + " cameraid=" + cameraId + " auto=" + autoStart);
|
|
|
|
|
// efu.setText(rtmpUrl + " 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);
|
|
|
|
|
}
|
|
|
|
|
}, 500);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
|
|
@ -424,15 +403,11 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onStop() {
|
|
|
|
|
super.onStop();
|
|
|
|
|
unregisterReceiver(updateReceiver);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onResume() {
|
|
|
|
|
super.onResume();
|
|
|
|
|
Log.d("com.xypower.mplive", "进入resume");
|
|
|
|
|
requestPermission();
|
|
|
|
|
final Button btn = (Button) findViewById(R.id.publish);
|
|
|
|
|
btn.setEnabled(true);
|
|
|
|
|
mPublisher.resumeRecord();
|
|
|
|
|