我正在使用 pjsua2 库构建应用程序。库附带的示例应用程序运行良好。但是在我的应用程序中,当我尝试使用以下代码调用电话时,
if (currentCall != null) {return;}
String buddy_uri = "sip:" + number + "@" + domain;
MyCall call = new MyCall(account, -1);
CallOpParam prm = new CallOpParam(true);
try {
call.makeCall(buddy_uri, prm);
} catch (Exception e) {
call.delete();
return;
}
currentCall = call;
我得到下面的异常,
java.lang.Exception: Title: pjsua_call_make_call(acc.getId(),
&pj_dst_uri, param.p_opt, this, param.p_msg_data, &id)
Code: 420003
Description: Audio subsystem not initialized (PJMEDIA_EAUD_INIT)
Location: ../src/pjsua2/call.cpp:490
此错误仅出现在 Marshmallow 中,在 Kitkat 设备中,它工作正常。有人可以阐明这个问题吗?
最佳答案
在 Marshmallow 的 Android 中,我们需要获得权限。
我使用下面的代码来检查并获得用户的权限。
private static final int REQUEST_PERMISSIONS = 100;
private static final String PERMISSIONS_REQUIRED[] = new String[]{
Manifest.permission.READ_CONTACTS,Manifest.permission.RECORD_AUDIO
};
private boolean checkPermission(String permissions[]) {
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(getApplicationContext(), permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
private void checkPermissions() {
boolean permissionsGranted = checkPermission(PERMISSIONS_REQUIRED);
if (permissionsGranted) {
Toast.makeText(this, "You've granted all required permissions!", Toast.LENGTH_SHORT).show();
} else {
boolean showRationale = true;
for (String permission : PERMISSIONS_REQUIRED) {
showRationale = ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
if (!showRationale) {
break;
}
}
String dialogMsg = showRationale ? "We need some permissions to run this APP!" : "You've declined the required permissions, please grant them from your phone settings";
new AlertDialog.Builder(this)
.setTitle("Permissions Required")
.setMessage(dialogMsg)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(mActivity, PERMISSIONS_REQUIRED, REQUEST_PERMISSIONS);
}
}).create().show();
}
}
关于android - PJSUA2 无法从 Android 6.0.1 调用电话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42291432/