gpt4 book ai didi

android - 我正在使用 Twilio android SDK,当我点击挂断按钮时总是出现错误?拨号后无法断开调用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:39:09 24 4
gpt4 key购买 nike

我已经成功集成了适用于 Android 的 Twilio SDK,但是当我尝试使用它进行调用时,它没有连接并在 logcat 中显示错误消息,我收到以下错误:

1) Failed to hangup call due to error code: 70015, message: pjsua_call_hangup(): Object already exists (PJ_EEXISTS) 

2) Connection disconnected with error code 31000 and message Generic error

this both errors occurs when i am calling disconnect method if i comment this method then my calling is working fine and i make call but if i call this method phone.disconnect(), i am getting error shown as above.

This is my methods which i am using to make calls and to disconnect the calls.

//////调用电话

public void connect(String phoneNumber) { 
Map parameters = new HashMap();
parameters.put("PhoneNumber", phoneNumber);
connection = device.connect(parameters, null /* ConnectionListener */);
if (connection == null)
Log.w(TAG, "Failed to create new connection");
}

//断开电话

public void disconnect() { 
if (connection != null) {
connection.disconnect();
connection = null; // will null out in onDisconnected()
if (basicConnectionListener != null)
basicConnectionListener.onConnectionDisconnecting();
}
}

在我的 OnClick 事件上:

public void onClick(View view) { 
if (view.getId() == R.id.dialButton)
Toast.makeText(getApplicationContext(), "Dialing...", Toast.LENGTH_LONG).show();
phone.connect("PHONE NUMBER");
if (view.getId() == R.id.hangupButton)
Toast.makeText(getApplicationContext(), "Call Disconnected...", Toast.LENGTH_LONG).show();
phone.disconnect();

Please suggest me and help me because i tried all possible thing for hangout but still not able to solve it.Thanks in advance. ![enter image description here][1]

最佳答案

我正在为 twilio SDK 分享我的 worker 类(Class)设置。

这个调用非常适合我。我想分享我的类(class),以便任何人都可以引用。

这是处理 twillio 调用和断开调用的所有事件的基类。

    import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.media.AudioManager;
import android.util.Log;
import android.view.Gravity;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.twilio.client.Connection;
import com.twilio.client.Connection.State;
import com.twilio.client.ConnectionListener;
import com.twilio.client.Device;
import com.twilio.client.DeviceListener;
import com.twilio.client.PresenceEvent;
import com.twilio.client.Twilio;

public class MonkeyPhone implements Twilio.InitListener, DeviceListener, ConnectionListener {
private static final String TAG = "MonkeyPhone";
private Device device;
private Connection connection;
private final Context context;
private BasicConnectionListener basicConnectionListener;
private BasicDeviceListener basicDeviceListener;
private Connection pendingIncomingConnection;
InputMethodManager imm;
private boolean speakerEnabled;
private boolean muteEnabled;

public interface BasicConnectionListener {
public void onIncomingConnectionDisconnected();

public void onConnectionConnecting();

public void onConnectionConnected();

public void onConnectionFailedConnecting(Exception error);

public void onConnectionDisconnecting();

public void onConnectionDisconnected();

public void onConnectionFailed(Exception error);
}

public interface BasicDeviceListener {
public void onDeviceStartedListening();

public void onDeviceStoppedListening(Exception error);
}

public MonkeyPhone(Context context) {
this.context = context;
Twilio.initialize(context, this /* Twilio.InitListener */);
}

public void setListeners(BasicConnectionListener basicConnectionListener, BasicDeviceListener basicDeviceListener) {
this.basicConnectionListener = basicConnectionListener;
this.basicDeviceListener = basicDeviceListener;
}

/* Twilio.InitListener method */
@Override
public void onInitialized() {
Log.d(TAG, "Twilio SDK is ready");
try {
// String capabilityToken =
// HttpHelper.httpGet("http://------/mobile/auth.php");
String capabilityToken = HttpHelper.httpGet("http:------/auth.php");

device = Twilio.createDevice(capabilityToken, null /* DeviceListener */);
} catch (Exception e) {
Log.e(TAG, "Failed to obtain capability token: " + e.getLocalizedMessage());
}
}

/* Twilio.InitListener method */
@Override
public void onError(Exception e) {
Log.e(TAG, "Twilio SDK couldn't start: " + e.getLocalizedMessage());
}

@Override
protected void finalize() {
if (device != null)
device.release();
if (connection != null)
connection.disconnect();
}

// To Make Calls

public void connect(String phoneNumber) {
Toast toast = Toast.makeText(context, "Dialing...", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("PhoneNumber", phoneNumber);
String capabilityToken;
try {
// capabilityToken =
// HttpHelper.httpGet("http://------/mobile/auth.php");
capabilityToken = HttpHelper.httpGet("http://------/mobile/auth.php");

device = Twilio.createDevice(capabilityToken, null /* DeviceListener */);
} catch (Exception e1) {
e1.printStackTrace();
}
try {
device.disconnectAll();
} catch (Exception e) {
e.printStackTrace();
}
connection = device.connect(parameters, null /* ConnectionListener */);
if (connection == null) {
Log.w(TAG, "Failed to create new connection");
}
}

// To Disconnect Phone
public void disconnect() {
Toast toast = Toast.makeText(context, "Call Disconnected...", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

if (connection != null) {
connection.disconnect();
connection = null; // will null out in onDisconnected()

if (basicConnectionListener != null)
basicConnectionListener.onConnectionDisconnecting();
}
}

public void setSpeakerEnabled(boolean speakerEnabled) {
if (speakerEnabled != this.speakerEnabled) {
this.speakerEnabled = speakerEnabled;
updateAudioRoute();
}
}

private void updateAudioRoute() {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(speakerEnabled);
}

public void setMuteEnabled(boolean muteEnabled) {
if (muteEnabled != this.muteEnabled) {
this.muteEnabled = muteEnabled;
updateAudioRouteForMute();
}
}

private void updateAudioRouteForMute() {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMicrophoneMute(muteEnabled);
}

public State status() {
connection.getState();
State statusHere = connection.getState();
return statusHere;
}

@Override
public void onConnected(Connection arg0) {
updateAudioRoute();
updateAudioRouteForMute();
if (basicConnectionListener != null) {
basicConnectionListener.onConnectionConnected();
}
}

@Override
public void onConnecting(Connection arg0) {
if (basicConnectionListener != null) {
basicConnectionListener.onConnectionConnecting();
}
}

@Override
public void onDisconnected(Connection inConnection) {
if (inConnection == connection) {
connection = null;
if (basicConnectionListener != null)
basicConnectionListener.onConnectionDisconnected();
} else if (inConnection == pendingIncomingConnection) {
pendingIncomingConnection = null;
if (basicConnectionListener != null)
basicConnectionListener.onIncomingConnectionDisconnected();
}
}

@Override
public void onDisconnected(Connection inConnection, int arg1, String inErrorMessage) {
if (inConnection == connection) {
connection = null;
if (basicConnectionListener != null)
basicConnectionListener.onConnectionFailedConnecting(new Exception(inErrorMessage));
}

}

@Override
public void onPresenceChanged(Device arg0, PresenceEvent arg1) {

}

@Override
public void onStartListening(Device arg0) {
if (basicDeviceListener != null) {
basicDeviceListener.onDeviceStartedListening();
}
}

@Override
public void onStopListening(Device arg0) {
if (basicDeviceListener != null) {
basicDeviceListener.onDeviceStoppedListening(null);
}
}

@Override
public void onStopListening(Device arg0, int arg1, String arg2) {

}

@Override
public boolean receivePresenceEvents(Device arg0) {
return false;
}

}

你可以像这样使用这个类:

  private MonkeyPhone phone;
//On create
phone = new MonkeyPhone(getApplicationContext());

//On Call Button Click

@Override
public void onClick(View view) {
if (view.getId() == R.id.btnCallHere) {

phone.connect(newContact);

} else if (view.getId() == R.id.btnDisconnectHere) {

phone.disconnect();

}

关于android - 我正在使用 Twilio android SDK,当我点击挂断按钮时总是出现错误?拨号后无法断开调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17971536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com