美文网首页
liblinphone-Android的简单应用

liblinphone-Android的简单应用

作者: 一个圈_c7f7 | 来源:发表于2018-04-27 18:13 被阅读0次

官网: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){

        //结束释放、错误通话

    }

}

相关文章

  • liblinphone-Android的简单应用

    官网:http://www.linphone.org 源码:https://github.com/Belledon...

  • 简单应用

    统计a.sh文件中hello出现的频率 输出a.sh文件中hello出现的行数 输出文档的前n行记录 输出文档后n...

  • WKWebView的简单应用

    苹果公司在2014 WWDC上发布了iOS8,同时引入了WKWebView来替代传统的UIWebView。现在的产...

  • 函数的简单应用

    1.函数的简单应用 公共: str, int,float,bool,bytes,list,set,tuple,...

  • redux的简单应用

    1、安装 redux和react-redux: npm i redux react-redux --save-de...

  • VUEX的简单应用

  • EasyAR的简单应用

    为什么选EasyAR AR的广泛应用 AR(Augmented Reality)即增强现实,是一种实时地计算摄影机...

  • WebSocket的简单应用

    1、WebSocket简单介绍 WebSocket是html5规范中的一个部分,它借鉴了socket这种思想,为w...

  • CAShapeLayer的简单应用

    CAShapeLayer是一个通过矢量图形而不是bitmap来绘制的图层CALayer的子类。你指定诸如颜色和线宽...

  • 盒子的简单应用

    内边距就是内边距到边框的距离。 padding :上 右 下 左 四边分别设置 padding:上 右 下 上下...

网友评论

      本文标题:liblinphone-Android的简单应用

      本文链接:https://www.haomeiwen.com/subject/hkjaixtx.html