2012-03-19 23 views
6

Android cihazında gerçek zamanlı ses akışına sahip bir fonksiyona sahip olmak istiyorum, bu da cihazın MIC ile sesi yakalayıp sunucuya gönderiyor. Kayıttan sonra bir ses dosyası göndermeyi biliyorum ama gerçek zamanlı olarak yardıma ihtiyacım var. Sunucuya sürekli bayt dizisi göndererek yapılabilir. Eğer öyleyse ya da başka bir şekilde, lütfen fikirlerinizi paylaşın. Teşekkürler.Streaming Real time Audio

Edit-

Android Müşteri Kodu: -

public class Main extends Activity { 
    private MediaRecorder recorder; 

    private final String TAG = "AudioTest"; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     String hostname = "192.168.50.25"; 
     int port = 2004; 

     Socket socket = null; 
     try { 
      socket = new Socket(InetAddress.getByName(hostname), port); 

     } catch (UnknownHostException e) { 

      Log.d(TAG, "Inside [email protected]@@@@@@@@@@@@@@@@@@@@@"); 

      e.printStackTrace(); 
     } catch (IOException e) { 
      Log.d(TAG, "Inside IOException%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"); 

      e.printStackTrace(); 
     } 

     ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket); 

     recorder = new MediaRecorder(); 
     recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 

     recorder.setOutputFile(pfd.getFileDescriptor()); 


     try { 
      Log.i(TAG, pfd.getFileDescriptor().toString()); 
     } catch (Exception e) { 
      Log.d(TAG, "Inside MyException################################"); 
     } 

     try { 
      recorder.prepare(); 
     } catch (IllegalStateException e) { 

      e.printStackTrace(); 
     } catch (IOException e) { 

      e.printStackTrace(); 
     } 

     recorder.start(); 

    } 

JAVA Sunucu Kodunuzu

public class Provider { 
    ServerSocket providerSocket; 
    Socket connection = null; 
    ObjectOutputStream out; 
    ObjectInputStream in; 
    String message; 

    Provider() { 
    } 

    void run() { 
     try { 
      // 1. creating a server socket 
      providerSocket = new ServerSocket(2004, 10); 
      // 2. Wait for connection 
      System.out.println("Waiting for connection"); 

      connection = providerSocket.accept(); 
      System.out.println("Connection received from " 
        + connection.getInetAddress().getHostName()); 
      // 3. get Input and Output streams 
      out = new ObjectOutputStream(connection.getOutputStream()); 
      out.flush(); 
      in = new ObjectInputStream(connection.getInputStream()); 
      sendMessage("Connection successful"); 
      // 4. The two parts communicate via the input and output streams 
      do { 
       try { 
        message = (String) in.readObject(); 
        System.out.println("client>" + message); 
        if (message.equals("bye")) 
         sendMessage("bye"); 
       } catch (ClassNotFoundException classnot) { 
        System.err.println("Data received in unknown format"); 
       } 
      } while (!message.equals("bye")); 
     } catch (IOException ioException) { 
      ioException.printStackTrace(); 
     } finally { 
      // 4: Closing connection 
      try { 
       in.close(); 
       out.close(); 
       providerSocket.close(); 
      } catch (IOException ioException) { 
       ioException.printStackTrace(); 
      } 
     } 
    } 

    void sendMessage(String msg) { 
     try { 
      out.writeObject(msg); 
      out.flush(); 
      System.out.println("server>" + msg); 
     } catch (IOException ioException) { 
      ioException.printStackTrace(); 
     } 
    } 

    public static void main(String args[]) { 
     Provider server = new Provider(); 
     while (true) { 
      server.run(); 
     } 
    } 
} 

cevap

5

Çok olarak girişleri kullanabilirsiniz:

String hostname = "1.2.3.4"; 
int port = 865; 

Socket socket = null; 

try { 
    socket = new Socket(InetAddress.getByName(hostname), port); 
} catch (Exception e) { 

    e.printStackTrace(); 
} 

ParcelFileDescriptor socketedFile = ParcelFileDescriptor.fromSocket(socket); 

Sonra set socketedDosyaya çıktı Ses kaydedicinin dosya (socketedFile.getFileDescriptor()). Bu bayt olarak gönderir. Alternatif olarak, daha kararlı hale getirmek için, MediaRecorder'dan yerel bir ara belleğe veriyi yazınız ve daha sonra soket bağlantısında küçük ayrılmalara izin vermek için, onu yerine arabelleğe ayıran ve bunu yerine yazacak ayrı bir iş parçacığına sahip olacaklardır.

fazla bilgi için bu soruya bakın: android stream audio to server

Açıkçası ardından bayt almak ve ses verilerine yönelik açmak için bir sunucuda çalışan bir uygulama olması gerekir.

+0

Merhaba Micky ... İlginiz için çok teşekkür ederim. Ama başka bir istisna alıyorum (java.io.StreamCorruptedException: geçersiz akış başlığı: 00000018). Eğer yapabiliyorsanız, bu konuda yardım edin. Düzenlenmiş soruya bakın. – Nitin4Android

+0

+1 size ...... – Nitin4Android