liblinphone-Android的簡單應用

官網:http://www.linphone.org

源碼: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')

bulid.gradle配置

之后Reduild project重新構建就能使用。

方法二:new Module選擇import JAR/.AAR Package選項,選擇liblinphone-sdk.aar文件后會直接生成一個lib庫,其他Module直接引用就能使用。

導入aar文件
生成的lib庫

二、初始化

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){

? ? ? ? //結束釋放、錯誤通話

? ? }

}

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容