首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建在Wifi Hotspot内工作的VOIP应用程序

问创建在Wifi Hotspot内工作的VOIP应用程序
EN

Stack Overflow用户
提问于 2019-11-22 21:49:55
回答 1查看 50关注 0票数 1

使用以下代码:

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity {

    int clientAmount = 0;
    int localPort = 0;
    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        try {

            AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            audio.setMode(AudioManager.MODE_IN_COMMUNICATION);
            final AudioGroup m_AudioGroup = new AudioGroup();
            m_AudioGroup.setMode(AudioGroup.MODE_NORMAL);

            final AudioStream m_AudioStream = new AudioStream(getAPAddress());

            int localPort = m_AudioStream.getLocalPort();


            Log.d("VOIP","Local Port: "+ Integer.toString(localPort));

            m_AudioStream.setCodec(AudioCodec.PCMU);
            m_AudioStream.setMode(RtpStream.MODE_NORMAL);

            findViewById(R.id.connectButton).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    String remoteAddress = "";
                    String remotePort = "";

                    try {
                        remoteAddress = ((TextView) findViewById(R.id.ipField)).getText().toString();
                        //String remoteAddress = fetchIPs().get(0);
                        remotePort = ((EditText) findViewById(R.id.portField)).getText().toString();

                    }catch(Exception e){
                        ((TextView) findViewById(R.id.statusLabel)).setText("Enter Correct Information");
                    }
                    //Log.d("VOIP", fetchIPs().get(0));

                    try {
                        m_AudioStream.associate(InetAddress.getByName(remoteAddress), Integer.parseInt(remotePort));

                        Log.d("VOIP","Succesfully connected to " + remoteAddress);

                    } catch (Exception e) {
                        ((TextView) findViewById(R.id.statusLabel)).setText("Exception with associating");
                        Log.d("VOIP","Exception with associating");
                        Log.d("VOIP", e.getMessage());
                    }
                    m_AudioStream.join(m_AudioGroup);
                }
            });

            findViewById(R.id.DisconnectButton).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    m_AudioStream.release();
                }
            });

        } catch (Exception e) {
            Log.e("VOIP", "Exception when setting up the Audiostream!");
            e.printStackTrace();
        }
    }


    public InetAddress getAPAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();

                if(intf.getName().equals("ap0")) {

                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                        InetAddress inetAddress = enumIpAddr.nextElement();

                        for(int i = 0; i<intf.getInterfaceAddresses().size(); i++){

                            if( intf.getInterfaceAddresses().get(i).getAddress() instanceof Inet4Address){

                                Log.d("VOIP", "IP: " + intf.getInterfaceAddresses().get(i).getAddress() + " NetworkInterface: " + intf.getName());
                                return (Inet4Address) intf.getInterfaceAddresses().get(i).getAddress();
                            }
                        }
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e("VOIP", ex.toString());
        }
        return null;
    }

    public InetAddress getWLANAddress() {
        InetAddress result = null;

        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();


                if(intf.getName().equals("wlan0")) {
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        InterfaceAddress a = intf.getInterfaceAddresses().get(1);

                        for(int i = 0; i<intf.getInterfaceAddresses().size(); i++){

                            if( intf.getInterfaceAddresses().get(i).getAddress() instanceof Inet4Address){
                                Log.d("VOIP", "IP: " + intf.getInterfaceAddresses().get(i).getAddress() + " NetworkInterface: " + intf.getName());
                                result =  intf.getInterfaceAddresses().get(i).getAddress();
                            }
                        }
                        if (!inetAddress.isLoopbackAddress()) {
                            return result;
                        }
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e("VOIP", ex.toString());
        }

        Log.d("VOIP"," Access Point nor Wlan accessible");

        return null;

    }

}

activity_main.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/lblLocalPort"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/localPort" />

    <EditText
        android:id="@+id/ipField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="@string/iPHint"
        android:inputType="phone" />

    <EditText
        android:id="@+id/portField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="@string/portHint"
        android:inputType="number">

        <requestFocus />
    </EditText>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <Button
            android:id="@+id/connectButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/connect" />

        <Button
            android:id="@+id/DisconnectButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Disconnect" />

        <Button
            android:id="@+id/fetchButtons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Fetch IPs" />
    </LinearLayout>

    <TextView
        android:id="@+id/statusLabel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

我想创建一个点对点的对讲机应用程序,允许我在两个设备之间传输音频。一台称为HostDevice的设备创建一个WiFi接入点(热点),另一台设备ClientDevice连接到该接入点。我的理解是,对于HostDevice,我利用接入点网络接口的InetAddress (称为"ap0")发起AudioStream,而对于ClientDevice,我利用wlan网络接口的InetAddress (“InetAddress”)发起AudioStream。(这样做正确吗?)因此,当我将代码上传到HostDevice时,我写道:

代码语言:javascript
复制
final AudioStream m_AudioStream = new AudioStream(getAPAddress());

在客户端设备端,我有相同的代码,除了我像这样启动AudioStream:

代码语言:javascript
复制
final AudioStream m_AudioStream = new AudioStream(getWLANAddress());

当我输入两个设备的IP地址和端口号(如下图所示),并在这两个设备上按下连接时,我在m_AudioStream.associate(InetAddress.getByName(remoteAddress), Integer.parseInt(remotePort));收到一个异常,很明显,我听不到任何来自这两个设备的扬声器的任何声音。

​

This answer from Murphybro2说,这就是两个设备相互通信所需的全部内容。那么,我的理解在哪里崩溃了呢?

EN

回答 1

Stack Overflow用户

发布于 2019-11-24 21:10:01

检查VOIP或对等应用程序是否正常工作的步骤

Step1:检查音频流采集处理是否正常。打印日志或检查特定日志或音频原始转储的logcat (如果可能)。

步骤2:检查音频RTP数据包是否已发送到网络,以及是否发送到正确的IP地址和端口号。pcap文件(用于嗅探wireshark /tcpdump工具)网络数据包。解码为RTP数据包

步骤3:检查接收方接收到的数据是否被正确处理,并使用与捕获时相同的配置进行呈现。打印日志或检查特定日志或音频原始转储的logcat (如果可能)。

请根据以上步骤分享您的观察/结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58995648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档