安卓直播推流

本文介绍了在安卓平台上进行直播推流的实现过程,包括引入SRS客户端库net.ossrs.yasea,并详细讲解了如何在Activity中配置和使用推流功能。
摘要由CSDN通过智能技术生成

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一: 使用javacv来实现,最终也是用过ffmpeg来进行编码和推流,javacv实现到可以直接接收摄像头的帧数据 需要自己实现的代码只是打开摄像头,写一个SurfaceView进行预览,然后实现PreviewCallback将摄像头每一帧的数据交给javacv即可 javacv地址:https://github.com/bytedeco/javacv demo地址:https://github.com/beautifulSoup/RtmpRecoder/tree/master 二: 使用Android自带的编码工具,可实现硬编码,这里有一个国内大神开源的封装很完善的的库yasea,第一种方法需要实现的Camera采集部分也一起封装好了,进行一些简单配置就可以实现编码推流,并且yasea目前已经直接支持摄像头的热切换,和各种滤镜效果 yasea地址(内置demo):https://github.com/begeekmyfriend/yasea 服务器 流媒体服务器我用的是srs,项目地址:https://github.com/ossrs/srs 关于srs的编译、配置、部署、在官方wiki中已经写的很详细了,并且srs同样是国内开发人员开源的项目,有全中文的文档,看起来很方便 这里有最基本的简单编译部署过程 Android直播实现(二)srs流媒体服务器部署 播放器 android端的播放使用vitamio,还是国内的开源播放器,是不是感觉国内的前辈们越来越屌了^~^! vitamio支持几乎所有常见的的视频格式和流媒体协议 vitamio地址(内置demo):https://github.com/yixia/VitamioBundle 这里使用的是yaesa库,先介绍一下直播实现的流程:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值