SIP音调通话调研结果

android中Sip音频通话调研结果       

        分类:            android移动开发 1539人阅读 评论(0) 收藏 举报
  1. <span style="font-family: Arial, Verdana, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">     首先简单介绍一下sip协议,sip是会话启动协议,主要用于网络多媒体通话。必须是android2.3或其以上版本才可以调用Sip api,并且设备必须支持sip才可以进行sip通话。</span> 
     首先简单介绍一下sip协议,sip是会话启动协议,主要用于网络多媒体通话。必须是android2.3或其以上版本才可以调用Sip api,并且设备必须支持sip才可以进行sip通话。

 

SIP使用的api主要放在android.net.sip中,其中最核心的类为SipManager.java,关系图如下所示:

        本地的SipProfile存放在一个xml文件中,主要用于保存sip的username,domain和password,SharedPreference配置文件如下所示:

 

  1. <PreferenceScreenxmlns:android="http://schemas.android.com/apk/res/android"> 
  2.     <EditTextPreference 
  3.         android:name="SIP Username" 
  4.         android:summary="Username for your SIP Account" 
  5.         android:defaultValue="" 
  6.         android:title="Enter Username" 
  7.         android:key="namePref"/> 
  8.     <EditTextPreference 
  9.         android:name="SIP Domain" 
  10.         android:summary="Domain for your SIP Account" 
  11.         android:defaultValue="" 
  12.         android:title="Enter Domain" 
  13.         android:key="domainPref"/> 
  14.     <EditTextPreference 
  15.         android:name="SIP Password" 
  16.         android:summary="Password for your SIP Account" 
  17.         android:defaultValue="" 
  18.         android:title="Enter Password" 
  19.         android:key="passPref" 
  20.         android:password="true"  
  21.         /> 
  22. </PreferenceScreen> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <EditTextPreference
        android:name="SIP Username"
        android:summary="Username for your SIP Account"
        android:defaultValue=""
        android:title="Enter Username"
        android:key="namePref" />
    <EditTextPreference
        android:name="SIP Domain"
        android:summary="Domain for your SIP Account"
        android:defaultValue=""
        android:title="Enter Domain"
        android:key="domainPref" />
    <EditTextPreference
        android:name="SIP Password"
        android:summary="Password for your SIP Account"
        android:defaultValue=""
        android:title="Enter Password"
        android:key="passPref"
        android:password="true" 
        />
</PreferenceScreen>

更新SharedPreference的代码如下:

 

  1. publicvoid updatePreferences() { 
  2.         Intent settingsActivity = new Intent(getBaseContext(), 
  3.                 SipSettings.class); 
  4.         startActivity(settingsActivity); 
  5.     } 
public void updatePreferences() {
        Intent settingsActivity = new Intent(getBaseContext(),
                SipSettings.class);
        startActivity(settingsActivity);
    }

调用updatePreferences()方法会有如下的效果图显示:

  1. <img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/efe590150cfef69385fcd6799a68ba24.png"

 

通过此界面可以修改本地的SipProfile帐号信息。

接打电话时需要更新对方SipProfile的信息,代码如下:

 

  1. /**
  2.      * Updates the status box at the top of the UI with a messege of your choice.
  3.      * @param status The String to display in the status box.
  4.      */ 
  5.     publicvoid updateStatus(final String status) { 
  6.         // Be a good citizen.  Make sure UI changes fire on the UI thread. 
  7.         this.runOnUiThread(new Runnable() { 
  8.             publicvoid run() { 
  9.                 TextView labelView = (TextView) findViewById(R.id.sipLabel); 
  10.                 labelView.setText(status); 
  11.             } 
  12.         }); 
  13.     } 
  14.  
  15.     /**
  16.      * Updates the status box with the SIP address of the current call.
  17.      * @param call The current, active call.
  18.      */ 
  19.     publicvoid updateStatus(SipAudioCall call) { 
  20.         String useName = call.getPeerProfile().getDisplayName(); 
  21.         if(useName == null) { 
  22.           useName = call.getPeerProfile().getUserName(); 
  23.         } 
  24.         updateStatus(useName + "@" + call.getPeerProfile().getSipDomain()); 
  25.     } 
/**
     * Updates the status box at the top of the UI with a messege of your choice.
     * @param status The String to display in the status box.
     */
    public void updateStatus(final String status) {
        // Be a good citizen.  Make sure UI changes fire on the UI thread.
        this.runOnUiThread(new Runnable() {
            public void run() {
                TextView labelView = (TextView) findViewById(R.id.sipLabel);
                labelView.setText(status);
            }
        });
    }

    /**
     * Updates the status box with the SIP address of the current call.
     * @param call The current, active call.
     */
    public void updateStatus(SipAudioCall call) {
        String useName = call.getPeerProfile().getDisplayName();
        if(useName == null) {
          useName = call.getPeerProfile().getUserName();
        }
        updateStatus(useName + "@" + call.getPeerProfile().getSipDomain());
    }

拨电话代码如下:

 

 

  1. /**
  2.      * Make an outgoing call.
  3.      */ 
  4.     publicvoid initiateCall() { 
  5.  
  6.         updateStatus(sipAddress); 
  7.  
  8.         try
  9.             SipAudioCall.Listener listener = new SipAudioCall.Listener() { 
  10.                 // Much of the client's interaction with the SIP Stack will 
  11.                 // happen via listeners.  Even making an outgoing call, don't 
  12.                 // forget to set up a listener to set things up once the call is established. 
  13.                 @Override 
  14.                 publicvoid onCallEstablished(SipAudioCall call) { 
  15.                     call.startAudio(); 
  16.                     call.setSpeakerMode(true); 
  17.                     call.toggleMute(); 
  18.                     updateStatus(call); 
  19.                 } 
  20.  
  21.                 @Override 
  22.                 publicvoid onCallEnded(SipAudioCall call) { 
  23.                     updateStatus("Ready."); 
  24.                 } 
  25.             }; 
  26.  
  27.             call = manager.makeAudioCall(me.getUriString(), sipAddress, listener, 30); 
  28.  
  29.         } 
  30.         catch (Exception e) { 
  31.             Log.i("WalkieTalkieActivity/InitiateCall", "Error when trying to close manager.", e); 
  32.             if (me != null) { 
  33.                 try
  34.                     manager.close(me.getUriString()); 
  35.                 } catch (Exception ee) { 
  36.                     Log.i("WalkieTalkieActivity/InitiateCall"
  37.                             "Error when trying to close manager.", ee); 
  38.                     ee.printStackTrace(); 
  39.                 } 
  40.             } 
  41.             if (call != null) { 
  42.                 call.close(); 
  43.             } 
  44.         } 
  45.     } 
/**
     * Make an outgoing call.
     */
    public void initiateCall() {

        updateStatus(sipAddress);

        try {
            SipAudioCall.Listener listener = new SipAudioCall.Listener() {
                // Much of the client's interaction with the SIP Stack will
                // happen via listeners.  Even making an outgoing call, don't
                // forget to set up a listener to set things up once the call is established.
                @Override
                public void onCallEstablished(SipAudioCall call) {
                    call.startAudio();
                    call.setSpeakerMode(true);
                    call.toggleMute();
                    updateStatus(call);
                }

                @Override
                public void onCallEnded(SipAudioCall call) {
                    updateStatus("Ready.");
                }
            };

            call = manager.makeAudioCall(me.getUriString(), sipAddress, listener, 30);

        }
        catch (Exception e) {
            Log.i("WalkieTalkieActivity/InitiateCall", "Error when trying to close manager.", e);
            if (me != null) {
                try {
                    manager.close(me.getUriString());
                } catch (Exception ee) {
                    Log.i("WalkieTalkieActivity/InitiateCall",
                            "Error when trying to close manager.", ee);
                    ee.printStackTrace();
                }
            }
            if (call != null) {
                call.close();
            }
        }
    }

接电话代码如下:

 

 

  1. // Set up the intent filter.  This will be used to fire an 
  2.         // IncomingCallReceiver when someone calls the SIP address used by this 
  3.         // application. 
  4.         IntentFilter filter = new IntentFilter(); 
  5.         filter.addAction("android.SipDemo.INCOMING_CALL"); 
  6.         callReceiver = new IncomingCallReceiver(); 
  7.         this.registerReceiver(callReceiver, filter); 
// Set up the intent filter.  This will be used to fire an
        // IncomingCallReceiver when someone calls the SIP address used by this
        // application.
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.SipDemo.INCOMING_CALL");
        callReceiver = new IncomingCallReceiver();
        this.registerReceiver(callReceiver, filter);

 

  1. package com.example.android.sip; 
  2.  
  3. import android.content.BroadcastReceiver; 
  4. import android.content.Context; 
  5. import android.content.Intent; 
  6. import android.net.sip.*; 
  7.  
  8. /**
  9. * Listens for incoming SIP calls, intercepts and hands them off to WalkieTalkieActivity.
  10. */ 
  11. publicclass IncomingCallReceiver extends BroadcastReceiver { 
  12.     /**
  13.      * Processes the incoming call, answers it, and hands it over to the
  14.      * WalkieTalkieActivity.
  15.      * @param context The context under which the receiver is running.
  16.      * @param intent The intent being received.
  17.      */ 
  18.     @Override 
  19.     publicvoid onReceive(Context context, Intent intent) { 
  20.         System.out.println("IncomingCallReceiver::::::"); 
  21.         SipAudioCall incomingCall = null
  22.         try
  23.             SipAudioCall.Listener listener = new SipAudioCall.Listener() { 
  24.                 @Override 
  25.                 publicvoid onRinging(SipAudioCall call, SipProfile caller) { 
  26.                     try
  27.                         call.answerCall(30); 
  28.                     } catch (Exception e) { 
  29.                         e.printStackTrace(); 
  30.                     } 
  31.                 } 
  32.             }; 
  33.  
  34.             WalkieTalkieActivity wtActivity = (WalkieTalkieActivity) context; 
  35.  
  36.             incomingCall = wtActivity.manager.takeAudioCall(intent, listener); 
  37.             incomingCall.answerCall(30); 
  38.             incomingCall.startAudio(); 
  39.             incomingCall.setSpeakerMode(true); 
  40.             if(incomingCall.isMuted()) { 
  41.                 incomingCall.toggleMute(); 
  42.             } 
  43.  
  44.             wtActivity.call = incomingCall; 
  45.  
  46.             wtActivity.updateStatus(incomingCall); 
  47.  
  48.         } catch (Exception e) { 
  49.             if (incomingCall != null) { 
  50.                 incomingCall.close(); 
  51.             } 
  52.         } 
  53.     } 
package com.example.android.sip;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.sip.*;

/**
 * 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) {
    	System.out.println("IncomingCallReceiver::::::");
        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.manager.takeAudioCall(intent, listener);
            incomingCall.answerCall(30);
            incomingCall.startAudio();
            incomingCall.setSpeakerMode(true);
            if(incomingCall.isMuted()) {
                incomingCall.toggleMute();
            }

            wtActivity.call = incomingCall;

            wtActivity.updateStatus(incomingCall);

        } catch (Exception e) {
            if (incomingCall != null) {
                incomingCall.close();
            }
        }
    }
}

 

 

设置本地sip:

  1. /**
  2. * Handles SIP authentication settings for the Walkie Talkie app.
  3. */ 
  4. publicclass SipSettings extends PreferenceActivity { 
  5.  
  6.  
  7.     @Override 
  8.     publicvoid onCreate(Bundle savedInstanceState) { 
  9.         // Note that none of the preferences are actually defined here. 
  10.         // They're all in the XML file res/xml/preferences.xml. 
  11.         super.onCreate(savedInstanceState); 
  12.         addPreferencesFromResource(R.xml.preferences); 
  13.         System.out.println("SipSettings::::::::::"); 
  14.     } 
/**
 * Handles SIP authentication settings for the Walkie Talkie app.
 */
public class SipSettings extends PreferenceActivity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        // Note that none of the preferences are actually defined here.
        // They're all in the XML file res/xml/preferences.xml.
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        System.out.println("SipSettings::::::::::");
    }
}

转载于:https://www.cnblogs.com/sy171822716/archive/2012/08/16/2642187.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值