基于linphone android sdk 的voip语音、视频通话 教程二、sip语音拨打、接听

如果觉得下面的麻烦可以直接到https://item.taobao.com/item.htm?id=587133228080获取源码。源码功能性更好、更完善。

想测试apk请加群261074724 

最新的sdk4教程地址  https://blog.csdn.net/Java_lilin/article/details/84842314

在前一篇我写了关于sip注册的 可以参考https://blog.csdn.net/Java_lilin/article/details/80539116

1.在实现拨打之前我们先把注册状态显示到view上 

让LinphoneMiniManager extends Service 便于发送消息到MainActivity.java 别忘了AndroidManifest.xml的注册service

    <service android:name=".LinphoneMiniManager"></service>

在把manger的代码改改

public LinphoneMiniManager(Context c) {
		mContext = c;
		LinphoneCoreFactory.instance().setDebugMode(true, "Linphone Mini");

		try {
			String basePath = mContext.getFilesDir().getAbsolutePath();
			copyAssetsFromPackage(basePath);
			mLinphoneCore = LinphoneCoreFactory.instance().createLinphoneCore(this, basePath + "/.linphonerc", basePath + "/linphonerc", null, mContext);
			initLinphoneCoreValues(basePath);

			setUserAgent();
			setFrontCamAsDefault();
			startIterate();
			mInstance = this;
	        mLinphoneCore.setNetworkReachable(true); // Let's assume it's true
		} catch (LinphoneCoreException e) {
		} catch (IOException e) {
		}
	}



//上面的改为

private LinphoneCoreFactory lcFactory;
	 public static boolean isReady() {
	        return mInstance != null;
	 }
	 
	@Override
	public void onCreate() { 
		super.onCreate();
		 
		mContext = this;
		lcFactory = LinphoneCoreFactory.instance();
		lcFactory.setDebugMode(true, "lilinaini 1"); 
		try {
			String basePath = mContext.getFilesDir().getAbsolutePath();
			copyAssetsFromPackage(basePath);
			mLinphoneCore = lcFactory.createLinphoneCore(this, basePath + "/.linphonerc", basePath + "/linphonerc", null, mContext);
			initLinphoneCoreValues(basePath);

			setUserAgent();
			setFrontCamAsDefault();
			startIterate();
			mInstance = this;
	        mLinphoneCore.setNetworkReachable(true); // Let's assume it's true
		} catch (LinphoneCoreException e) { 	} catch (IOException e) { }
		
	}

	public static LinphoneMiniManager getInstance() {
		return mInstance;
	}
	public static LinphoneCore getLC() {
		if(null==mInstance){ return null; }
		return mInstance.mLinphoneCore;
	}

完了在activity_main.xml添加显示的文本

   <TextView
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:id="@+id/id_text_status"
        android:textSize="20dip"
        android:text="状态" />

在MainActivity.java添加广播接受者并在onCreate注册

public class MainActivityReceiver extends BroadcastReceiver { 
        @Override
        public void onReceive(Context context, Intent intent) {
             String action = intent.getStringExtra("action");
             switch (action) {
             case "reg_state":
                 id_text_status.setText(intent.getStringExtra("data") );
                break;  
            default:
                break;
            } 
        }
    }

public static final String RECEIVE_MAIN_ACTIVITY = "receive_main_activity";
    private MainActivityReceiver mReceiver;

扫描二维码关注公众号,回复: 5311801 查看本文章

    protected void onCreate(Bundle savedInstanceState) { 

。。。

   //广播
       IntentFilter intentFilter = new IntentFilter(RECEIVE_MAIN_ACTIVITY);
       mReceiver = new MainActivityReceiver();
       registerReceiver(mReceiver, intentFilter); 

。。。

}

再到LinphoneMiniManager.java里面的

@Override
    public void registrationState(LinphoneCore lc, LinphoneProxyConfig cfg,
            RegistrationState cstate, String smessage) {
        //Log.e("lilin Registration state: " + cstate + "(" + smessage + ")");
        Intent intent = new Intent(MainActivity.RECEIVE_MAIN_ACTIVITY);
        intent.putExtra("action", "reg_state");intent.putExtra("data",smessage );
        sendBroadcast( intent); 

}

重新启动就可以看到注册的状态了

2.实现拨打电话

<Button 
            android:layout_width="wrap_content"
            android:layout_marginTop="5dip"
            android:layout_height="wrap_content"
            android:id="@+id/id_btn_boda"
            android:text="语音拨打"
        />

在MainActivity.java添加实现点击

    instance.lilin_call(id_etext_dail.getText().toString(),host,false);

在LinphoneMiniManager.java添加方法

public void     lilin_call(String username,String host,boolean isVideoCall) throws LinphoneCoreException{
        LinphoneAddress address = mLinphoneCore.interpretUrl(username+ "@" + host);
        address.setDisplayName(username);
          
        LinphoneCallParams params = mLinphoneCore.createCallParams(null);
        if (isVideoCall) {
            params.setVideoEnabled(true);
            params.enableLowBandwidth(false);
        } else {
            params.setVideoEnabled(false);
        } 
        LinphoneCall call = mLinphoneCore.inviteAddressWithParams(address, params);
         if (call == null) {
               Log.e("lilin error", "Could not place call to " + username);
               return;
          } 
    }

好了  注册成功后  可以点击拨打了  测试的话找另外一个sip 客户端接听就行了

3. 接听

  <Button 
            android:layout_width="match_parent"
            android:layout_marginTop="5dip"
            android:layout_height="wrap_content"
            android:id="@+id/id_btn_jie"
            android:text="接电话"
        />

点击事件

instance.lilin_jie();

在LinphoneMiniManager.java添加方法

public void lilin_jie() throws LinphoneCoreException{ 
         
        //instance.getLC().setVideoPolicy(true, instance.getLC().getVideoAutoAcceptPolicy());/*设置初始话视频电话,设置了这个你拨号的时候就默认为使用视频发起通话了*/
        getLC().setVideoPolicy(getLC().getVideoAutoInitiatePolicy(), true);/*设置自动接听视频通话的请求,也就是说只要是视频通话来了,直接就接通,不用按键确定,这是我们的业务流,不用理会*/
        /*这是允许视频通话,这个选了false就彻底不能接听或者拨打视频电话了*/
        getLC().enableVideo(true, true);
        LinphoneCall currentCall = getLC().getCurrentCall();
        if (currentCall != null) {
            LinphoneCallParams params = getLC().createCallParams(currentCall);
            getLC().acceptCallWithParams(currentCall,params );     
        } 
    }

铃声需要将

private void initLinphoneCoreValues(String basePath) { 

 。。。
        mLinphoneCore.setRing( basePath + "/oldphone_mono.wav");//null gai

。。。

}

好了  现在可以更具注册的 接听电话

未完待续  更多到博客列表

对此感兴趣的可以加群261074724 

猜你喜欢

转载自blog.csdn.net/Java_lilin/article/details/81868588