2012-03-19 23 views
6

मैं एंड्रॉइड डिवाइस पर रीयल टाइम ऑडियो स्ट्रीमिंग की कार्यक्षमता चाहता हूं जो डिवाइस के एमआईसी के माध्यम से ऑडियो कैप्चर कर रहा है और इसे सर्वर पर भेजता है। मुझे रिकॉर्डिंग के बाद एक ऑडियो फाइल भेजने के लिए पता है, लेकिन वास्तविक समय के मामले में मुझे मदद चाहिए। यह सर्वर पर लगातार बाइट सरणी भेजकर किया जा सकता है। यदि ऐसा है तो कैसे या यदि कोई अन्य तरीका है, तो कृपया अपने विचार साझा करें। धन्यवाद।स्ट्रीमिंग रीयल टाइम ऑडियो

संपादित

एंड्रॉयड क्लाइंट कोड: -

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

    } 

जावा सर्वर Code-

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

उत्तर

5

आप इतनी के रूप में सॉकेट उपयोग कर सकते हैं:

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

फिर सेट उत्पादन के लिए socketed फ़ाइल ऑडियो रिकॉर्डर के फ़ाइल (socketedFile.getFileDescriptor())। यह इसे बाइट्स के रूप में भेज देगा।

वैकल्पिक रूप से इसे अधिक स्थिर बनाने के लिए, MediaRecorder से डेटा को स्थानीय बफर में लिखें और उसके बाद सॉकेट कनेक्शन में छोटे डिस्कनेक्शन की अनुमति देने के लिए बफर और इसे सॉकेट में लिखें।

अधिक जानकारी के लिए इस सवाल का देखें: android stream audio to server

जाहिर है आप तो अपने बाइट्स प्राप्त करते हैं और कि बारी ऑडियो डेटा में करने के लिए एक आवेदन पत्र एक सर्वर पर चल रहा है की जरूरत है।

+0

हाय मिकी ... आपकी चिंता के लिए बहुत बहुत धन्यवाद। लेकिन मुझे एक और अपवाद मिल रहा है (java.io.StreamCorruptedException: अमान्य स्ट्रीम हेडर: 00000018)। यदि आप कर सकते हैं तो कृपया इस संबंध में सहायता करें। संपादित प्रश्न का संदर्भ लें। – Nitin4Android

+0

आपको +1 ... – Nitin4Android