How to record audio file using MediaRecorder and save to sdcard in android -
i working on 1 module record voice , upload php server using multipartentity.
i able record voice , saving file in sdcard whenever trying uplaod file php server (using mpe), not uploading , server getting blank file content.
while in device able play voice , working fine in device. issue uploading. using same code upload other media images , video , other files. working fine. issue recorded file.
this code record voice.
mediarecorder recorder = new mediarecorder(); recorder.reset(); recorder.setaudiosource(mediarecorder.audiosource.mic); recorder.setoutputformat(mediarecorder.outputformat.mpeg_4); recorder.setaudioencoder(mediarecorder.audioencoder.default); string filepath = environment.getexternalstoragedirectory() + "/" + environment.directory_dcim + "/abc.mp4"; recorder.setoutputfile(filepath); // file path store data recorder.prepare(); recorder.start();
and using multipart upload voice recording on server.
this multipartentity code.
public class multipartutility { private static final string line_feed = "\r\n"; private static final int timeout = 60000; private final string boundary; private httpurlconnection httpconn; private outputstream outputstream; private printwriter writer; public multipartutility(string requesturl) throws ioexception { boundary = "======" + system.currenttimemillis() + "======"; url url = new url(requesturl); httpconn = (httpurlconnection) url.openconnection(); httpconn.setusecaches(false); httpconn.setdooutput(true); // indicates post method httpconn.setdoinput(true); httpconn.setreadtimeout(timeout); httpconn.setconnecttimeout(timeout); httpconn.setrequestproperty("content-type", "multipart/form-data; boundary=" + boundary); httpconn.setrequestproperty("connection", "keep-alive"); // ================ outputstream = httpconn.getoutputstream(); writer = new printwriter(new outputstreamwriter(outputstream, "utf-8"), true); } public void addformfield(string name, string value) { writer.append("--").append(boundary).append(line_feed); writer.append("content-disposition: form-data; name=\"").append(name).append("\"").append(line_feed); //writer.append("content-type: text/plain; charset=utf-8").append(line_feed); writer.append(line_feed).append(value).append(line_feed); writer.flush(); } public void addfilepart(string fieldname, file uploadfile) throws ioexception { string filename = uploadfile.getname(); writer.append("--").append(boundary).append(line_feed); writer.append("content-disposition: form-data; name=\"").append(fieldname).append("\"; filename=\"").append(filename).append("\"").append(line_feed); writer.append("content-type: ").append(urlconnection.guesscontenttypefromname(filename)).append(line_feed); writer.append("content-transfer-encoding: binary").append(line_feed); writer.append(line_feed); writer.flush(); fileinputstream inputstream = new fileinputstream(uploadfile); byte[] buffer = new byte[4096]; int bytesread; while ((bytesread = inputstream.read(buffer)) != -1) { outputstream.write(buffer, 0, bytesread); } outputstream.flush(); inputstream.close(); writer.append(line_feed).flush(); } public string execute() throws ioexception { string response = ""; writer.append(line_feed).flush(); writer.append("--").append(boundary).append("--").append(line_feed); writer.close(); int status = httpconn.getresponsecode(); if (status == httpurlconnection.http_ok) { bufferedreader reader = new bufferedreader(new inputstreamreader(httpconn.getinputstream())); string line; while ((line = reader.readline()) != null) { response += line; } reader.close(); } else { return null; } httpconn.disconnect(); return response; }
i have searched lot on same topic on web not getting exect solution issue.
please me friends. thanx in advance
maybe video you.
Comments
Post a Comment