Hello everyone,

I am new at Java(android)-before I have little information about Java lang- and in particular socket programming -before I do not know nothing about socket client server programming-, previously I wanted to write simple chat program by using Threads and now I pass this step, two android emulators can talk each other, to success -just for test even I parse whether it is string or integer- that I use ObjectInputStream and ObjectOutputStream classes with writeObject(obj: MyClass) (in Client Thread class) and readObject() (in Server Thread class). MyClass -just for now I hope with your help- contains only string attribute which implements Serializable interface. Yes it is my history, no problem BUT NOW I am asking to you how can I use and modify MyClass according to sending/receiving sound record, NOT FROM A FILE. Here my ideas -thanks to my chief-

Primitive Algorithm
1-> yes I have MyClass and it is needed to this purpose so go to step 2
2-> I want to add buffer array typed byte named audio_array ok no problem go to step 3
now my class contains string literal and byte array
3-> I should fill MyClass's byte array with voice -starting this part, it is very hard to me :(- my question is that how it will understand that sending data is voice call and how byte array is filled ??!! I should NOT have a file with extension .wav or .pcm format :( Also I should responsible to adjust size of this array but how? step 4
4-> Receiver class (In my program it is server thread class) It should understand of type of data and go to 5
5-> Yes Receiver class understands the type and calls the new Threads -i.e If I send text call the Thread which is responsible for showing text or If I send voice stream then It should call the Thread which is responsible for playing this record. Ok just for this part, I start threads for different purposes no problem.

Also my chief talks about junking i.e. in a for loop just for 2 seconds I send sound data after 2 second wait and re-start again and again I do not understand :(

To sum up, In fact, It is no difference sending text or sending voice call -it should be!!- because I do not use any file whether sending text or sending voice in fact I want to send/receive serializable objects (I already but just for string literals). I only want to extend MyClass ability but how? It sounds easy for my ears but to implement it seems very hard ? Please help me where I should start how to use google efficently by true keywords and true java classes for this purpose, I want to do this before my intern period ends :(

Thanks for previous posting , in particular Moderator JamesChyrelll...

Recommended Answers

All 8 Replies

So your sound is in a byte array? In that case you can simply write that to the socket via an ObjectOutputStream. At the receiver you just read it via an ObjectInputStream and now the receiver has a copy of the sender's array. You already have code to do that, because your file sending code first creates a byte array from the input file, then sends it. Similarly at the receiving end you read the data into a byte array before writing it to the output file. The code you need is just a subset of the code you already have.
The interesting part is how to get the sound into a byte array in the first place, and how to play a sound that's in a byte array. That depends on what software you are using to recoed and play, and exactly how you are using that software.

I actually do not understand last two sentences, I understand what you are saying otherwise ok now I already try to working with files that stored under sdcard.

"The interesting part is how to get the sound into a byte array in the first place, and how to play a sound that's in a byte array."
Hmmm if I assume the working file -> example.wav (ok) emulator 5554
I create new file under sdcard -> new.wav (ok) emulator 5556
overwrite the data using byte array -MyClass's byte array ->
then MediaPlayer object executes playing (in receiver class it will created or receiver class start a new Thread responsible for create MediaPlayer object (ok) no problem

but in the first place ?? It seems flu for me what does mean may you explain ?
I will try to ask more elegant && understandable question in English

Sorry about the colloquial English - I will try to keep it simpler.
Are you using JMF?
If so, maybe you can treat the voice input as a PushBufferDataSource. That will call your listener and pass it buffers of sound that you can send to the receiver via the Object output/input streams. At the receiver you can process these buffers in the usual way.

I ask "Uncle Google" what is JMF (Java Media Framework) and how it works in android -> it is not used, in fact I already MediaPlayer class in my android project but I try to stil send .wav extension sound file (and trying the file size as integer data with BufferedReader... classes) with Object output/input streams but I can not make any progress on my project, anyway thanks a lot when I manage I will post related code snippet.

OK. Sorry, but I have no experience of MediaPlayer for Android, so I cannot help much. Maybe someone else knows more about it? Good luck anyway.

MediaPlayer is not scope of my problem, ıt is about 5 or 6 line of code snippet :) Problem is sending file and file size I am looking at using chunks to send from client side. anyway thanks for posting.

I'm very confused.
Your first post in this thread said

NOW I am asking to you how can I use and modify MyClass according to sending/receiving sound record, NOT FROM A FILE.

now you say

Problem is sending file ...

can you see why that is confusing?

If the problem is file size, then the first code I showed in one of your earlier threads breaks the file into small chunks for sending. Did you read that code?

sorry about confusing, I did not explain in truely; my final task in my internship is to write android application to allow to communicate && share sound/video streams like walkie-talkie application. Before embarking this job, I have not known any information about socket programming (and also threads <->)... so I take this assignment/problem step by step, first of all I was getting learn how to socket and server socket classes are used, then I learnt to connect two android emulators then I wanted to learn transfer some kind of data between these connected 2 emulators. String is remarkably is easy data then sound remarkably in particular for me. Then I successed to send string (you can say message application) types from one emulator to other and vice versa (I use two different ports...) Yes my final aim is to read/write stream datas not from a file but as step by step I want to send files truely I got to do two hours ago. -byte[] buffer = new byte[8192] while data do not equal to -1 etc. -now I manage to this step, but I stay still far behind my final task, I know. I also tell my chief what I done and what I have to do more, he suggests looking at AudioTrack class in android API. I think this link quite useful for who want to develop android application in particular these kind of stream applications. http://developer.android.com/reference/android/media/AudioTrack.html
It allows to play streaming data with buffer arrays, now I am looking at this link information, I hope, I will learn quick enough.
I have special thanks for your care ;)

Be a part of the DaniWeb community

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