Dans mon application avec de nombreuses activités, j'essaie de faire un enregistrement dans une activité et de l'enregistrer dans les fichiers du téléphone. Ensuite, dans une autre activité, je veux lire ce fichier. Je pense que je peux avoir un problème avec la façon dont je sauvegarde le fichier d'origine, car il se bloque sur la deuxième activité quand il veut lire le fichier. Je ne suis pas sûr de savoir comment enregistrer dans une activité et ensuite lire ce fichier audio dans l'activité suivante. J'ai inclus ce que je pensais être le code pertinent des deux activités.Enregistrer et enregistrer l'audio dans une activité et lire l'audio dans une autre activité
//This is the Activity that simply records and then saves the audio file
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context mContext = getApplicationContext();
createTempFile("Status_Recorder.txt", "INPROGRESS");
/*
* Create the file where the audio tone that is recorded will be saved
*
*/
try{
FileOutputStream fOut = openFileOutput("audio_test_right.3gp" , MODE_WORLD_READABLE);
}catch(IOException e) {
e.printStackTrace();
exit_function();
}
path = mContext.getFilesDir()+"/audio_test_right.3gp";
start_recording();
}
//Method to Start the Recording
private void start_recording() {
//Intialize the recorder
try{
speaker_recorder = new MediaRecorder();
speaker_recorder.reset();
} catch(Exception e){
Log.e(log_tag,"Recorder Initialization Failed");
createTempFile("Status.txt", "COMPLETED-RECORDER FAILED");
}
//Setting for the Recorder
try{
Log.i(log_tag,"Setting the recorder");
speaker_recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
speaker_recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
speaker_recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
speaker_recorder.setOutputFile(path);
} catch(Exception e){
Log.e(log_tag,"Recording Settings Failed");
createTempFile("Status.txt", "COMPLETED-RECORDER FAILED");
}
//Prepare the Recorder
try{
Log.i(log_tag,"Preparing the Recorder");
speaker_recorder.prepare();
} catch(Exception e){
Log.e(log_tag,"Recording failed");
createTempFile("Status.txt", "COMPLETED-RECORDER FAILED");
exit_function();
}
//Start the Recorder
try {
Log.i(log_tag,"Starting the recorder");
title_text = ((TextView) findViewById(R.id.textView));
title_text.setTextColor(Color.RED);
title_text.setText("RECORDING");
speaker_recorder.start();
// Thread.sleep(10000);
mHandler.postDelayed(new Runnable() {
public void run() {
createTempFile("Status_Recorder.txt", "COMPLETED-RECORDER FAILED");
exit_function();
}
}, timer);
} catch (Exception e) {
Log.e(log_tag,"Recorder start failed");
createTempFile("Status.txt", "COMPLETED-RECORDER FAILED");
exit_function();
}
}
private void exit_function() {
if (speaker_recorder != null) {
speaker_recorder.release();
}
onDestroy();
}
@Override
/*
* (non-Javadoc)
* @see android.app.Activity#onDestroy()
* Function invoked before we exit the application . Reset all the volume
* and stream values in this function
*/
protected void onDestroy() {
Log.i(log_tag,"Entered onDestroy()");
super.onDestroy();
this.finish();
}
/*
* Function to create the a text file in the application directory context. This function
* takes the file name and the string that is to be written in it as the input. This function is invoked
* to create the Result.txt file.
*/
private void createTempFile(String filename, String text) {
try {
FileOutputStream fOut = openFileOutput(filename , MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(text);
osw.flush();
osw.close();
} catch(IOException e) {
e.printStackTrace();
}
}
// C'est l'activité En second lieu, mais il tombe en panne quand il arrive à la mp.setDataSource (chemin); parce que je suppose qu'il ne peut pas trouver le chemin
private void playSound(boolean speakers) {
mContext = getApplicationContext();
// audioManager.setMicrophoneMute(true);
path = mContext.getFilesDir() + "/audio_test_right.3gp";
audioManager.setSpeakerphoneOn(true);
try {
mp.setDataSource(path);
} catch (IOException e) {
e.printStackTrace();
}
if (speakers) {
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
} else {
mp.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
}
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
createTempFile("Status_RightSpeaker.txt", "COMPLETED");
exit_function();
}
});
}
}
Vous êtes incroyable !!! J'essaie de trébucher à travers ce truc Android et vous continuez à sauver ma vie. Merci beaucoup!!!! – Toby