我一直在研究android RTP一对一音频流。如果我将其关联到相同的设备ip和端口,则代码可以正常工作。
我有发送方的工作代码,可以自行发送和接收
。我想要的是将音频流发送到接收器android应用,然后两部手机上的两个应用都实现基于一对一通话的完整RTP音频流。
我已经尝试使用Inet在audioStream.associate中尝试AudioStream.getRemoteAddress()和AudioStream.getRemotePort()。
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import android.media.audiofx.AudioEffect;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.net.rtp.AudioCodec;
import android.net.rtp.AudioGroup;
import android.net.rtp.AudioStream;
import android.net.rtp.RtpStream;
import android.os.StrictMode;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.media.MediaPlayer;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@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);
TextView t = findViewById(R.id.ip);
try {
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.setMode(AudioManager.MODE_IN_COMMUNICATION);
audio.setSpeakerphoneOn(true);
AudioGroup audioGroup = new AudioGroup();
audioGroup.setMode(AudioGroup.MODE_ECHO_SUPPRESSION);
AudioStream AudioStream = new AudioStream(InetAddress.getByName(this.getLocalIpAddress()));
AudioStream.setCodec(AudioCodec.PCMU);
AudioStream.setMode(RtpStream.MODE_NORMAL);
AudioStream.associate(InetAddress.getByName(this.getLocalIpAddress()),AudioStream.getLocalPort());
t.setText(this.getLocalIpAddress()+":"+AudioStream.getLocalPort());
AudioStream.join(audioGroup);
Log.e("Pass",getLocalIpAddress());
} catch (Exception e) {
Log.e("----------------------", e.toString());
e.printStackTrace();
}
}
public String getLocalIpAddress()
{
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
Log.e("-----------ip", ip);
return ip;
}
}
这是实例的权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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.CALL_PHONE" />
<uses-permission android:name="android.permission.VIBRATE" />
<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" />
我需要有关此代码的帮助,以便我将其安装在两部android手机上,并且它们彼此发送并接收1-1 。 许多人都发布了我知道的相同问题,但没有人回答我的问题。
如果有人可以帮助您,将不胜感激。