12

我正在尝试使用 android.net.rtp 包的 AudioGroup 和 AudioStream 类来实现 VoIP 应用程序。但我的应用程序无法正常运行。将“AudioGroup”类对象与“AudioStream”对象“加入”后,成功发送udp数据包。我使用数据包分析器进行了检查。但是电话听不到声音。我在 2 部手机中运行我的应用程序并尝试在它们之间进行语音通信。

在下面我提到了我的源代码。

public class MainActivity extends Activity {
private AudioStream audioStream;
private AudioGroup audioGroup;

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

try {
   audioGroup = new AudioGroup();
   audioGroup.setMode(AudioGroup.MODE_NORMAL);        
   audioStream = new AudioStream(InetAddress.getByAddress(new byte[] {(byte)192, (byte)168, (byte)1, (byte)4 }));
   audioStream.setCodec(AudioCodec.PCMU);
   audioStream.setMode(RtpStream.MODE_NORMAL);
   audioStream.associate(InetAddress.getByAddress(new byte[] {(byte)192, (byte)168, (byte)1, (byte)2 }), 5004);
   audioStream.join(audioGroup);
   AudioManager Audio =  (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
   Audio.setMode(AudioManager.MODE_IN_COMMUNICATION);
} 
catch (SocketException e) { e.printStackTrace();} 
catch (UnknownHostException e) { e.printStackTrace();} 
catch (Exception ex) { ex.printStackTrace();}
}

我在清单文件中设置了这个权限。

<uses-permission android:name="android.permission.USE_SIP" />
<uses-permission android:name="android.permission.INTERNET" />
<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 4.0 操作系统的三星 GALAXY S3 手机

4

3 回答 3

6

诀窍是让端口映射正确。您需要使用来自 audioStream.getLocalPort() 的端口号,并将此端口号作为 SIP 信令发送到 SDP 数据包中的对等方。

查看实现 sip 功能的示例应用程序 https://github.com/Mobicents/restcomm-android-sdk/tree/master/Examples/JAIN%20SIP

于 2014-10-28T18:18:09.263 回答
5

I used the same code you submitted, and got it working with minor changes. Basically i found the problem was getting the port number correct.

When creating the audioStream the port number seems to be random. At Android developer I found: Note that the local port is assigned automatically to conform with RFC 3550.

What I did was I started the application on one phone first and used audioStream.getLocalPort() to find the port number. Then I connected to this port using the other one. This resulted in two-way communication, even if i only had the correct port number on one phone.

Hope this helps.

于 2012-08-28T13:30:35.013 回答
1

我认为你应该打开扬声器!

也许您可以使用以下方法:

audioManager.setSpeakerphoneOn(true);
于 2013-08-09T01:51:54.990 回答