Chinaunix首页 | 论坛 | 博客
  • 博客访问: 840343
  • 博文数量: 182
  • 博客积分: 1992
  • 博客等级: 上尉
  • 技术积分: 1766
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-18 11:49
文章分类

全部博文(182)

文章存档

2019年(1)

2016年(5)

2015年(29)

2014年(38)

2013年(21)

2012年(36)

2011年(52)

我的朋友

分类: Oracle

2012-05-16 13:47:13


点击(此处)折叠或打开

  1. package rmd.media.StreamingAudio;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.security.InvalidParameterException;

  7. import java.net.InetAddress;
  8. import java.net.DatagramSocket;
  9. import java.net.DatagramPacket;
  10. import java.net.SocketException;
  11. import java.net.UnknownHostException;

  12. import android.app.Activity;
  13. import android.media.AudioFormat;
  14. import android.media.AudioManager;
  15. import android.media.AudioTrack;
  16. import android.os.Bundle;
  17. import android.view.View;
  18. import android.view.View.OnClickListener;
  19. import android.widget.Button;
  20. import android.util.Log;

  21. /** UdpStream activity sends and recv audio data through udp */
  22. public class UdpStream extends Activity {
  23.     /** Called when the activity is first created. */
  24.     @Override
  25.     public void onCreate(Bundle savedInstanceState) {
  26.         super.onCreate(savedInstanceState);
  27.         setContentView(R.layout.udpstream);
  28.         Button btnSend = (Button)findViewById(R.id.btnSend);
  29.         btnSend.setOnClickListener(new OnClickListener() {
  30.             
  31.             @Override
  32.             public void onClick(View v) {
  33.                 Log.d(LOG_TAG, "btnSend clicked");
  34.                 SendAudio();
  35.             }
  36.         });

  37.         Button btnRecv = (Button)findViewById(R.id.btnRecv);
  38.         btnRecv.setOnClickListener(new OnClickListener() {
  39.             
  40.             @Override
  41.             public void onClick(View v) {
  42.                 Log.d(LOG_TAG, "btnRecv clicked");
  43.                 RecvAudio();
  44.             }
  45.         });
  46.         
  47.     }

  48.     static final String LOG_TAG = "UdpStream";
  49.     static final String AUDIO_FILE_PATH = "/sdcard/1.wav";
  50.     static final int AUDIO_PORT = 2048;
  51.     static final int SAMPLE_RATE = 8000;
  52.     static final int SAMPLE_INTERVAL = 20; // milliseconds
  53.     static final int SAMPLE_SIZE = 2; // bytes per sample
  54.     static final int BUF_SIZE = SAMPLE_INTERVAL*SAMPLE_INTERVAL*SAMPLE_SIZE*2;
  55.     
  56.     public void RecvAudio()
  57.     {
  58.         Thread thrd = new Thread(new Runnable() {
  59.             @Override
  60.             public void run()
  61.             {
  62.                 Log.e(LOG_TAG, "start recv thread, thread id: "
  63.                     + Thread.currentThread().getId());
  64.                 AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC,
  65.                         SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
  66.                         AudioFormat.ENCODING_PCM_16BIT, BUF_SIZE,
  67.                         AudioTrack.MODE_STREAM);
  68.                 track.play();
  69.                 try
  70.                 {
  71.                     DatagramSocket sock = new DatagramSocket(AUDIO_PORT);
  72.                     byte[] buf = new byte[BUF_SIZE];

  73.                     while(true)
  74.                     {
  75.                         DatagramPacket pack = new DatagramPacket(buf, BUF_SIZE);
  76.                         sock.receive(pack);
  77.                         Log.d(LOG_TAG, "recv pack: " + pack.getLength());
  78.                         track.write(pack.getData(), 0, pack.getLength());
  79.                     }
  80.                 }
  81.                 catch (SocketException se)
  82.                 {
  83.                     Log.e(LOG_TAG, "SocketException: " + se.toString());
  84.                 }
  85.                 catch (IOException ie)
  86.                 {
  87.                     Log.e(LOG_TAG, "IOException" + ie.toString());
  88.                 }
  89.             } // end run
  90.         });
  91.         thrd.start();
  92.     }

  93.     public void SendAudio()
  94.     {
  95.         Thread thrd = new Thread(new Runnable() {
  96.             @Override
  97.             public void run()
  98.             {
  99.                 Log.e(LOG_TAG, "start send thread, thread id: "
  100.                     + Thread.currentThread().getId());
  101.                 long file_size = 0;
  102.                 int bytes_read = 0;
  103.                 int bytes_count = 0;
  104.                 File audio = new File(AUDIO_FILE_PATH);
  105.                 FileInputStream audio_stream = null;
  106.                 file_size = audio.length();
  107.                 byte[] buf = new byte[BUF_SIZE];
  108.                 try
  109.                 {
  110.                     InetAddress addr = InetAddress.getLocalHost();
  111.                     DatagramSocket sock = new DatagramSocket();
  112.                     audio_stream = new FileInputStream(audio);

  113.                     while(bytes_count < file_size)
  114.                     {
  115.                         bytes_read = audio_stream.read(buf, 0, BUF_SIZE);
  116.                         DatagramPacket pack = new DatagramPacket(buf, bytes_read,
  117.                                 addr, AUDIO_PORT);
  118.                         sock.send(pack);
  119.                         bytes_count += bytes_read;
  120.                         Log.d(LOG_TAG, "bytes_count : " + bytes_count);
  121.                         Thread.sleep(SAMPLE_INTERVAL, 0);
  122.                     }
  123.                 }
  124.                 catch (InterruptedException ie)
  125.                 {
  126.                     Log.e(LOG_TAG, "InterruptedException");
  127.                 }
  128.                 catch (FileNotFoundException fnfe)
  129.                 {
  130.                     Log.e(LOG_TAG, "FileNotFoundException");
  131.                 }
  132.                 catch (SocketException se)
  133.                 {
  134.                     Log.e(LOG_TAG, "SocketException");
  135.                 }
  136.                 catch (UnknownHostException uhe)
  137.                 {
  138.                     Log.e(LOG_TAG, "UnknownHostException");
  139.                 }
  140.                 catch (IOException ie)
  141.                 {
  142.                     Log.e(LOG_TAG, "IOException");
  143.                 }
  144.             } // end run
  145.         });
  146.         thrd.start();
  147.     }
  148.     
  149. }

阅读(530) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~