我正在使用实时协议进行语音通话android应用程序。我看到了有关android.net.rtp的Android开发文档,并编写了一些代码。 我想使用Firebase来为远程主机的android设备Ip和我的android设备Ip交换Ip地址。然后,将这些IP地址放在“ rtpstream”中,但是它不起作用。因此,我检查了IP地址,然后发现每个设备的IP地址已实时更改。 我的问题是我该如何设置这些IP地址,因为应用程序正常工作,在任何情况下都不会更改。
我找不到有关android.net.rtp的足够实践,所以我不确定我写的接收片段是否正确。我也想知道如何设置远程主机IP和设备的IP。我必须将这些IP地址设置为静态吗? 远程设备和我的设备如何交换其IP地址?
////////////////////////////////////////发送片段////// /////////////////////////////
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_teach, container, false);
final TextView myIP = view.findViewById(R.id.myIp);
final TextView opIP = view.findViewById(R.id.opIp);
Button makeRoomButton = view.findViewById(R.id.fragmentteach_makenewroom);
FirebaseUser teacher = FirebaseAuth.getInstance().getCurrentUser();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference callRef = database.getReference("rooms/0EtpWJLXrUWH0pfCKsFddgL3rSl2/huhuhu/ipp");
callRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ip = dataSnapshot.getValue().toString().getBytes();
opIP.setText(ip.toString());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
AudioGroup audioGroup = new AudioGroup();
audioGroup.setMode(AudioGroup.MODE_NORMAL);
AudioStream audioStream = new AudioStream(InetAddress.getByAddress(getLocalIPAddress ()));
myIP.setText(getLocalIPAddress().toString());
audioStream.setCodec(AudioCodec.PCMU);
audioStream.setMode(RtpStream.MODE_NORMAL);
audioStream.associate(InetAddress.getByAddress(ip), 22222);
audioStream.join(audioGroup);
AudioManager audio = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
audio.setMode(AudioManager.MODE_IN_COMMUNICATION);
} catch (Exception e) {
Log.e("----------------------", e.toString());
e.printStackTrace();
}
makeRoomButton.setOnClickListener(this);
return view;
}
//////////////////////////////////接收片段////////////// ///////////////////
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
AudioGroup audioGroup = new AudioGroup();
audioGroup.setMode(AudioGroup.MODE_NORMAL);
AudioStream audioStream = new AudioStream(InetAddress.getByAddress(getLocalIpAddress ()));
smyIP.setText(getLocalIpAddress().toString());
audioStream.setCodec(AudioCodec.PCMU);
audioStream.setMode(RtpStream.MODE_RECEIVE_ONLY);
audioStream.associate(InetAddress.getByAddress(ip), 22222);
sopIP.setText(ip.toString());
audioStream.join(audioGroup);
AudioManager audio = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
audio.setMode(AudioManager.MODE_IN_COMMUNICATION);
} catch (Exception e) {
Log.e("----------------------", e.toString());
e.printStackTrace();
}
gogo = view.findViewById(R.id.studentText);
final Bundle bundle = this.getArguments();
final DatabaseReference myRef = database.getReference("rooms/0EtpWJLXrUWH0pfCKsFddgL3rSl2/roomName");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.getValue().toString().equals(bundle.getString("key"))){
gogo.setText("ok");
IpModel ip = new IpModel();
ip.ipp = getLocalIpAddress().toString();
myRef.getParent().child("huhuhu").setValue(ip);
}
else
gogo.setText("nooooooooo");
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
return view;
}
////////////////////////////////////////用//////获取设备Ip /////////////////////
public static byte[] getLocalIpAddress () {
byte ip[]=null;
try {
for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
ip= inetAddress.getAddress();
}
}
}
} catch (SocketException ex) {
Log.i("SocketException ", ex.toString());
}
return ip;
}
///////////////////////权限/////// ////////////////////////////
<uses-permission android:name="android.permission.USE_SIP" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature android:name="android.hardware.sip.voip" android:required="true" />
<uses-feature android:name="android.hardware.wifi" android:required="true" />
<uses-feature android:name="android.hardware.microphone" android:required="true" />
这是我在Stack Overflow中的第一个问题,我也不懂英语。我很抱歉语法错误和格式错误。