[英]How to record audio file using MediaRecorder and save to sdcard in android
i am working on one module that will record voice and then upload to php server using MultiPartEntity. 我正在开发一个将录制语音,然后使用MultiPartEntity上传到php服务器的模块。
i am able to record voice and also its saving file in sdcard but whenever i am trying to uplaod that file to PHP server (Using MPE), its not uploading and server getting blank file content. 我能够在sdcard中录制语音及其保存的文件,但是每当我尝试将该文件升级到PHP服务器(使用MPE)时,它就不会上传并且服务器获取空白文件内容。
while in my device i am able to play that voice and working fine in device. 在我的设备中,我可以播放该声音并在设备中正常工作。 just issue with uploading.
只是上传问题。 i am using the same code to upload other media like images and video and other files.
我正在使用相同的代码上传其他媒体,例如图像,视频和其他文件。 all is working fine.
一切都很好。 just issue with recorded file.
只是记录文件的问题。
This is my code to 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); // This is my file path to store data
recorder.prepare();
recorder.start();
and i am using multipart to upload this voice recording on server. 我正在使用多部分将此语音记录上传到服务器上。
This is my MultiPartEntity code. 这是我的MultiPartEntity代码。
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 a lot on the same topic on web but not getting the exect solution for this issue. 我在网上搜索了同一主题的很多内容,但没有得到该问题的有效解决方案。
Please help me friends. 请帮我的朋友们。 Thanx in Advance
提前感谢
Maybe this video will help you. 也许这段视频会为您提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.