私は大学のプロジェクトのためにAndroidのピアツーピアツーボイスコールアプリケーションに取り組んでいます。UDPを介してマイクオーディオをストリーミングするためのサンプルコードを見つけました。そのコードは、UDPを介してWAVファイルをストリーミングするのに役立ちます。ただし、一部のファイルは正しく再生されません。また、そのコードはマイクオーディオをストリーミングしません。私を助けてください。
public class UdpStream extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.udpstream);
Button btnSend = (Button)findViewById(R.id.btnSend);
btnSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(LOG_TAG, "btnSend clicked");
SendAudio();
}
});
Button btnRecv = (Button)findViewById(R.id.btnRecv);
btnRecv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(LOG_TAG, "btnRecv clicked");
RecvAudio();
}
});
Button btnStrmic = (Button)findViewById(R.id.btnStrmic);
btnStrmic.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(LOG_TAG, "btnStrmic clicked");
SendMicAudio();
}
});
}
static final String LOG_TAG = "UdpStream";
static final String AUDIO_FILE_PATH = "/sdcard/1.wav";
static final int AUDIO_PORT = 2048;
static final int SAMPLE_RATE = 8000;
static final int SAMPLE_INTERVAL = 20; // milliseconds
static final int SAMPLE_SIZE = 2; // bytes per sample
static final int BUF_SIZE = SAMPLE_INTERVAL*SAMPLE_INTERVAL*SAMPLE_SIZE*2;
protected String host="localhost";
public void RecvAudio()
{
Thread thrd = new Thread(new Runnable() {
@Override
public void run()
{
Log.e(LOG_TAG, "start recv thread, thread id: "
+ Thread.currentThread().getId());
AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC,
SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, BUF_SIZE,
AudioTrack.MODE_STREAM);
track.play();
try
{
DatagramSocket sock = new DatagramSocket(AUDIO_PORT);
byte[] buf = new byte[BUF_SIZE];
while(true)
{
DatagramPacket pack = new DatagramPacket(buf, BUF_SIZE);
sock.receive(pack);
Log.d(LOG_TAG, "recv pack: " + pack.getLength());
track.write(pack.getData(), 0, pack.getLength());
}
}
catch (SocketException se)
{
Log.e(LOG_TAG, "SocketException: " + se.toString());
}
catch (IOException ie)
{
Log.e(LOG_TAG, "IOException" + ie.toString());
}
} // end run
});
thrd.start();
}
Toast toast;
int port=3008;
public void SendAudio()
{
toast=Toast.makeText(getApplicationContext(), port + " : " + host , Toast.LENGTH_SHORT);
Thread thrd = new Thread(new Runnable() {
@Override
public void run()
{
Log.e(LOG_TAG, "start send thread, thread id: "
+ Thread.currentThread().getId());
long file_size = 0;
int bytes_read = 0;
int bytes_count = 0;
File audio = new File(AUDIO_FILE_PATH);
FileInputStream audio_stream = null;
file_size = audio.length();
byte[] buf = new byte[BUF_SIZE];
try
{
EditText d= (EditText) findViewById(R.id.txttohost);
host=d.getText().toString();
if(host=="")
{
port=2048;
host="localhost";
}
port=2048;
InetAddress addr = InetAddress.getByName(host);
toast.setText(port + " : " + addr.toString());
toast.show();
DatagramSocket sock = new DatagramSocket();
audio_stream = new FileInputStream(audio);
while(bytes_count < file_size)
{
bytes_read = audio_stream.read(buf, 0, BUF_SIZE);
DatagramPacket pack = new DatagramPacket(buf, bytes_read,
addr, port);
sock.send(pack);
bytes_count += bytes_read;
Log.d(LOG_TAG, "bytes_count : " + bytes_count);
Thread.sleep(SAMPLE_INTERVAL, 0);
}
}
catch (InterruptedException ie)
{
Log.e(LOG_TAG, "InterruptedException");
}
catch (FileNotFoundException fnfe)
{
Log.e(LOG_TAG, "FileNotFoundException");
}
catch (SocketException se)
{
Log.e(LOG_TAG, "SocketException");
}
catch (UnknownHostException uhe)
{
Log.e(LOG_TAG, "UnknownHostException");
}
catch (IOException ie)
{
Log.e(LOG_TAG, "IOException");
}
} // end run
});
thrd.start();
}
public void SendMicAudio()
{
Thread thrd = new Thread(new Runnable() {
@Override
public void run()
{
Log.e(LOG_TAG, "start SendMicAudio thread, thread id: "
+ Thread.currentThread().getId());
AudioRecord audio_recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, SAMPLE_RATE,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
AudioRecord.getMinBufferSize(SAMPLE_RATE,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT) * 10);
int bytes_read = 0;
int bytes_count = 0;
byte[] buf = new byte[BUF_SIZE];
try
{
InetAddress addr = InetAddress.getLocalHost();
DatagramSocket sock = new DatagramSocket();
while(true)
{
bytes_read = audio_recorder.read(buf, 0, BUF_SIZE);
DatagramPacket pack = new DatagramPacket(buf, bytes_read,
addr, AUDIO_PORT);
sock.send(pack);
bytes_count += bytes_read;
Log.d(LOG_TAG, "bytes_count : " + bytes_count);
Thread.sleep(SAMPLE_INTERVAL, 0);
}
}
catch (InterruptedException ie)
{
Log.e(LOG_TAG, "InterruptedException");
}
// catch (FileNotFoundException fnfe)
// {
// Log.e(LOG_TAG, "FileNotFoundException");
// }
catch (SocketException se)
{
Log.e(LOG_TAG, "SocketException");
}
catch (UnknownHostException uhe)
{
Log.e(LOG_TAG, "UnknownHostException");
}
catch (IOException ie)
{
Log.e(LOG_TAG, "IOException");
}
} // end run
});
thrd.start();
}
}