Research results of SIP audio calls in Android

Source: Internet
Author: User
First, we will briefly introduce the SIP protocol, which is a Session Initiation Protocol and mainly used for network multimedia calls. The sip api can be called only in android2.3 or later versions, and the device must support the sip before making a SIP call.

The APIs used by SIP are mainly stored in android.net. Sip. The core class is sipmanager. java. The diagram is as follows:

The local sipprofile is stored in an XML file, which is mainly used to store the username, domain, and password of the SIP. The sharedpreference configuration file is as follows:

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

The code for updating sharedpreference is as follows:

public void updatePreferences() {        Intent settingsActivity = new Intent(getBaseContext(),                SipSettings.class);        startActivity(settingsActivity);    }

Calling the updatepreferences () method is shown as follows:

 

You can modify the local sipprofile account information on this page.

Update the sipprofile information of the recipient when making a call. The Code is as follows:

/**     * 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());    }

The dialing code is as follows:

/**     * 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();            }        }    }

The answer code is as follows:

// 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);

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();            }        }    }}

Set local SIP:

/** * 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::::::::::");    }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.