samedi 10 octobre 2015

Saving audio recording in android to SQLite

I am writing an program that allows the user to record and save an audio recording to an SQLite database. The information that will be saved in each record is the following: String description, double longitude, double latitude, MediaRecorder audioFile (not sure if the actual recording would be stored in a record or if a reference to the file stored in memory is used). Longitude and Latitude will be taken from the MainActivity with a GoogleMaps object and other related location methods

For loading the audio files, I want to have a list in my MainActivity and when a particular record is pressed, android will jump to a playing activity to play the recording (I assume a query will be used to select the particular recording pressed)

This is what I have done so far

public class AudioActivity extends ActionBarActivity {

    private MediaRecorder mediaRecorder;
    private String outputFile = "";
    private EditText audioDescription;
    Button startRecording;
    Button stopRecording;
    Button playRecording;
    Button saveRecording;
    private FileOutputStream outputStream;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio);

        audioDescription = (EditText) findViewById(R.id.descriptionOfAudioRecording);
        startRecording = (Button) findViewById(R.id.startRecording);
        stopRecording = (Button) findViewById(R.id.finishRecording);
        playRecording = (Button) findViewById(R.id.playRecording);
        saveRecording = (Button) findViewById(R.id.saveRecording);
        stopRecording.setEnabled(false);
        playRecording.setEnabled(false);
        String filepath = String.valueOf(audioDescription.getText());
        outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filepath + ".3gp";;

        mediaRecorder=new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        mediaRecorder.setOutputFile(outputFile);

        startRecording.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (audioDescription.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(), "Please enter description before record", Toast.LENGTH_LONG).show();
                } else {

                    try {
                        mediaRecorder.prepare();
                        mediaRecorder.start();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    startRecording.setEnabled(false);
                    stopRecording.setEnabled(true);

                    Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
                }
            }
        });

        stopRecording.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaRecorder.stop();
                mediaRecorder.release();
                mediaRecorder  = null;

                stopRecording.setEnabled(false);
                playRecording.setEnabled(true);

                Toast.makeText(getApplicationContext(), "Audio recorded successfully",Toast.LENGTH_LONG).show();
            }
        });

        playRecording.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) throws IllegalArgumentException,SecurityException,IllegalStateException {
                MediaPlayer m = new MediaPlayer();

                try {
                    m.setDataSource(outputFile);
                }

                catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                    m.prepare();
                }

                catch (IOException e) {
                    e.printStackTrace();
                }

                m.start();
                Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();
            }
        });

        saveRecording.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TO BE IMPLEMENTED WITH SQLITE
            }
        });
    }

    @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_audio, 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);
    }


}

activity_audio.xml

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
android:id="@+id/descriptionOfAudioRecording"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/startRecording"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/start_recording"/>

<Button
    android:id="@+id/finishRecording"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Stop Recording"/>

<Button
    android:id="@+id/playRecording"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Play Recording"/>

<Button
    android:id="@+id/saveRecording"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/save_audio_recording"/>
</LinearLayout>

Aucun commentaire:

Enregistrer un commentaire