2012-03-19 14 views
6

Tôi muốn có chức năng phát trực tuyến âm thanh thời gian thực trên thiết bị Android đang ghi âm qua MIC của thiết bị và gửi đến máy chủ. Tôi biết gửi một tập tin âm thanh sau khi ghi âm nhưng trong trường hợp thời gian thực tôi cần sự giúp đỡ. Có thể nó có thể được thực hiện bằng cách gửi mảng byte liên tục đến máy chủ. Nếu vậy, làm thế nào hoặc nếu bất kỳ cách nào khác, Xin vui lòng chia sẻ ý tưởng của bạn. Cảm ơn.Phát trực tuyến Âm thanh thời gian thực

Edit-

Android Mã Chủ đầu tư: -

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 server số-

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

Trả lời

5

Bạn có thể sử dụng ổ cắm như vậy:

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

Sau đó thiết lập các socketedFile vào đầu ra file (socketedFile.getFileDescriptor()) của máy ghi âm. Điều này sẽ gửi nó lên dưới dạng byte. Để làm cho nó ổn định hơn, hãy ghi dữ liệu từ MediaRecorder vào bộ đệm cục bộ và sau đó có một chuỗi riêng biệt kiểm tra bộ đệm đó và ghi nó vào ổ cắm để cho phép ngắt kết nối nhỏ trong kết nối ổ cắm.

Xem câu hỏi này để biết thêm thông tin: android stream audio to server

Rõ ràng sau đó bạn cần phải có một ứng dụng chạy trên một máy chủ để nhận byte của bạn và biến chúng thành dữ liệu âm thanh.

+0

Xin chào Micky ... Cảm ơn rất nhiều vì sự quan tâm của bạn. Nhưng tôi nhận được một ngoại lệ khác (java.io.StreamCorruptedException: tiêu đề luồng không hợp lệ: 00000018). Xin hãy giúp đỡ về vấn đề này nếu bạn có thể. Tham khảo câu hỏi đã chỉnh sửa. – Nitin4Android

+0

+1 cho bạn ...... – Nitin4Android