创建可在Wifi热点内使用的VOIP应用

问题描述 投票:0回答:1

使用此代码:

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:

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

我想创建一个对等的Walkie Talkie应用程序,该应用程序允许我在两个设备之间传输音频。一个设备称为HostDevice,创建一个WiFi接入点(热点),另一个设备ClientDevice连接到它。我的理解是,使用HostDevice,我使用接入点网络接口的InetAddress(称为“ ap0”)启动AudioStream,而使用ClientDevice,我使用wlan网络接口(“ wlan0”)的InetAddress初始化AudioStream。 (这正确吗?)因此,当我将代码上传到HostDevice时,我写:

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

并且在ClientDevice端,我有相同的代码,除了我这样启动AudioStream:

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

[当我输入下图所示的两个设备的IP地址和端口号,然后在任一设备上按Connect时,我在m_AudioStream.associate(InetAddress.getByName(remoteAddress), Integer.parseInt(remotePort));处出现异常,很显然,我听不到扬声器的任何声音从任一设备。enter image description here

This answer from Murphybro2说这就是两个设备相互通信所需的全部。那么我的理解力在哪里分解?

android audio-streaming hotspot rpt
1个回答
0
投票

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

步骤1:检查音频流是否已正确捕获和处理。打印日志或检查logcat中的特定日志或音频原始转储(如果可能)。

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

[步骤3:检查接收器接收到的数据是否得到了正确处理并以与捕获时相同的配置进行渲染。打印日志或检查logcat中的特定日志或音频原始转储(如果可能)。

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

© www.soinside.com 2019 - 2024. All rights reserved.