I'm creating a Wall where people can send text and audio messages to a php server, text messages and the mural work perfectly, but I have no idea how to store recorded audio and send it right away .
I saw some examples on the internet and I'm using this code below:
To perform audio recording :
private void stopRecording(){
if(recorder != null) recorder.stop();
}
private void beginRecording() throws IOException, InterruptedException {
ditchMediaRecorder();
File outFile = new File(OUTPUT_FILE);
if(outFile.exists()) outFile.delete();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(OUTPUT_FILE);
recorder.prepare();
recorder.start();
}
private void ditchMediaRecorder(){
if(recorder != null) recorder.release();
}
In order to do the sending: (I do not know if this is correct because I am not able to receive anything on the php server) ...
public void enviaAudioServer(View v) {
new LongOperationAudio().execute("");
}
private class LongOperationAudio extends AsyncTask<String, Void, String> {
String name = nome.toString();
String mail = email.toString();
ProgressBar carrega = (ProgressBar) findViewById(R.id.carregaMsg);
@Override
protected String doInBackground(String... params) {
try {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final InputStream in = new FileInputStream(outFile); //Outfile usei pra fazer a gravação . . .
final byte[] buf = new byte[2048];
int n;
while ((n = in.read(buf)) >= 0) {
out.write(buf, 0, n);
}
final byte[] data = out.toByteArray();
String urlString = "http://www.siteServidorPHP.com.br/mural/listener.php?a=13";
HttpPost postRequest = new HttpPost(urlString);
postRequest.setEntity(new ByteArrayEntity(data));
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(postRequest);
HttpEntity entity = response.getEntity();
InputStream ins = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
String temp_str;
StringBuilder sb = new StringBuilder();
while((temp_str = br.readLine()) != null) {
sb.append(temp_str);
}
Log.e("response", sb.toString());
} catch (Exception e) {
// handle exception here
Log.e(e.getClass().getName(), e.getMessage());
return "exception";
}
return null;
}
@Override
protected void onPostExecute(String result) {
carrega.setVisibility(View.INVISIBLE);
}
@Override
protected void onPreExecute() {
carrega.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
When I run the following error occurs in logcat:
E / response: 403 Forbidden
Forbidden
You do not have permission to access /mural/listener.phpon this server.
Additionally, the 404 Not Founderror was encountered while trying to use an ErrorDocument to handle the request.
If anyone knows some way to send the audio and receive audios as well, every tip or suggestion is welcome.
Thank you!