源码:https://github.com/BelledonneCommunications/linphone-android
API:http://www.linphone.org/docs/liblinphone-javadoc
如果官网提供的SDK无法满足需求,可以下载完整版进行编译,使用git命令(提示:因编码方式不同,下载的源码复制到不同的系统下将无法编译)
git clone git://git.linphone.org/linphone-android.git --recursive
一、下载SDK并引用
1.从官网下载sdk压缩包并解压,获得三个文件,其中linphone-android-liblinphone-tester-javadoc.jar为API,linphone-android-liblinphone-tester-sources.jar为java源码,liblinphone-sdk.aar为带资源的SDK文件。
2.AndroidStudio引用aar文件
方法一:将liblinphone-sdk.aar放到Module的libs目录下,并在bulid.gradle里添加
repositories { flatDir { dirs 'libs' } }
并在dependencies中添加
compile(name:'liblinphone-sdk',ext:'aar')
之后Reduild project重新构建就能使用。
方法二:new Module选择import JAR/.AAR Package选项,选择liblinphone-sdk.aar文件后会直接生成一个lib库,其他Module直接引用就能使用。
二、初始化
1.自定义Service实现LinphoneCoreListener接口,创建LinphoneCore并进行注册登录。
@Override
public void onCreate() {
try{
//创建LinphoneCore
LinphoneCore lc = LinphoneCoreFactory.instance().createLinphoneCore(this, this);
TimerTask lTask = new TimerTask() {
@Override
public void run() {
lc.iterate();
}
};
mTimer = new Timer("LinphoneMini scheduler");
mTimer.schedule(lTask, 0, 20);
String sipAddress = "sip:账号@服务器地址"; //你的sip地址
String password = ""; //你的密码
LinphoneAddress address = LinphoneCoreFactory.instance().createLinphoneAddress(sipAddress);
String username = address.getUserName();
String domain = address.getDomain();
LinphoneAuthInfo authInfo = LinphoneCoreFactory.instance().createAuthInfo(username, password, null, domain); LinphoneProxyConfig proxyConfig = getLc().createProxyConfig(sipAddress, domain, null, true);
lc.addProxyConfig(proxyConfig);
lc.addAuthInfo(authInfo);
lc.setDefaultProxyConfig(proxyConfig);
}catch(LinphoneCoreException e){
e.printStackTrace();
}
}
2.利用registrationState()回调监听注册状态
@Override
public void registrationState(LinphoneCore linphoneCore, LinphoneProxyConfig linphoneProxyConfig, LinphoneCore.RegistrationState registrationState, String s) {
Log.i("registrationState","registration: " + registrationState + " ---" + linphoneProxyConfig.getAddress() + " - " + s);
}
三、LinphoneChatMessage文本消息
1.使用LinphoneChatRoom和LinphoneChatMessage进行文本消息的发送
public void sendMessage() {
try {
//对方的sip地址
String to = "sip:对方账号@服务器地址";
LinphoneAddress toAddress = lc.interpretUrl(to);
//建立对话
LinphoneChatRoom cr = lc.getChatRoom(toAddress);
//创建消息
LinphoneChatMessage msg = cr.createLinphoneChatMessage("你好");
//发送消息
cr.sendChatMessage(msg);
} catch (LinphoneCoreException e) {
e.printStackTrace();
}
}
2.利用messageReceived()回调接收消息
@Override
public void messageReceived(LinphoneCore lpc, LinphoneChatRoom cr, LinphoneChatMessage msg) {
Toast.makeText(getApplicationContext(), "接收到来自" + msg.getFrom().getUserName() + "的消息: " + msg.getText(), Toast.LENGTH_SHORT).show();
}
四、LinphoneCall语音视频通话(单通话)
1.拨打电话
public synchronized void callOut(){
try {
//对方的sip地址
String to = "sip:对方账号@服务器地址";
LinphoneProxyConfig lpc = lc.getDefaultProxyConfig();
if (lpc != null){
to = lpc.normalizePhoneNumber(to);
}
LinphoneAddress toAddress = lc.interpretUrl(to);
if (lpc != null && toAddress.asStringUriOnly().equals(lpc.getIdentity())){
//判断下拨打的号码是否是自己注册登录的号码
return;
}
if (lc.isNetworkReachable()){
//创建通话参数
LinphoneCallParams params = lc.createCallParams(null);
//是否启用视频
params.setVideoEnabled(false);
//设置音频带宽
params.setAudioBandwidth(40);
//建立通话
lc.inviteAddressWithParams(toAddress,params);
} else {
Log.e("Error: no network");
}
} catch (LinphoneCoreException e){
e.printStackTrace();
}
}
2.接听电话
public synchronized void callAnswer(){
LinphoneCall mCall = null;
ArrayList calls = new ArrayList<>(Arrays.asList(lc.getCalls()));
for (LinphoneCall call : calls){
LinphoneCall.State cstate = call.getState();
if (LinphoneCall.State.IncomingReceived == cstate){
mCall = call;
break;
}
}
if (mCall == null){
return;
}
LinphoneCallParams params = lc.createCallParams(mCall);
if (params != null){
try {
lc.acceptCallWithParams(mCall,params);
} catch (LinphoneCoreException e){
lc.enableSpeaker(false);
Log.i(e,"Accept call failed");
}
} else {
Toast.makeText(this,"An error occurred while accepting the call",Toast.LENGTH_LONG).show();
}
}
3.挂断电话
lc.terminateAllCalls();
4.利用callState()回调监听电话状态
@Override
public void callState(LinphoneCore linphoneCore,LinphoneCall call,LinphoneCall.State state,String s){
Log.i("callState","与你通话的是: " + call.getRemoteAddress()+ " - " + state + " - " + s);
if (state == LinphoneCall.State.IncomingReceived){
//来电
}
if (state == LinphoneCall.State.OutgoingRinging){
//去电
}
if (state == LinphoneCall.State.Connected){
//来去电接通
}
if (state == LinphoneCall.State.CallEnd || state == LinphoneCall.State.CallReleased || state == LinphoneCall.State.Error){
//结束释放、错误通话
}
}