我正在使用(Python PJSUA(从我的树莓派进行调用。 如果接收器已断开连接。 我无法识别呼叫已断开连接。我打算为陆地电话到手机VoIP呼叫转移设备创建此内容。我没有编写完整的代码,因为我无法解决这个基本问题。那么我如何识别接收器(安卓手机(断开通话
while in_call:
if self.call.info().state == pjsua.CallState.DISCONNECTED:
in_call = False
break
pass
'
此方法不起作用。 我的程序在呼叫者断开连接后退出
完整代码如下
import sys
import pjsua
import threading
import wave
from time import sleep
callerid = 0
#acc_cfg.ka_interval =30; re-registration period
def log_cb(level, str, len):
print str,
class MyAccountCallback(pjsua.AccountCallback):
sem = None
def __init__(self, account=None):
pjsua.AccountCallback.__init__(self, account)
def wait(self):
self.sem = threading.Semaphore(0)
self.sem.acquire()
def on_reg_state(self):
if self.sem:
if self.account.info().reg_status >= 200:
self.sem.release()
def cb_func(pid) :
print '%s playback is done' % pid
current_call.hangup()
# Callback to receive events from Call
class MyCallCallback(pjsua.CallCallback):
def __init__(self, call=None):
pjsua.CallCallback.__init__(self, call)
# Notification when call state has changed
def on_state(self):
global current_call
global in_call
print "Call with", self.call.info().remote_uri,
print "is", self.call.info().state_text,
print "last code =", self.call.info().last_code,
print "(" + self.call.info().last_reason + ")"
if self.call.info().state == pjsua.CallState.DISCONNECTED:
current_call = None
print 'Current call is', current_call
in_call = False
elif self.call.info().state == pjsua.CallState.CONFIRMED:
#Call is Answred
print "Call Answred"
call_slot = self.call.info().conf_slot
#if self.call.info().media_state == pj.MediaState.ACTIVE:
call_slot = self.call.info().conf_slot
lib.conf_connect(call_slot, 0)
lib.conf_connect(0, call_slot)
print("Hey !!!!! Hope you are doing GOOD !!!!!!!!!!")
while in_call:
if self.call.info().state == pjsua.CallState.DISCONNECTED:
in_call = False
break
pass
self.call.hangup()
in_call = False
# Notification when call's media state has changed.
def on_media_state(self):
if self.call.info().media_state == pjsua.MediaState.ACTIVE:
print "Media is now active"
else:
print "Media is inactive"
# Function to make call
def make_call(uri):
try:
print "Making call to", uri
return acc.make_call(uri, cb=MyCallCallback())
except pjsua.Error, e:
print "Exception: " + str(e)
return None
lib = pjsua.Lib()
try:
lib.init(log_cfg = pjsua.LogConfig(level=4, callback=log_cb))
lib.create_transport(pjsua.TransportType.UDP, pjsua.TransportConfig(6500))
#lib.set_null_snd_dev()
lib.start()
lib.handle_events()
acc_cfg = pjsua.AccountConfig()
acc_cfg.id = "sip:111@192.168.1.102"
acc_cfg.reg_uri = "sip:111@192.168.1.102"
acc_cfg.proxy = [ "sip:111@192.168.1.102;lr" ]
acc_cfg.auth_cred = [ pjsua.AuthCred("*", "111@111@192.168.1.102", "0000") ]
acc_cb = MyAccountCallback()
acc = lib.create_account(acc_cfg, cb=acc_cb)
acc_cb.wait()
print "n"
print "Registration complete, status=", acc.info().reg_status,
"(" + acc.info().reg_reason + ")"
#YOURDESTINATION is landline or mobile number you want to call
dst_uri="sip:100@192.168.1.102"
in_call = True
lck = lib.auto_lock()
current_call = make_call(dst_uri)
print 'Current call is', current_call
del lck
#wait for the call to end before shuting down
while in_call:
pass
sys.stdin.readline()
lib.destroy()
lib = None
except pjsua.Error, e:
print "Exception: " + str(e)
lib.destroy()
更改为
if self.call.info().state == pjsua.CallState.CONFIRMED:
"your statements"
if self.call.info().state == pjsua.CallState.DISCONNECTED:
"your statements"
ELIF 语句不会在您的情况下标记。
您必须将其从"已确认"状态中移出
while in_call:
if self.call.info().state == pjsua.CallState.DISCONNECTED:
in_call = False
break
pass
self.call.hangup()
in_call = False
因为当呼叫断开连接时,状态将更改为已断开连接