I would like to know how I can save various audio and then access it, and my program is closing every time I record the first audio.
Code:
package com.example.pedri.audiorecod;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private Button play, stop, record; //variáveis de instância
private MediaRecorder myAudioRecorder;//variáveis de instância
private String outputFile;//variáveis de instância
/*Declando as variáveis de instância, pois assim todos os métodos da Classe MainActivity extends AppCompatActivity
pode manipular a variávels de instância
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.play);
stop = (Button) findViewById(R.id.stop);
record = (Button) findViewById(R.id.recod);
stop.setEnabled(false);
play.setEnabled(false);
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IllegalStateException ise) {
} catch (IOException ioe) {
}
record.setEnabled(false);
stop.setEnabled(true);
Toast.makeText(getApplicationContext(), "Gravação Iniciada", Toast.LENGTH_LONG).show();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder = null;
record.setEnabled(true);
stop.setEnabled(false);
play.setEnabled(true);
Toast.makeText(getApplicationContext(), "Áudio Gravado com Sucesso", Toast.LENGTH_LONG).show();
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MediaPlayer mediaPlayer = new MediaPlayer();
try{
mediaPlayer.setDataSource(outputFile);
mediaPlayer.prepare();
mediaPlayer.start();
Toast.makeText(getApplicationContext(), "Comece o Áudio", Toast.LENGTH_LONG).show();
} catch (IOException e) {
}
}
});
}
}