Android - How to send an Audio to an FTP server?

Asked

Viewed 93 times

0

I’m creating a Mural where people can send text and audio messages to a php server, text messages and the wall work perfectly, but I have no idea how to store a recorded audio and send it right away.

I saw some examples on the internet and I’m using this code below:

To make 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();
}

To perform the Upload: (I don’t know if this upload is correct because I can’t get anything into 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 run the following logcat error appears:

E/Answer: 403 Forbidden

Forbidden

You don’t have permission to access /mural/Listener.phpon this server.

Additionally, a 404 Not Founderror was encountered while trying to use an Errordocument to Handle the request.

If anyone knows any way to send audio and receive audio as well, any hint or suggestion is welcome.

Thank you!

  • 1

    Then you can turn your file(audio) into an array of bytes [] so send it to your server! Search how to convert a file to bytes and you will get quiet

  • I will try here, if I succeed or not, warning. Thank you!

  • Hello Alessandro, I tried to do it the way you suggested, but unfortunately I haven’t succeeded yet. I posted the latest version of the code in the question so you can see, if you identify the problem, please inform. Thank you for your attention.

  • 1

    Why not use lib Retrofit2.0? It is quite simple to use.

  • Blz, I’ll read about it and try here, if I get it or not, warning. Thanks for the tip!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.