|
|
|
@ -1,11 +1,16 @@
|
|
|
|
|
package net.ossrs.yasea.demo;
|
|
|
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
|
import android.content.pm.ActivityInfo;
|
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
import android.content.res.Configuration;
|
|
|
|
|
import android.hardware.Camera;
|
|
|
|
|
import android.os.Build;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.os.Environment;
|
|
|
|
|
import android.support.v4.app.ActivityCompat;
|
|
|
|
|
import android.support.v4.content.ContextCompat;
|
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.Menu;
|
|
|
|
@ -32,6 +37,7 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
|
|
|
|
|
SrsRecordHandler.SrsRecordListener, SrsEncodeHandler.SrsEncodeListener {
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "Yasea";
|
|
|
|
|
public final static int RC_CAMERA = 100;
|
|
|
|
|
|
|
|
|
|
private Button btnPublish;
|
|
|
|
|
private Button btnSwitchCamera;
|
|
|
|
@ -48,6 +54,7 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
|
|
|
|
|
|
|
|
|
|
private int mWidth = 640;
|
|
|
|
|
private int mHeight = 480;
|
|
|
|
|
private boolean isPermissionGranted = false;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
@ -59,6 +66,41 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
|
|
|
|
|
// response screen rotation event
|
|
|
|
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
|
|
|
|
|
|
|
|
|
|
requestPermission();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void requestPermission() {
|
|
|
|
|
//1. 检查是否已经有该权限
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 23 && (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
|
|
|
|
|
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
|
|
|
|
|
!= PackageManager.PERMISSION_GRANTED)) {
|
|
|
|
|
//2. 权限没有开启,请求权限
|
|
|
|
|
ActivityCompat.requestPermissions(this,
|
|
|
|
|
new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, RC_CAMERA);
|
|
|
|
|
}else{
|
|
|
|
|
//权限已经开启,做相应事情
|
|
|
|
|
isPermissionGranted = true;
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//3. 接收申请成功或者失败回调
|
|
|
|
|
@Override
|
|
|
|
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
|
if (requestCode == RC_CAMERA) {
|
|
|
|
|
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
//权限被用户同意,做相应的事情
|
|
|
|
|
isPermissionGranted = true;
|
|
|
|
|
init();
|
|
|
|
|
} else {
|
|
|
|
|
//权限被用户拒绝,做相应的事情
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void init() {
|
|
|
|
|
// restore data.
|
|
|
|
|
sp = getSharedPreferences("Yasea", MODE_PRIVATE);
|
|
|
|
|
rtmpUrl = sp.getString("rtmpUrl", rtmpUrl);
|
|
|
|
@ -249,7 +291,7 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
|
|
|
|
|
@Override
|
|
|
|
|
protected void onStart() {
|
|
|
|
|
super.onStart();
|
|
|
|
|
if(mPublisher.getCamera() == null){
|
|
|
|
|
if(mPublisher.getCamera() == null && isPermissionGranted){
|
|
|
|
|
//if the camera was busy and available again
|
|
|
|
|
mPublisher.startCamera();
|
|
|
|
|
}
|
|
|
|
@ -447,4 +489,5 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
|
|
|
|
|
public void onEncodeIllegalArgumentException(IllegalArgumentException e) {
|
|
|
|
|
handleException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|