使用多方和广播功能的基于OpenTok的应用程序

时间:2018-02-08 17:38:38

标签: android opentok tokbox broadcasting

我开发的应用程序是多方和广播的混合体。将有一个特定的管理员/主持人,所有订户(广播)都订阅了该流。主持人/发布者以及所有订阅者都能够看到会话中的所有参与者(有点多方)。在来自主机/发布者的事件(例如按钮点击等)上,发布者现在将停止发布他/她的流,并且所有订户将在主机选择的会话中查看来自特定订户的流。由于我的应用程序使用Android SDK并且没有主持人令牌。我怎样才能成功创建我想要的应用程序。( (注意:我已经完成基本音频,视频聊天)  你能否提供必要的方法和示例代码来完成它。

我使用了以下代码:

private HashMap<String, Stream> mStringStreams = new HashMap<String, Stream>();
 @Override
    public void onStreamReceived(Session session, final Stream stream) {

        Log.d(LOG_TAG, "onStreamReceived: New Stream Received " + stream.getStreamId() + " in session: " + session.getSessionId()+" Stream name: "+stream.getName());

           if(stream.getName().equalsIgnoreCase("Main_stream"))
            {
                    mSubscriber = new Subscriber.Builder(SuscriberActivity.this, stream).build();
                    mSession.subscribe(mSubscriber);
                    mSubscriberViewContainer.addView(mSubscriber.getView());

            }
        if (mSubscribers.size() + 1 > MAX_NUM_SUBSCRIBERS ) {
            Toast.makeText(this, "New subscriber ignored. MAX_NUM_SUBSCRIBERS limit reached.", Toast.LENGTH_LONG).show();
            return;
        }
        if(stream.getName().compareTo("Main_stream")!=0)
        {

            final Subscriber subscriber = new Subscriber.Builder(SuscriberActivity.this, stream).build();
            mStringStreams.put(stream.getStreamId().toString(), stream);
            mSession.subscribe(subscriber);
            mSubscribers.add(subscriber);
            mSubscriberStreams.put(stream,subscriber);
            int position = mSubscribers.size() - 1;
            final int id = getResources().getIdentifier("subscriberview" + (new Integer(position)).toString(), "id", SuscriberActivity.this.getPackageName());
            FrameLayout subscriberViewContainer = (FrameLayout) findViewById(id);
            subscriber.setStyle(BaseVideoRenderer.STYLE_VIDEO_SCALE, BaseVideoRenderer.STYLE_VIDEO_FILL);
            subscriberViewContainer.addView(subscriber.getView());
        }

 @Override
   public void onSignalReceived(Session session, String s, String s1, Connection connection) {
stream = mSubscriberStreams.get(s1).getStream();
          Stream stream= mStringStreams.get(s1);
             Subscriber mSubscriber1 = new Subscriber.Builder(SuscriberActivity.this, stream).build();
            mSubscriber1.setStyle(BaseVideoRenderer.STYLE_VIDEO_SCALE, BaseVideoRenderer.STYLE_VIDEO_FILL);
            mSubscriberViewContainer.removeView(mPublisher.getView());
            mSubscriberViewContainer.addView(mSubscriber1.getView().getRootView());

        }
    }

1 个答案:

答案 0 :(得分:0)

如果我理解正确,当发布者(主要主持人)点击按钮时,您尝试让参与者订阅不同的流。为此,您可以使用OpenTok Signaling API向所有参与者发送带有streamId数据的信号,以便他们可以订阅该特定用户。