Skip to content

Instantly share code, notes, and snippets.

@jchang419
Created March 1, 2017 22:02
Show Gist options
  • Save jchang419/e8d20ec7e64cf230ff157f48107968a8 to your computer and use it in GitHub Desktop.
Save jchang419/e8d20ec7e64cf230ff157f48107968a8 to your computer and use it in GitHub Desktop.
package com.example.jchang.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.util.Log;
import com.opentok.android.Publisher;
import com.opentok.android.PublisherKit;
import com.opentok.android.Session;
import com.opentok.android.Stream;
import com.opentok.android.Subscriber;
import com.opentok.android.SubscriberKit;
import com.opentok.android.OpentokError;
import android.speech.tts.TextToSpeech;
public class MainActivity extends AppCompatActivity implements Session.SessionListener,
Publisher.PublisherListener, Subscriber.SubscriberListener,
Subscriber.VideoListener {
public static final String API_KEY = "";
public static final String SESSION_ID = "";
public static final String TOKEN = "";
public static final String LOGTAG = MainActivity.class.getName();
private TextToSpeech textToSpeech;
public Session session;
private LinearLayout publisherView;
private LinearLayout.LayoutParams publisherParams;
private LinearLayout subscriberView;
private LinearLayout.LayoutParams subscriberParams;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout parentLayout = new LinearLayout(this);
subscriberView = new LinearLayout(this);
subscriberParams = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
subscriberParams.weight = 0.5f;
subscriberView.setLayoutParams(subscriberParams);
publisherView = new LinearLayout(this);
publisherParams = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
publisherParams.weight = 0.5f;
publisherView.setLayoutParams(publisherParams);
parentLayout.addView(publisherView);
parentLayout.addView(subscriberView);
session = new Session(MainActivity.this, API_KEY, SESSION_ID);
session.setSessionListener(this);
session.connect(TOKEN);
}
@Override
public void onConnected(Session session) {
Log.i(LOGTAG, "call to onConnected of the SessionListener");
textToSpeech = new TextToSpeech(getBaseContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS) {
textToSpeech.speak("one two three four five six seven eight nine ten one two three four five six seven eight nine ten one two three four five six seven eight nine ten", TextToSpeech.QUEUE_FLUSH, null);
}
}
});
Publisher publisher = new Publisher(MainActivity.this);
publisher.setPublisherListener(this);
publisherView.addView(publisher.getView(), publisherParams);
session.publish(publisher);
}
@Override
public void onStreamReceived(Session session, Stream stream) {
Log.i(LOGTAG, "call to onStreamReceived");
try {
Thread.sleep(2500);
Log.i(LOGTAG, "sleeping");
} catch (InterruptedException e) {
e.printStackTrace();
}
Subscriber subscriber = new Subscriber(MainActivity.this, stream);
subscriber.setVideoListener(this);
session.subscribe(subscriber);
subscriberView.addView(subscriber.getView(), subscriberParams);
}
@Override
public void onDisconnected(Session session) {
Log.i(LOGTAG, "call to onDisconnected of the SessionListener");
}
@Override
public void onStreamDropped(Session session, Stream stream) {
Log.i(LOGTAG, "call to onStreamDropped of the SessionListener");
}
@Override
public void onError(Session session, OpentokError error) {
Log.i(LOGTAG, "SessionListener error: " + error.getMessage());
}
@Override
public void onStreamCreated(PublisherKit publisher, Stream stream) {
Log.i(LOGTAG, "call to onStreamCreated of the PublisherListener");
}
@Override
public void onStreamDestroyed(PublisherKit publisher, Stream stream) {
Log.i(LOGTAG, "call to onStreamDestroyed of the PublisherListener");
}
@Override
public void onError(PublisherKit publisher, OpentokError error) {
Log.i(LOGTAG, "PublisherListener error: " + error.getMessage());
}
@Override
public void onConnected(SubscriberKit subscriber) {
Log.i(LOGTAG, "call to onConnected of the SubscriberListener");
}
@Override
public void onDisconnected(SubscriberKit subscriber) {
Log.i(LOGTAG, "call to onDisconnected of the SubscriberListener");
}
@Override
public void onError(SubscriberKit subscriber, OpentokError error) {
Log.i(LOGTAG, "SubscriberListener error: " + error.getMessage());
}
@Override
public void onVideoDataReceived(SubscriberKit subscriber) {
Log.i(LOGTAG, "call to onVideoDataReceived of the VideoListener");
}
@Override
public void onVideoDisabled(SubscriberKit subscriber, java.lang.String reason) {
Log.i(LOGTAG, "call to onVideoDisabled of the VideoListener");
}
@Override
public void onVideoEnabled(SubscriberKit subscriber, java.lang.String reason) {
Log.i(LOGTAG, "call to onVideoEnabled of the VideoListener");
}
@Override
public void onVideoDisableWarning(SubscriberKit subscriber) {
Log.i(LOGTAG, "call to onVideoDisableWarning of the VideoListener");
}
@Override
public void onVideoDisableWarningLifted(SubscriberKit subscriber) {
Log.i(LOGTAG, "call to onVideoDisableWarning of the VideoListener");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment