Android Open Source - android-voice-client Main Activity






From Project

Back to project page android-voice-client.

License

The source code is released under:

MIT License

If you think the Android project android-voice-client listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.ramdom.voice_backend.android_transceiver;
/*from  w ww . j av a2 s  .  c o  m*/
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.io.File;
import java.io.IOException;


public class MainActivity extends Activity {
    private MediaRecorder myRecorder;
    private String recordingFile;
    private Button start,stop,play;
    private TextView textState;

    private RequestQueue reqQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        start = (Button)findViewById(R.id.buttonRecord);
        stop = (Button)findViewById(R.id.buttonStop);
        play = (Button)findViewById(R.id.buttonPlay);
        textState = (TextView)findViewById(R.id.textState);

        stop.setEnabled(false);
        play.setEnabled(false);

        recordingFile = Environment.getExternalStorageDirectory().
                getAbsolutePath() + "/myRecording.aac";

        myRecorder = new MediaRecorder();
        myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

        myRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
        //myRecorder.setAudioEncoder(MediaRecorder.getAudioSourceMax());
        myRecorder.setAudioEncodingBitRate(48000);
        myRecorder.setAudioSamplingRate(44100);
        myRecorder.setOutputFile(recordingFile);

        textState.setText("Waiting");
        reqQueue = Volley.newRequestQueue(this);
    }

    public void startRecording(View view){
        try {
            myRecorder.prepare();
            myRecorder.start();
        } catch (IllegalStateException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        start.setEnabled(false);
        stop.setEnabled(true);
        Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();

    }

    public void stopRecording(View view){
        try{
            myRecorder.stop();
        } catch (RuntimeException e){
            e.printStackTrace();

            Toast.makeText(getApplicationContext(), "Audio recorder failed",
                    Toast.LENGTH_LONG).show();

        } finally {
            myRecorder.release();
            myRecorder  = null;
        }

        stop.setEnabled(false);
        play.setEnabled(true);
        textState.setText("Recorded");
        Toast.makeText(getApplicationContext(), "Audio recorded successfully",
                Toast.LENGTH_LONG).show();
    }

    public void play(View view) throws IllegalArgumentException,
            SecurityException, IllegalStateException, IOException{

        MediaPlayer m = new MediaPlayer();
        m.setDataSource(recordingFile);
        m.prepare();
        m.start();
        Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();
    }

    public void sendRequest(View view){
        // Instantiate the RequestQueue.

        String url ="http://www.google.com";

        StringRequest strReq = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(getApplicationContext(), "Resp: " + response.substring(0,30),
                                Toast.LENGTH_LONG).show();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "Resp fail",
                        Toast.LENGTH_LONG).show();
            }
        });
// Add the request to the RequestQueue.
        reqQueue.add(strReq);

    }

    public void showAlertDialog(View view){
        // Create a popup
        // Create a recorder
        // Start recording
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

        alertDialogBuilder.setMessage("Are you sure?");

        alertDialogBuilder.setPositiveButton("Positive btn",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {

                    }
                });

        alertDialogBuilder.create();
        alertDialogBuilder.show();
    }

    public void killDialog(AlertDialog dialog){
        dialog.dismiss();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}




Java Source Code List

org.ramdom.voice_backend.android_transceiver.ApplicationTest.java
org.ramdom.voice_backend.android_transceiver.MainActivity.java