私は音声を録音し、MultiPartEntityを使用してPHPサーバにアップロードする1つのモジュールで作業しています。MediaRecorderを使用してオーディオファイルを録音し、androidのsdcardに保存する方法
私は音声とその保存ファイルをsdcardに記録できますが、そのファイルをPHPサーバ(MPEを使用)にアップロードしようとしているときは、そのファイルはアップロードされず、空のファイルコンテンツを取得しています。
私のデバイスでは、私はその声を再生し、デバイスで正常に動作することができます。アップロードするだけで問題が発生します。私は同じコードを使用して、画像やビデオ、その他のファイルのような他のメディアをアップロードしています。すべて正常に動作しています。記録されたファイルを発行するだけです。
これは私の声を録音するコードです。
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();
と私はmultipartを使用してサーバーにこの音声録音をアップロードしています。
これは私の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;
}
私はウェブ上で同じトピックについて多くを検索しましたが、この問題のためのexectソリューションを取得していません。
友人を助けてください。 Thanx in Advance
uは、この上の解決策は、私も同じ問題を抱え起こす??持っているか、サーバーにオーディオファイルが壊れています – ken