2013-07-29 55 views
5

Tôi đang cố phát một tệp âm thanh từ thiết bị Android của mình qua udp đến wifi cục bộ và yêu cầu khách hàng trên mạng cục bộ nghe qua mạng VLC tùy chọn phát trực tuyến. Tôi có thể phát nó và nhận nó trên anydevice kết nối với mạng nếu tôi sử dụng mã nhận của riêng tôi, Nhưng tôi muốn VLC để có thể chơi nó. Có bất kỳ mã hóa hoặc định dạng cụ thể nào cần được thực hiện trước khi tôi gửi gói datagram không?phát sóng âm thanh/video từ android qua udp tới wifi và nghe với vlc

gửi mã My

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

} 

Một lần nữa, sử dụng này tôi có thể gửi thông điệp này từ một thiết bị Android và lắng nghe từ một tốt bằng cách sử dụng ive đang Nhận các trao , nhưng tôi muốn chơi nó bằng cách sử dụng lệnh vlc's network stream và lắng nghe h77p: // @: port và phát âm thanh. Tnx một lần nữa :)

Trả lời

0

Bạn có thể sử dụng mã từ tutorial này:

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; 

} 
Các vấn đề liên quan