Android 使用 AudioStream 接收音频 RTP
Android Use AudioStream To receive audio RTP
我在尝试在其他 android 设备上接收我的音频流时遇到问题
-我有两个 android phone 其中一个应该通过 rtp 发送语音,另一个应该接收它
这里是发件人代码
package com.mywork.johnathan.zoomsenderapk;
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.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;
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);
try {
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.setMode(AudioManager.MODE_NORMAL);
AudioGroup audioGroup = new AudioGroup();
audioGroup.setMode(AudioGroup.MODE_NORMAL);
AudioStream AudioStream = new AudioStream(InetAddress.getByName(getLocalIpAddress()));
AudioStream.setCodec(AudioCodec.PCMU);
AudioStream.setMode(RtpStream.MODE_SEND_ONLY);
//set receiver(vlc player) machine ip address(please update with your machine ip)
AudioStream.associate(InetAddress.getByName("192.168.1.3"),123);
AudioStream.join(audioGroup);
Log.e("pass",getLocalIpAddress().toString());
} catch (Exception e) {
Log.e("----------------------", e.toString());
e.printStackTrace();
}
}
public String getLocalIpAddress()
{
WifiManager wifiMgr = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
Log.e("-----------ip", Formatter.formatIpAddress(ip));
return Formatter.formatIpAddress(ip);
}
}
但我不知道我的接收器应用程序代码应该如何
我想诀窍就在这一行
AudioStream.setMode(RtpStream.MODE_SEND_ONLY);
提前致谢♥
你尝试了吗?
RtpStream.MODE_RECEIVE_ONLY
浏览 class RtpStream 的定义,您可以在那里找到所有选项。
我在尝试在其他 android 设备上接收我的音频流时遇到问题
-我有两个 android phone 其中一个应该通过 rtp 发送语音,另一个应该接收它
这里是发件人代码
package com.mywork.johnathan.zoomsenderapk;
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.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;
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);
try {
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.setMode(AudioManager.MODE_NORMAL);
AudioGroup audioGroup = new AudioGroup();
audioGroup.setMode(AudioGroup.MODE_NORMAL);
AudioStream AudioStream = new AudioStream(InetAddress.getByName(getLocalIpAddress()));
AudioStream.setCodec(AudioCodec.PCMU);
AudioStream.setMode(RtpStream.MODE_SEND_ONLY);
//set receiver(vlc player) machine ip address(please update with your machine ip)
AudioStream.associate(InetAddress.getByName("192.168.1.3"),123);
AudioStream.join(audioGroup);
Log.e("pass",getLocalIpAddress().toString());
} catch (Exception e) {
Log.e("----------------------", e.toString());
e.printStackTrace();
}
}
public String getLocalIpAddress()
{
WifiManager wifiMgr = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
Log.e("-----------ip", Formatter.formatIpAddress(ip));
return Formatter.formatIpAddress(ip);
}
}
但我不知道我的接收器应用程序代码应该如何
我想诀窍就在这一行
AudioStream.setMode(RtpStream.MODE_SEND_ONLY);
提前致谢♥
你尝试了吗?
RtpStream.MODE_RECEIVE_ONLY
浏览 class RtpStream 的定义,您可以在那里找到所有选项。