从Android通过udp广播音频/视频到wifi并用vlc听

时间:2013-07-29 09:57:09

标签: android udp audio-streaming vlc

我正在尝试通过udp从我的Android设备向本地wifi广播音频文件,并让本地网络上的客户端通过VLC的网络流媒体选项收听它。我可以播放它并在连接到网络的任何设备上接收它,如果我使用我自己的接收代码,但我希望VLC能够播放它。在发送数据报包之前是否需要进行任何特定的编码或格式化?

我的发送代码

public void SendAudio()
{
    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
            {
                InetAddress addr = InetAddress.getByName("192.168.1.255");
                DatagramSocket sock = new DatagramSocket();


                while(true){
                    bytes_count=0;
                    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, 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();
}

我的接收代码`

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();

}

再一次,使用这个我可以从一个Android设备发送这个并使用我给出的接收代码听另一个很好,但我想使用vlc的get network stream命令播放它并听取h77p:// @:端口并播放音频。 Tnx再次:))

1 个答案:

答案 0 :(得分:0)

您可以使用此tutorial中的代码:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  StrictMode.setThreadPolicy(policy);
  try {   
      AudioManager audio =  (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
      audio.setMode(AudioManager.MODE_IN_COMMUNICATION);
      AudioGroup audioGroup = new AudioGroup();
      audioGroup.setMode(AudioGroup.MODE_NORMAL);        
      AudioStream audioStream = new AudioStream(InetAddress.getByAddress(getLocalIPAddress ()));
      audioStream.setCodec(AudioCodec.PCMU);
      audioStream.setMode(RtpStream.MODE_NORMAL);
                           //set receiver(vlc player) machine ip address(please update with your machine ip)
      audioStream.associate(InetAddress.getByAddress(new byte[] {(byte)192, (byte)168, (byte)1, (byte)19 }), 22222);
      audioStream.join(audioGroup);


  } catch (Exception e) {
   Log.e("----------------------", e.toString());
   e.printStackTrace();
  }
 }
public static byte[] getLocalIPAddress () {
    byte ip[]=null;
       try {
           for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
               NetworkInterface intf = en.nextElement();
               for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                   InetAddress inetAddress = enumIpAddr.nextElement();
                   if (!inetAddress.isLoopbackAddress()) {
                    ip= inetAddress.getAddress();
                   }
               }
           }
       } catch (SocketException ex) {
           Log.i("SocketException ", ex.toString());
       }
       return ip;

 }