开发者社区> 问答> 正文

安卓使用Rtc服务时,进入频道后如何发布自己的音频流和获得其他人发布的音视频流?

写的回调里只有加入频道结果回调触发了,其他都没有反应,结果是能听到音频看不到远程的视频信息

private final AliRtcEngineNotify mNotify= new AliRtcEngineNotify() {

        @Override
        public void onRemoteUserOnLineNotify(String uid, int elapsed){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    LinearLayoutCompat layout= findViewById(R.id.remote);
                    SurfaceView mRemoteView =engine.createRenderSurfaceView(getApplicationContext());
                    mRemoteView.setZOrderMediaOverlay(true);
                    layout.addView(mRemoteView);
                    AliRtcEngine.AliRtcVideoCanvas canvas = new AliRtcEngine.AliRtcVideoCanvas();
                    canvas.renderMode = AliRtcRenderModeAuto/* renderMode */;
                    canvas.view = mRemoteView;/* 渲染view */
                    engine.setRemoteViewConfig(canvas, uid, AliRtcVideoTrackCamera);

                }
            });
        }

        @Override
        public void onRemoteTrackAvailableNotify(String uid, AliRtcEngine.AliRtcAudioTrack audioTrack,
                                                 AliRtcEngine.AliRtcVideoTrack videoTrack) {
            // UI或者逻辑处理,例如渲染远端视频流的操作如下。
            if(videoTrack == AliRtcVideoTrackCamera) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        LinearLayoutCompat layout= findViewById(R.id.remote);
                        SurfaceView mLocalView=engine.createRenderSurfaceView(getApplicationContext());
                        mLocalView.setZOrderMediaOverlay(true);
                        layout.addView(mLocalView);
                        AliRtcEngine.AliRtcVideoCanvas canvas = new AliRtcEngine.AliRtcVideoCanvas();
                        canvas.renderMode = AliRtcRenderModeAuto/* renderMode */;
                        canvas.view = mLocalView;/* 渲染view */
                        engine.setRemoteViewConfig(canvas, uid, AliRtcVideoTrackCamera);

                    }
                });
            }

        }
        @Override
        public void onFirstVideoFrameReceived(String uid, AliRtcEngine.AliRtcVideoTrack videoTrack, int timeCost){
            if(videoTrack == AliRtcVideoTrackCamera) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        LinearLayoutCompat layout= findViewById(R.id.remote);
                        SurfaceView mRemoteView =engine.createRenderSurfaceView(getApplicationContext());
                        mRemoteView.setZOrderMediaOverlay(true);
                        layout.addView(mRemoteView);
                        AliRtcEngine.AliRtcVideoCanvas canvas = new AliRtcEngine.AliRtcVideoCanvas();
                        canvas.renderMode = AliRtcRenderModeAuto/* renderMode */;
                        canvas.view = mRemoteView;/* 渲染view */
                        engine.setRemoteViewConfig(canvas, "uid", AliRtcVideoTrackCamera);

                    }
                });
            }
        }
    };

    private final AliRtcEngineEventListener mListener = new AliRtcEngineEventListener() {



        @Override
        public void onVideoPublishStateChanged(AliRtcEngine.AliRtcPublishState oldState ,
                                               AliRtcEngine.AliRtcPublishState newState,
                                               int elapseSinceLastState, String channel){
            Looper.prepare();
            Toast.makeText(MainActivity.this,"old:"+oldState+",new:"+newState,Toast.LENGTH_SHORT).show();
            Looper.loop();
        }

        @Override
        public void onVideoSubscribeStateChanged(String uid,
                                                 AliRtcEngine.AliRtcSubscribeState oldState,
                                                 AliRtcEngine.AliRtcSubscribeState newState,
                                                 int elapseSinceLastState, String channel){
            Looper.prepare();
            Toast.makeText(MainActivity.this,uid+" old:"+oldState+" new:"+newState,Toast.LENGTH_SHORT).show();
            Looper.loop();
        }

        @Override
        public void onJoinChannelResult(int result, String channel, int elapsed) {
            Looper.prepare();
            if(result==0)
                Toast.makeText(MainActivity.this,"加入成功",Toast.LENGTH_SHORT).show();
            else Toast.makeText(MainActivity.this,"加入失败,错误码为"+result,Toast.LENGTH_SHORT).show();
            engine.publishLocalAudioStream(true);
            engine.publishLocalVideoStream(true);
            engine.setDefaultSubscribeAllRemoteAudioStreams(true);
            engine.setDefaultSubscribeAllRemoteVideoStreams(true);
            Log.e("log",String.valueOf(engine.isLocalVideoStreamPublished()));
            /*runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    LinearLayoutCompat layout= findViewById(R.id.remote);
                    SurfaceView mRemoteView =engine.createRenderSurfaceView(getApplicationContext());
                    mRemoteView.setZOrderMediaOverlay(true);
                    layout.addView(mRemoteView);
                    AliRtcEngine.AliRtcVideoCanvas canvas = new AliRtcEngine.AliRtcVideoCanvas();
                    canvas.renderMode = AliRtcRenderModeAuto*//* renderMode *//*;
                    canvas.view = mRemoteView;*//* 渲染view *//*
                    engine.setRemoteViewConfig(canvas, "uid", AliRtcVideoTrackCamera);

                }
            });*/

            Looper.loop();
        }

        @Override
        public void onLeaveChannelResult(int result, AliRtcEngine.AliRtcStats stats){
            Looper.prepare();
            if(result==0)
                Toast.makeText(MainActivity.this,"离开成功",Toast.LENGTH_SHORT).show();
            else Toast.makeText(MainActivity.this,"离开失败,错误码为"+result,Toast.LENGTH_SHORT).show();
            Looper.loop();
        }


    };

展开
收起
游客rp53bsjx3g7hc 2021-08-09 22:41:56 358 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载