使用OpenTok

时间:2018-02-23 13:19:21

标签: java android opentok

我正在使用opentok来实现语音聊天应用。一切都工作正常我将音频扬声器更改为耳机模式时卡住了。我尝试了不同的方法,但没有人帮助我。我也尝试手动将扬声器音频语音更改为我的耳机扬声器,但没有用。

请帮助我提前谢谢

这是我的班级

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) {

    }

}

我用过的图书馆

implementation 'com.opentok.android:opentok-android-sdk:2.13.0'
implementation 'pub.devrel:easypermissions:0.4.0'

1 个答案:

答案 0 :(得分:3)

解决方案

  

将此代码添加到onStreamCreated方法

@Override
public void onStreamCreated(PublisherKit publisherKit, Stream stream) {
    AudioDeviceManager.getAudioDevice().setOutputMode(BaseAudioDevice.OutputMode.Handset);
}

浪费了我的4天后,我得到了解决方案。我正在回答我自己的问题,因为它可能对某人有所帮助而不是浪费他/她4天。我回答我的问题只是因为我得到了解决方案,我的另一个问题是没有答案,你可以检查。