1.引用net.ossrs.yasea包
2.activity
public class PreviewActivity extends Activity implements RtmpHandler.RtmpListener, SrsRecordHandler.SrsRecordListener, SrsEncodeHandler.SrsEncodeListener { @BindView(R.id.iv_guanbi1) ImageView ivGuanbi; @BindView(R.id.iv_zhaopian) ImageView ivZhaopian; @BindView(R.id.tv_zhibobiaoti) EditText tvZhibobiaoti; @BindView(R.id.act_login_btn_login) Button actLoginBtnLogin; @BindView(R.id.tv_daojishi) TextView tvDaojishi; @BindView(R.id.rl_zhuanbei) RelativeLayout rlzhunbei; @BindView(R.id.kaishi_zhibo) RelativeLayout kaishizhibo; @BindView(R.id.civ_touxiang) CircleImageView civTouxiang; @BindView(R.id.tv_name) TextView tvName; @BindView(R.id.tv_guankan_renshu) TextView tvGuankanRenshu; @BindView(R.id.tv_guanzhu) TextView tvGuanzhu; @BindView(R.id.tv_zaixin_renshu) TextView tvZaixinRenshu; @BindView(R.id.zhibo_hlist_touxiang) HorizontalListView hlistTouxiang; @BindView(R.id.glsurfaceview_camera_1) SrsCameraView mCameraView; @BindView(R.id.pause) Button pause; @BindView(R.id.publish) Button publish; @BindView(R.id.record) Button record; @BindView(R.id.swEnc) Button swEnc; @BindView(R.id.url) EditText url; @BindView(R.id.swCam) ImageView swCam; @BindView(R.id.iv_guanbi_zhibo) ImageView ivGuanbiZhibo; @BindView(R.id.svgaImage) SVGAImageView svgaImage; private int time = 10; private Timer timer; private Timer timer1; private int time1=3; Unbinder unbinder1; View view; private boolean autoReconnect = true; private boolean error; private static String OOS_ACCESS_KEY; private static String OOS_ACCESS_ID; private static String OOS_ENDPOINT; private static String BUCKET_NAME; public static final int TAKE_PHOTO = 1; public static final int CHOOSE_PHOTO = 2; public static final int NAME = 3; private static final int REQUEST_CODE = 10; private String lujing; private static final String TAG = "Yasea"; public final static int RC_CAMERA = 100; private SharedPreferences sp; private SrsPublisher mPublisher; private String rtmpUrl; private String recPath = Environment.getExternalStorageDirectory().getPath() + "/test.mp4"; private int mWidth = 1280; private int mHeight = 720; private boolean isPermissionGranted = false; private String tupian, fangjianhao, fangjianmingchneg; private ArrayList<String> keylist = new ArrayList<String>(); private ArrayList<String> viuelist = new ArrayList<String>(); private static final int CAMERA_PERMISSION_REQUEST_CODE = 100; private PicAdapter adapter1; private List<GuanZhuRenshuModel.DataBean.OnLinePeopleListBean> guankanlist = new ArrayList<>(); private Button btn_baocun; private List<String> photoList; private String xingbie; private Uri imageUri; private String mytoken; private File finalFile; String imageStr = ""; private String userId; private Uri image; private String gkrs, gzrs, uuid; private String nicheng,type; private TextView mTvStart; private TextView mTvEnd; private TextView mTvTime; private int dianji; private int recLen = 3; private ServiceConnection mServiceConnection; private static final int EXTERNAL_STORAGE_REQ_WRITE_EXTERNAL_STORAGE_CODE = 101; private int yichang; private NetWorkStateReceiver netWorkStateReceiver; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 1: // tvDaojishi.setText(time + ""); time--; if (time <= 0) { guankanrenshu(); guanzhushu(); } break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setContentView(R.layout.activity_preview); ButterKnife.bind(this); SharedPreferences sPreferences = getSharedPreferences("Login", MODE_PRIVATE); mytoken = sPreferences.getString("token", ""); uuid = sPreferences.getString("userid", ""); type=sPreferences.getString("type",""); if (type.equals("0")){ Intent intent=new Intent(PreviewActivity.this,ShiMing1Activity.class); startActivity(intent); finish(); } setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); huoqufangjian2(); // requestPermission(); initListView(); sp = getSharedPreferences("Yasea", MODE_PRIVATE); rtmpUrl = sp.getString("rtmpUrl", rtmpUrl); // initialize url. // final EditText efu = (EditText) findViewById(R.id.url); // efu.setText(rtmpUrl); // initialize url. final EditText efu = (EditText) findViewById(R.id.url); // efu.setText(rtmpUrl); pause.setEnabled(false); mCameraView = (SrsCameraView) findViewById(R.id.glsurfaceview_camera_1); mPublisher = new SrsPublisher(mCameraView); 个人资料(); NetworkManager.getInstance().registerObserver(this); EventBus.getDefault().register(this); JPushInterface.setDebugMode(true);//正式版的时候设置false,关闭调试 JPushInterface.init(PreviewActivity.this); SVGAParser.Companion.shareParser().init(this); netWorkStateReceiver = new NetWorkStateReceiver() { @Override public void onReceive(Context context, Intent intent) { detectNetWork(); } }; } protected void detectNetWork() { if (!isConnected(this)) { } } 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 || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) { //2. 权限没有开启,请求权限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE}, RC_CAMERA); } else { //权限已经开启,做相应事情 isPermissionGranted = true; } } //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; } else { //权限被用户拒绝,做相应的事情 finish(); } } } private void init() { mPublisher.setEncodeHandler(new SrsEncodeHandler(this)); mPublisher.setRtmpHandler(new RtmpHandler(this)); mPublisher.setRecordHandler(new SrsRecordHandler(this)); mPublisher.setPreviewResolution(mWidth, mHeight); mPublisher.setOutputResolution(mHeight, mWidth); // 这里要和preview反过来 mPublisher.setVideoHDMode(); mPublisher.startCamera(); mCameraView.setCameraCallbacksHandler(new SrsCameraView.CameraCallbacksHandler() { @Override public void onCameraParameters(Camera.Parameters params) { //params.setFocusMode("custom-focus"); //params.setWhiteBalance("custom-balance"); //etc... } }); } @Override protected void onStart() { super.onStart(); requestPermission(); } @Override protected void onResume() { super.onResume(); final Button btn = (Button) findViewById(R.id.publish); btn.setEnabled(true); timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { Message msg = Message.obtain(); msg.what = 1; handler.sendMessage(msg); } }, 0, 10000); guankanrenshu(); guanzhushu(); mPublisher.resumeRecord(); registerReceiver(netWorkStateReceiver, new IntentFilter( ConnectivityManager.CONNECTIVITY_ACTION)); } @Override protected void onPause() { super.onPause(); mPublisher.pauseRecord(); try { unregisterReceiver(netWorkStateReceiver); } catch (Exception e) { } } @Override protected void onDestroy() { super.onDestroy(); if (dianji==1) { mPublisher.stopPublish(); mPublisher.stopRec