I have an application that streaming Video/Audio from Android device to Server Streaming are fine but when I save the streamed data fro MediaRecoreder I can't play the file
Android code :
String hostname = "000.000.000.000";
int port = 0000;
Socket socket = null;
try {
socket = new Socket(InetAddress.getByName(hostname), port);
} catch (UnknownHostException e1) {
e1.printStackTrace();
开发者_如何转开发} catch (IOException e1) {
e1.printStackTrace();
}
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(pfd.getFileDescriptor());
recorder.start();
server side :
Socket userSocket = socket.accept();
//DataInputStream dis;
dis = new DataInputStream(userSocket.getInputStream());
while(true){
dis.read(buf , 0 , buf.length);
saveBufferToFile(buf);
}
Now the I save the buffer using FileOutStream .write(); method but the out put file can't be played at all.
after research I understand that I need to add the mp4 headers to the file before I write the data on it BUT I don't know how to do this !?
Regards,
Firstly, the MPEG4 container is more than just "headers" to video data.
Secondly, the server side code does not look correct to me. I assume it is Java. Specifically:
dis = new DataInputStream(userSocket.getInputStream());
while(true){
dis.read(buf , 0 , buf.length);
saveBufferToFile(buf);
}
Specifically, DataInputStream.read()
does not necessarily read the entire length of the buffer. The JavaDocs say:
An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero.
I suspect the file you are writing to disk is corrupted. You can confirm this by comparing the number of bytes sent by the client and the size of file. My gut say the file will be significantly larger.
To fix it you'll need to take not of the bytes read in by read()
, and only write those to bytes to disk. The rest of the buffer is effectively garbage, ignore it.
精彩评论