Record audio and then play it

All your problems with Audio, Video and Images.

Record audio and then play it

Postby jabuti » Wed Sep 29, 2010 10:49 pm

Hello, I want to do an application where the user has to speak in the microphone. I want to record what the user is saying and create a file. I want to display a list with all the files I have recorded and when the user presses in one item, then play the file so the user can hear what is said in the file. Do you have any suggestions? I'm new on android and I'm completely lost. Do you have any good tutorial which I can follow? Thanks in advance
jabuti
Freshman
Freshman
 
Posts: 6
Joined: Wed Sep 29, 2010 10:45 pm

Top

Re: Record audio and then play it

Postby jabuti » Fri Oct 01, 2010 2:55 pm

I put some code.

This is the main class.

public class Record extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final AudioRecorder recorder = new AudioRecorder("/calls");
try {
recorder.start();
try {
this.wait(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
recorder.stop();
} catch (IOException e) {
e.printStackTrace();
}
}
}

And the helper class is:

public class AudioRecorder {

final MediaRecorder recorder = new MediaRecorder();
final String path;

public AudioRecorder(String path) {

this.path = sanitizePath(path);
}

private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gp";
}

//This is the command I would like to change, because I want to save the audio files in the internal memory
//instead of the Sdcard
return Environment.getDataDirectory().getAbsolutePath() + path;
}

public void start() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state + ".");
}

// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
}

public void stop() throws IOException {
recorder.stop();
recorder.release();
}
}
jabuti
Freshman
Freshman
 
Posts: 6
Joined: Wed Sep 29, 2010 10:45 pm

Re: Record audio and then play it

Postby jabuti » Tue Oct 05, 2010 10:07 pm

Please!! Anybody knows how to access the internal memory? And anybody knows how to create a file in the internal memory and the delete the same file? The file should be .3gp
jabuti
Freshman
Freshman
 
Posts: 6
Joined: Wed Sep 29, 2010 10:45 pm

Re: Record audio and then play it

Postby Draffodx » Wed Oct 06, 2010 9:46 am

jabuti wrote:Please!! Anybody knows how to access the internal memory? And anybody knows how to create a file in the internal memory and the delete the same file? The file should be .3gp


Set your files path to use the internal storage.

Code: Select all
final String PATH = "data/data/your.package.name/files/file_name.3gp";
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

Re: Record audio and then play it

Postby jabuti » Fri Oct 08, 2010 4:29 pm

Thanks a lot!! It was really helpfull
jabuti
Freshman
Freshman
 
Posts: 6
Joined: Wed Sep 29, 2010 10:45 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 13 guests