logo资料库

sip协议视频语音通话.pdf

第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
资料共6页,全文预览结束
eoe 移动开发者社区 www.eoe.cn 原文地址:http://developer.android.com/intl/ja/guide/topics/connectivity/sip.html 翻译:tyutNo4 更新:2012.06.06 Android提供了⼀个支持会话发起协议(SIP)的API,这可以让你添加基于SIP的网络电话功能到你的应用程序。Android 包括⼀个完整的SIP协议栈和集成的呼叫管理服务,让应用轻松无需管理会话和传输层的沟通就可设置传出和传⼊的语音 通话,或直接音频记录或播放。 以下类型的应用程序可能使用SIP API: 视频会议。 即时消息。 * 条件和限制 条件和限制* 以下是开发⼀个SIP应用程序的条件: * 你必须有⼀个运行Android2.3或者更高版本的移动设备。 * SIP是通过无线数据连 接来运行的,所以你的设备必须有⼀个数据连接(通过移动数据服务或者Wi-Fi)。这意味着你不能在模拟器(AVD)上进 行测试,只能在⼀个物理设备上测试。详情请参见应用程序测试(Testing SIP Applications)。 * 每⼀个参与者在应用程 序的通信会话过程中必须有⼀个SIP账户。有很多不同的SIP服务提供商提供SIP账户。 * SIP API类和接⼝ 类和接⼝* 以下是Android SIP API中包含的⼀些类和⼀个接⼝(SipRegistrationListener)的概述: {|style="border-spacing: 0px;margin: 4px 4px; width: 90%; border-left:1px solid #ccc;border-top:1px solid #ccc; " |-style="background:#DEE8F1; " ! style="border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px;" | 类/接⼝ ! style="border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px;" | 描述 |- style=" vertical-align:top;" | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | SipAudioCall | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | 通过SIP处理网络 音频电话 |- style=" vertical-align:top;" | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | SipAudioCall.Listener | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | 关于SIP电话的事件监听器,比如接受到⼀个电话(on ringing)或者呼出⼀个电话(on calling)的时候 |- style=" vertical- align:top;" | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | SipErrorCode | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | 定义在SIP活动中返回的错误代 码 |- style=" vertical-align:top;" | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | SipManager | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | 为SIP任务提供 APIs,比如初始化⼀个SIP连接。提供相关SIP服务的访问。 |- style=" vertical-align:top;" | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | SipProfile | style=" border-right:1px solid #ccc;border- bottom:1px solid #ccc; padding:5px 15px; " | 定义了SIP的相关属性,包含SIP账户、域名和服务器信息 |- style=" vertical- align:top;" | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | SipProfile.Builder | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | 创建SipProfile的帮助类 |- style=" vertical-align:top;" | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | SipSession | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | 代表⼀个SIP会 话,跟SIP对话框或者⼀个没有对话框的独立事务相关联 |- style=" vertical-align:top;" | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | SipSession.Listener | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | 关于SIP会话的事件监听器,比如注册⼀个会话(on registering)或者呼出⼀个电话(on calling)的时候 |- style=" vertical-align:top;" | style=" border-right:1px solid 2/6
eoe 移动开发者社区 www.eoe.cn #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | SipSession.State | style=" border-right:1px solid #ccc;border- bottom:1px solid #ccc; padding:5px 15px; " | 定义SIP会话的声明,比如“注册”、“呼出电话”、“打⼊电话” |- style=" vertical-align:top;" | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | SipRegistrationListener | style=" border-right:1px solid #ccc;border-bottom:1px solid #ccc; padding:5px 15px; " | ⼀个 关于SIP注册事件监听器的接⼝ |} * 创建创建Manifest文件文件* 如果你开发⼀个用到SIP API的应用程序,记住它需要Android2.3(API9)或者更高版本的平台的支持。所以在你的设备 上要运行Android2.3(API9)或者更高的版本,并不是所有的设备都提供SIP的支持。 为了使用SIP,需要添加以下权限到你的manifest文件: * android.permission.USE_SIP * android.permission.INTERNET 为了确保你的应用程序能够安装到支持SIP的设备上,你需要添加以下内容到你应用程序的manifest文件里: * . 这个设置 表明你的应用程序需要Android2.3或者更高版本的平台。详情请参考API Levels和元素相关的文档。 为了控制你的应用程 序被那些不支持SIP的设备过滤掉(比如:在Google Play),你需要添加以下内容到你应用程序的manifest文件里: * . 这 个设置声明了你的应用程序用到了SIP API。这个声明还应该包含⼀个android:required 属性来表明你是否想让你的应用程 序被那些不提供SIP支持的设备过滤掉。其他声明你也可能需要,具体取决于你的实现,详情请参考 元素相关的文档。 如 果你的应用程序设计用来接受呼叫,那么你还必须在应用程序的manifest文件里定义⼀个接收器(BroadcastReceiver 的 ⼦类): * 以下是从SipDemo项目manifest文件中摘录的内容: ... ... ... * 创建⼀个 创建⼀个SipManager对象对象* 要想使用SIP API,你的应用程序需要创建⼀个SipManager对象,这个SipManager对象在你的应用程序里负责以下内容: * 发起SIP会话 * 发起和接受呼叫 * 在SIP provider里进行注册和注销 * 验证会话的连通性 你可以像下面⼀样实例化⼀个新 的SipManager对象: public SipManager mSipManager = null; ... if(mSipManager == null) { mSipManager = SipManager.newInstance(this); } * 在在SIP服务器上进行注册 服务器上进行注册* ⼀个典型的Android SIP应用中包含⼀个或多个用户,他们中的每个⼈都有⼀个SIP账户。在Android SIP应用中,每⼀个 SIP账户代表⼀个SipProfile对象。 ⼀个SipProfile对象定义了⼀个SIP的概要文件,包括SIP账户、域名和服务器信息。跟正在这个设备上运行应用的SIP账户 相关联的概要文件被称之为本地配置文件。与会话相连接的概要文件被称之为对应配置文件。当你的SIP应用通过本地 SipProfile登录到SIP服务器的时候,这就有效的注册当前设备为基站来发送SIP呼叫到你想呼叫的SIP地址。 本节展示了如何创建⼀个SipProfile,以及如何把刚创建的SipProfile注册到SIP服务器上,并且跟踪注册事件。 你可以像以 下⼀样创建⼀个SipProfile对象: public SipProfile mSipProfile = null; ... SipProfile.Builder builder = new SipProfile.Builder(username, domain); builder.setPassword(password); mSipProfile = builder.build(); 接下来的代码摘录本地配置文件,用于呼出电话和/或接收通用的SIP电话。呼叫器可以通过 mSipManager.makeAudioCall来呼出后续电话。这段摘录同样设置了⼀个android.SipDemo.INCOMING_CALL行动,这 个行动会被⼀个intent过滤器来使用,当前设备接收到⼀个呼叫(见Setting up an intent filter to receive calls)。以下是 注册步骤: Intent intent = new Intent(); intent.setAction("android.SipDemo.INCOMING_CALL"); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA); mSipManager.open(mSipProfile, pendingIntent, null); 最后这段代码在SipManager上设置了⼀个SipRegistrationListener 监听器,这个监听器会跟踪 SipProfile是否成功的注册到你的SIP服务提供者。 mSipManager.setRegistrationListener(mSipProfile.getUriString(), 3/6
eoe 移动开发者社区 www.eoe.cn new SipRegistrationListener() { public void onRegistering(String localProfileUri) { updateStatus("Registering with SIP Server..."); } public void onRegistrationDone(String localProfileUri, long expiryTime) { updateStatus("Ready"); } public void onRegistrationFailed(String localProfileUri, int errorCode, String errorMessage) { updateStatus("Registration failed. Please check settings."); } 当你的应用程序使用完⼀个profile的时候,你应该关闭它来 释放相关联的对象到内存中以及从服务器上注销当前设备。例如: public void closeLocalProfile() { if (mSipManager == null) { return; } try { if (mSipProfile != null) { mSipManager.close(mSipProfile.getUriString()); } } catch (Exception ee) { Log.d("WalkieTalkieActivity/onDestroy", "Failed to close local profile.", ee); } } * 拨打⼀个语音电话 拨打⼀个语音电话* 要想拨打⼀个语音电话,你需要准备如下条件: * ⼀个发起呼叫电话的SipProfile对象(本地配置文件)和⼀个用来接收呼 叫的有效的SIP地址(对应配置文件) * ⼀个SipManager对象 要想拨打⼀个语音电话,你应该建立⼀个 SipAudioCall.Listener监听器。⼤部分客户与SIP堆栈的交互都是通过监听器来发生的。在这⼀⼩段你将会看到 SipAudioCall.Listener监听器是如何在呼叫制定之后建立事务的: SipAudioCall.Listener listener = new SipAudioCall.Listener() { }; ⼀旦你创建了这个SipAudioCall.Listener监听器,你就可以拨打电话了,SipManager对象里的makeAudioCall方法接受 以下参数: * ⼀个本地SIP配置文件(呼叫方) * ⼀个相对应的SIP配置文件(被呼叫方) * ⼀个用来监听从SipAudioCall 发出的呼叫事件的SipAudioCall.Listener,这个参数可以为null,但是如上所说,⼀旦呼叫电话制定,这个监听器将被用来 创建事务 * 超时的值,以秒为单位 例如: call = mSipManager.makeAudioCall(mSipProfile.getUriString(), sipAddress, listener, 30); * 接收呼叫 接收呼叫* 为了接收呼叫,SIP应用程序必须包含⼀个BroadcastReceiver的⼦类,这个⼦类得有能⼒响应⼀个表明有来电的intent。 因此你需要在你的应用程序里做如下事情: * 在AndroidManifest.xml文件中声明⼀个元素。在SipDemo项目中,元素是这 样的 * 实现BroadcastReceiver的⼦类,在SipDemo中,这个⼦类是IncomingCallReceiver * 通过挂起⼀个intent来初始化 本地配置文件(SipProfile),当有⼈呼叫你的时候,这个挂起的intent会调用你的接收器。 * 创建⼀个intent过滤器,这个 过滤器通过标志着来电的行动来进行过滤。在SipDemo中,这个action是android.SipDemo.INCOMING_CALL。 * 实 现实 现 BroadcastReceiver的 ⼦ 类的 ⼦ 类 * 为了接收呼叫,你的SIP应用必须实现BroadcastReceiver的⼦类。当Android系统接收到⼀个呼叫的时候,他会处理这个 SIP呼叫,然后广播⼀个来电intent(这个intent由系统来定义),以下是SipDemo中实现BroadcastReceiver⼦类的代码。 如果想查看完整的例⼦,你可以去SipDemo Sample项目,这个项目在SDK的samples文件夹中。关于下载和安装SDK samples,请参考 Getting the Samples。 /* * * Listens for incoming SIP calls, intercepts and hands them off to WalkieTalkieActivity. * / public class IncomingCallReceiver extends BroadcastReceiver { /* * * Processes the incoming call, answers it, and hands it over to the * WalkieTalkieActivity. * @param context The context under which the receiver is running. * @param intent The intent being received. * / @Override public void onReceive(Context context, Intent intent) { SipAudioCall incomingCall = null; try { SipAudioCall.Listener listener = new SipAudioCall.Listener() { @Override public void onRinging(SipAudioCall call, SipProfile caller) { try { call.answerCall(30); } catch (Exception e) { e.printStackTrace(); } } }; WalkieTalkieActivity wtActivity = (WalkieTalkieActivity) context; incomingCall = wtActivity.mSipManager.takeAudioCall(intent, listener); incomingCall.answerCall(30); incomingCall.startAudio(); incomingCall.setSpeakerMode(true); if(incomingCall.isMuted()) { incomingCall.toggleMute(); } wtActivity.call = 4/6
eoe 移动开发者社区 www.eoe.cn incomingCall; wtActivity.updateStatus(incomingCall); } catch (Exception e) { if (incomingCall != null) { incomingCall.close(); } } } } * 创建⼀个用来接收呼叫的 创建⼀个用来接收呼叫的intent过滤器过滤器* 当SIP服务接收到⼀个新的呼叫的时候,他会发送⼀个intent,这个intent会附带⼀个由应用程序提供的action。在SipDemo 项目中,这个action是android.SipDemo.INCOMING_CALL。 以下从SipDemo中摘录的代码展示了如何通过挂起⼀个基于android.SipDemo.INCOMING_CALL action的intent来创建 SipProfile对象的。PendingIntent对象将执行⼀个广播当SipProfile接收到⼀个呼叫的时候: public SipManager mSipManager = null; public SipProfile mSipProfile = null; ... Intent intent = new Intent(); intent.setAction("android.SipDemo.INCOMING_CALL"); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA); mSipManager.open(mSipProfile, pendingIntent, null); 上面被执行的广播如果被intent过滤器拦截的话,这个intent过滤器将会启动声明过的 Receiver(IncomingCallReceiver)。你可以在你的应用程序里的manifest文件中指定⼀个intent过滤器,或者通过代码来 指定⼀个intent过滤器,就像SipDemo项目中Activity中的onCreate()方法⼀样: public class WalkieTalkieActivity extends Activity implements View.OnTouchListener { ... public IncomingCallReceiver callReceiver; ... } * 测试测试SIP应用程序 应用程序* 要测试SIP应用程序的话,你需要以下条件: * ⼀个运行Android2.3或者更高版本的移动设备。SIP通过无线来运行,所以 你必须在⼀个真正的设备上测试,在AVD上是测试是行不通的 * ⼀个SIP账户,有很多不同的提供SIP账户的SIP服务提供 商。 * 如果你要打电话,这个电话必须是有效的SIP账户。 测试⼀个SIP应用程序的步骤: * 让你的设备连接到无线(设置 >无线&网络>Wi-Fi>Wi-Fi设置) * 设置你的移动设备进行测试,就像在Developing on a Device里描述的⼀样 * 在你的移 动设备上运行程序,就像在Developing on a Device里描述的⼀样 * 如果你正在使用Eclipse,你可以在Eclipse中查看应用 程序的日志输出(* Window > Show View > Other > Android > LogCat* )。 贡献者: tyutNo4 iceskysl tyutNo4 iceskysl ©eoe移动开发者社区 本文链接: http://wiki.eoe.cn/page/SIP 5/6
eoe 移动开发者社区 www.eoe.cn 6/6
分享到:
收藏