I am using opentok to implement Voice chat app. Everything is working fine I am stuck when changing the audio loudspeaker to Headset mode. I have also tried to manually change the loudspeaker audio voice to my ear piece speaker, but did not work.
Here is my class
public class MainActivity extends AppCompatActivity implements Session.SessionListener, PublisherKit.PublisherListener {
private static String API_KEY = "46066142";
private static String SESSION_ID = "1_MX40NjA2NjE0Mn5-MTUxOTM2OTQ3NDczMH5VaDJXL0lUdHRYWkpUTzlRbEs5aVJSaU1-fg";
private static String TOKEN = "T1==cGFydG5lcl9pZD00NjA2NjE0MiZzaWc9ZmE5N2IyMzExNTczYWM3NDRmYmUzNTNiNThkOWUzMDJmOTgxYWQ3MDpzZXNzaW9uX2lkPTFfTVg0ME5qQTJOakUwTW41LU1UVXhPVE0yT1RRM05EY3pNSDVWYURKWEwwbFVkSFJZV2twVVR6bFJiRXM1YVZKU2FVMS1mZyZjcmVhdGVfdGltZT0xNTE5MzcxMjUwJnJvbGU9cHVibGlzaGVyJm5vbmNlPTE1MTkzNzEyNTAuNzg1NDExODQyNDc1MjQ=";
private static final int RC_VIDEO_APP_PERM = 124;
private Session mSession;
private FrameLayout mPublisherViewContainer;
private FrameLayout mSubscriberViewContainer;
private Publisher mPublisher;
private Subscriber mSubscriber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermissions();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@AfterPermissionGranted(RC_VIDEO_APP_PERM)
private void requestPermissions() {
String[] perms = { Manifest.permission.INTERNET, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO,Manifest.permission.MODIFY_AUDIO_SETTINGS };
if (EasyPermissions.hasPermissions(this, perms)) {
// initialize view objects from your layout
mPublisherViewContainer = (FrameLayout)findViewById(R.id.publisher_container);
mSubscriberViewContainer = (FrameLayout)findViewById(R.id.subscriber_container);
// initialize and connect to the session
mSession = new Session.Builder(this, API_KEY, SESSION_ID).build();
mSession.setSessionListener(this);
mSession.connect(TOKEN);
} else {
EasyPermissions.requestPermissions(this, "This app needs access to your camera and mic to make video calls", RC_VIDEO_APP_PERM, perms);
}
}
@Override
public void onConnected(Session session) {
mPublisher = new Publisher.Builder(this).audioTrack(true).build();
mPublisher.setPublisherListener(this);
mPublisher.cycleCamera();
mPublisherViewContainer.addView(mPublisher.getView());
mSession.publish(mPublisher);
}
@Override
public void onDisconnected(Session session) {
}
@Override
public void onStreamReceived(Session session, Stream stream) {
if (mSubscriber == null) {
mSubscriber = new Subscriber.Builder(this, stream).build();
mSession.subscribe(mSubscriber);
mSubscriberViewContainer.addView(mSubscriber.getView());
}
}
@Override
public void onStreamDropped(Session session, Stream stream) {
if (mSubscriber != null) {
mSubscriber = null;
mSubscriberViewContainer.removeAllViews();
}
}
@Override
public void onError(Session session, OpentokError opentokError) {
}
@Override
public void onStreamCreated(PublisherKit publisherKit, Stream stream) {
}
@Override
public void onStreamDestroyed(PublisherKit publisherKit, Stream stream) {
}
@Override
public void onError(PublisherKit publisherKit, OpentokError opentokError) {
}
}
Libraries that i have used
implementation 'com.opentok.android:opentok-android-sdk:2.13.0'
implementation 'pub.devrel:easypermissions:0.4.0'
Add this code to your onStreamCreated method
@Override
public void onStreamCreated(PublisherKit publisherKit, Stream stream) {
AudioDeviceManager.getAudioDevice().setOutputMode(BaseAudioDevice.OutputMode.Handset);
}
After wasting my 4 days, I got my solution.I am answering my own question because maybe it helps someone rather than wasting his/her 4 days.I am answering my question only because I got the solution, my other question has no answer still, you can check.