5

I'm trying to broadcast a audio file from my android device via udp to the local wifi and have the clients on the local network listen to it via VLC's network streaming option. I can broadcast it and recieve it on anydevice connected to the network if i use my own recieve code, But i want VLC to be able to play it. Is ther any specific encoding or formatting that needs to be done before I send the datagram packet?

My sending code

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

My Recieving Code`

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

}

Once again, using this i can send this from one android device and listen from another just fine using the recieve code ive given, but i want to play it using vlc's get network stream command and listen to h77p://@:port and get the audio playing. Tnx again :)

4

1 回答 1

0

您可以使用本教程中的代码:

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;

 }
于 2017-06-03T18:30:55.340 回答