我已经成功集成了Twilio SDK for android但是当我试图用它进行调用时它没有连接并在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]
答案 0 :(得分:6)
我正在为twilio SDK设置分享我的工作班。
这个电话对我来说非常合适。我想分享我的课程,所以任何人都可以参考它。
这是处理twillio呼叫和断开呼叫的所有事件的Base类。
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();
}