Tsur Yohananov
Tsur Yohananov

Reputation: 543

How to generate multiple files with a media recorder?

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.

Upvotes: 1

Views: 1159

Answers (1)

Homayoon Ahmadi
Homayoon Ahmadi

Reputation: 2843

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);

Upvotes: 1

Related Questions