Hello everyone,

I progress step by step my problem, one step is passed -now I can send string datas -text messages- here another step is I want to record some audio sounds by using microphone in first android emulator assume phone1 and send to second android emulator-phone2-. I found whole class about capturing and recording into audio file so I only ask to you that how I can send these recorded sound file -example and assume that I have a file named myFirst.wav in sdcard of first android emulator 5554 but not in android emulator 5556 what classes in java api should be used and is there another way to transfer sound file between two android emulator instances ?

In fact, I am not asking code I have seen that -I am new at socket programming- server and client codes are canonical and almost same in connection side. I ask only that an idea or half-algorithm

Thanks for your posting
please help

Recommended Answers

All 6 Replies

1. Open file (if applicable)
2. convert to byte array
3. convert ip/host name to a InetAddress variable
4. create a packet (DatagramPacket for UDP for example)
5. create socet
6. send byte array
7. close socket.

That's how you send a byte array, and it dceonst matter what the byte array is about, it could be a jpeg .flac .exe, doesnt matter. What matter is that it's in a byte array.

Hope it helps.

Once you have your socket connection you can send the file over it by simply reading it in sensible sized blocks and writing them in a single stream of bytes to a DataOutputStream via the socket.
This is the standard code for doing that kind of thing, which you will find all over the web:

    DataOutputStream outbound = new DataOutputStream(clientSocket.getOutputStream());
    byte[] data = new byte[4096];
    int count;
    while ((count = file.read(data)) != -1) {
        outbound.write(data, 0, count);

similarly at the receiving end you open a DataInputStream via the socket and do whatever you need with the inbound bytes

I use BufferedOutputStream object to call write function in a while loop I also use byte -as I learned that Stream classes use bytes and Reader/Writer classes use chars;

FileOutputStream fos = new FileOutputStream(file.toString());
 BufferedOutputStream bos = new BufferedOutputStream(fos);
 InputStream is = socket.getInputStream();

    int count;
    byte [] buffer = new byte[8192];
    while ((count = is.read(buffer, 0, buffer.length)) != -1)
        bos.write(buffer, 0, count);

and in send function -which is send .wav file from client to server

 int packetSize = 1024;
                         double nosofpackets=Math.ceil(((int) file.length())/packetSize);

                         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

                         double i;
                         for (i = 0; i < nosofpackets+1; ++i)

                            byte[] mybytearray = new byte[1024];   
                            bis.read(mybytearray, 0, mybytearray.length);
                            Log.d("", "Packet:"+(i+1));
                            OutputStream os = socket.getOutputStream();
                            os.write(mybytearray, 0,mybytearray.length);

[I found from Internet yesterday] what does it do ?

1-> I placed 1.wav file under sdcard directory for both emulators
2-> and I generate new.wav file under sdcard but it is empty -wait to overwrite bytes-
It works fine but my problem is with these code -now I just send one .wav file emulator 5554 to emulator 556 and vice versa- I want to send different .wav files or sound records when I press send button how to I should modify these code -in algorithm level-

thanks for previous posting and suggestions.

That code you found on the internet looks like a longer, more error-prone variant of the code I posted for you, and your first small code sample uses. It's not good. I strongly suggest that if you get code off some random internet site, and you don't understand exactly how every line works, then just delete it.
Sending different files: use the same code with a different File as input! I don't know what methods are availabe for selecting Files on Java ME, sorry.

In fact, I use Java SE for android application, Yestersday, I tried what you said here,

File sdcard = Environment.getExternalStorageDirectory();
                         if (count%2 == 0)
                             file = new File(sdcard, "1.wav");
                             file = new File(sdcard, "rarOut.wav");

                         File newFile = new File(sdcard, "new.wav");

Above I defined earlier count:int variable by assinging to zero and each time I push send button I increment the count -these are assumption- new.wav is a file that holds/waits to overwrite first, 1.wav data and second rarOut.wav data; now I manage to send 1.wav from emulator 1 to emulator 2 and vice versa and also I play 1.wav sound by playing new.wav file and I increment count by 1 now second time pushing the send button now it executes else part at file = new File(sdcard, "rarOut.wav") this second operation can not be ended and I can not increment count again -means that my count variable value can not be 2 to debug that I added Log.i methods which is System.out.println() function for android emulators where ->

  int byte_counts=0;
                              while((byte_counts = bis.read(audio_file , 0 , audio_file.length)) != -1 )

                                  Log.i("Send bytes", ""+ byte_counts); 
                                    Log.i("Flush above", "" + byte_counts); 
                                    Log.i("Flush below", "" + byte_counts); 

I see that -> byte array length is 65536

Send bytes 6555
Flush above 6555
Flush below 6555
Send bytes 6555
and it stop it it waits the while loop, what its your suggestion ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.