Tarun Gupta
Tarun Gupta

Reputation: 6403

OpenTok Api Broadcasted video placement in web page

I am using opentok and connected to the broadcast service and getting object of flash player at the bottom of the page.

How can i place it in a particular div..

This is the code i am using to connect to opentol API

function initiatecall() {
            if (session != undefined) {
                if (!iscalled) {
                    session.addEventListener("sessionConnected", sessionConnectedHandler);
                    session.addEventListener("streamCreated", streamCreatedHandler);
                    session.connect("21457612", token_id); // Replace with your API key and token. See https://dashboard.tokbox.com/projects
                    // and https://dashboard.tokbox.com/projects
                    iscalled = true;

                    $.ajax({
                        data: '{"ChatId":"' + chat_id + '","NurseId":"' + nurse_id + '","DeviceType":"Browser"}',
                        type: "POST",
                        dataType: "json",
                        contentType: "application/json;charset=utf-8",
                        url: "someurl.asmx/MakeCall",
                        success: function (data) { initiatecall(chat_id, session_id, token_id); },
                        eror: function (a, b, c) { alert(a.responseText); }
                    });

                }
            } else {
                alert("Session Expired!!");
            }
        }

        function sessionConnectedHandler(event) {
            subscribeToStreams(event.streams);
            session.publish();
        }

        function streamCreatedHandler(event) {
            subscribeToStreams(event.streams);
        }

        function subscribeToStreams(streams) {
            for (i = 0; i < streams.length; i++) {
                var stream = streams[i];
                if (stream.connection.connectionId != session.connection.connectionId) {
                    session.subscribe(stream);
                }
            }
        }

        function exceptionHandler(event) {
            alert("Exception: " + event.code + "::" + event.message);
        }


        </script>
    <!--End of code-->
    <!--Signal R-->
    <script type="text/javascript">
        $(function () {

            //$.hubConnection.app.MapHubs(new HubConfiguration { EnableCrossDomain = true });
            // Proxy created on the fly
            var chat = $.connection.chat
            //var chat = $.connection.WebPushNotification;
            //alert(chat);
            // Start the connection
            //            $.connection.hub.start();
            //port 1935
            $.connection.hub.start({ transport: 'auto' }, function () {
                //alert('connected');
                $("#info").append("<br/>Hub Started..");
                initiatecall();
                $("#info").append("<br/>Call Initiated..");
                chat.send(chat_id + ',' + session_id + ',' + token_id + ',' + '<%=Session["UserId"].ToString() %>');
                $("#info").append("<br/>Broadcasted Message..");
                //$('#MainContent_connected').text('Connected to chat room');
            });

            // Declare a function on the chat hub so the server can invoke it
            chat.addMessage = function (message) {
                //alert(message);
                //$('#messages').append('<li>' + message + '</li>');
            };
        });

    </script>

I have taken a div to show all the progress.

 <div id="info">
        </div>

It connect me to my videos and ask for the permission but it takes its own place not according to my design.

Upvotes: 5

Views: 751

Answers (2)

nanocult
nanocult

Reputation: 104

You have to include an ID of the element which will be replaced in your DOM (here is for example replaceElementId)

<div id="container">
   <div id="replaceElementId"></div>
</div>

Modify your session.subscribe

function subscribeToStreams(streams) {
    for (i = 0; i < streams.length; i++) {
        var stream = streams[i];
        if (stream.connection.connectionId != session.connection.connectionId) {
            //This is where you have to make changes
            //session.subscribe(stream);

              session.subscribe(stream, "replaceElementId", 
                  { width:640, height:480 } );
        }
    }
}

As you could see you could even define your own size of the video stream or other GUI changes.

More information at official docs TokBox API:Session

Upvotes: 0

sjkm
sjkm

Reputation: 3937

I think you should add the parameter replaceElementId and set its value to a div's id that should be replaced with the player. Example: If you want to load the player inside the #container div you define a div #replaceElementId inside the #container that will be replaced with the player:

<div id="container">
  <div id="replaceElementId"></div>
</div>

Upvotes: 2

Related Questions