1

Some of my code:

    outPutFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";

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

List and adapter for showing the files

    listRecord.addAll(Arrays.asList(outPutFile));
    listAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, listRecord);
    recordList.setAdapter(listAdapter);

outPutFile is a String.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Tsur Yohananov
  • 533
  • 1
  • 7
  • 17

1 Answers1

1

You must change filename every time you want to record a new one, so you can add datetime to file name to create unique names:

String dateTime = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss aa",Locale.getDefault()).format(new Date());
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Recordings/recording -" + dateTime + ".3gp";

for showing the items you have to create a database to save filenames, and get the filenames from database when you want to show recording list

String DIR_DATABASE = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Recordings";
String sqliteQuery= "CREATE  TABLE  IF NOT EXISTS Recordings (ID INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , fileName VARCHAR)"
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE + "db.sqlite", null);
database.execSQL(sqliteQuery);

and for inserting data into database use:

SQLiteDatabase database = SQLiteDatabase.openDatabase(DIR_DATABASE + "db.sqlite", null, 0);
ContentValues values = new ContentValues();
                values.put("fileName", outputFile );
                database.insert("Recordings", "", values);

then for read fileNames from database use this lines:

Cursor cursor = database.rawQuery("SELECT fileName FROM Recordings", null);
ArrayList<String> fileNames = new ArrayList<>();
while (cursor.moveToNext()) {

     String fileName = cursor.getString(0);
     fileNames.add(fileName);
}
cursor.close();
database.close();

and finally add them to your list adapter :

listRecord.addAll(fileNames);
listAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, listRecord);
recordList.setAdapter(listAdapter);
Homayoon Ahmadi
  • 1,181
  • 1
  • 12
  • 24
  • 1
    Thank you very much! I have one problem: the name of the files appears as "0", I had to write the String fileName = cursor.getInt(0); as String fileName = String.valueOf(cursor.getInt(0)); any idea? – Tsur Yohananov Jul 31 '17 at 15:01
  • yes , I made a mistake in my code, you must use: fileName = cursor.getString(0); please upvote if it was helpful to you – Homayoon Ahmadi Aug 01 '17 at 05:24
  • Did that, it gives the name and the path... How do I access the files for playing them with the mediaplayer class? Assume I want to use onclicklistener on the list, using getposition? – Tsur Yohananov Aug 01 '17 at 08:06
  • 1
    you can find your answer here: https://stackoverflow.com/questions/7291731/how-to-play-audio-file-in-android – Homayoon Ahmadi Aug 01 '17 at 08:41
  • I can find my answer but first I have to get the name of the files correctly, because right now it gives me the name with the path, and still shows the "0" before the date... – Tsur Yohananov Aug 01 '17 at 09:34
  • I used . replaceAll and was able to erase all the letters but still the "0" consist – Tsur Yohananov Aug 01 '17 at 10:05