无法使用 OpenTok 在耳机模式下切换音频

Posted

技术标签:

【中文标题】无法使用 OpenTok 在耳机模式下切换音频【英文标题】:Unable to switch audio in headset mode using OpenTok 【发布时间】:2018-02-23 13:19:21 【问题描述】:

我正在使用 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】:

解决方案

将此代码添加到您的 onStreamCreated 方法中

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

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

【讨论】:

它对我不起作用,有什么我应该更改或添加的吗? @TokaA.Amin 你需要检查最新版本 我做了,但问题仍然存在

以上是关于无法使用 OpenTok 在耳机模式下切换音频的主要内容,如果未能解决你的问题,请参考以下文章

win7 进入传奇提示音频设备无法初始化(插上耳机就可以玩,不想插入耳机如何解决)

电脑无法识别耳机怎么解决 电脑无法识别耳机解决方法

Android蓝牙耳机/扬声器音频切换

Android蓝牙耳机/扬声器音频切换

连接蓝牙耳机时无法切换到扬声器输出

无法通过扬声器播放视频音频 - iOS