Play audio file from Resources – Android

playSample(R.raw.defaultmusic);

private void playSample(int resid)
	{
	    AssetFileDescriptor afd = getApplicationContext().getResources().openRawResourceFd(resid);

	    try
	    {   
	        mp.reset();
	        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
	        mp.prepare();
	        mp.start();
	        afd.close();
	    }
	    catch (IllegalArgumentException e)
	    {
	        Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
	    }
	    catch (IllegalStateException e)
	    {
	        Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
	    }
	    catch (IOException e)
	    {
	        Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
	    }
	}

Delete item from ListView on Long press -Cursor Adapter – Android

DBHelper db;
Cursor c;
RecordCursorAdapter recordCursorAdapter;
// RecordCursorAdapter extends CursorAdapter class

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView arg0, View v, int position, long arg3) {
                AlertDialog.Builder ad = new AlertDialog.Builder(ListActivity.this);
                ad.setTitle("Delete");
                ad.setMessage("Sure you want to delete record ?");
                final int pos = position;
                ad.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Delete of record from Database and List view.
                        c.moveToPosition(pos);
                        db.delete(c.getInt(c.getColumnIndex(DBHelper.KEY_ID)));
                        c=db.getAll();
                        recordCursorAdapter.swapCursor(c);
                        listView.setAdapter(recordCursorAdapter);
                    }
                });
                ad.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                ad.show();
                return false;
            }
        });


Delete and getAll() function in DBHelper class

    public void delete(int anInt) {
        SQLiteDatabase db = this.getWritableDatabase();

        db.delete(TABLE_RECORD,KEY_ID+"=?",new String[]{String.valueOf(anInt)});
        db.close();
    }

public Cursor getAll(){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery("SELECT  * FROM "+TABLE_RECORD, null);
        return c;
    }

Convert recorded 3gp file to Byte array and store in SQLite database – Android

Use MediaRecorder and store audio in outputFile

outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";;
byte[] soundBytes;

try {
    InputStream inputStream = getContentResolver().openInputStream(Uri.fromFile(new File(outputFile)));
    soundBytes = new byte[inputStream.available()];

    soundBytes = toByteArray(inputStream);
}catch(Exception e){
    e.printStackTrace();
}


public byte[] toByteArray(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int read = 0;
        byte[] buffer = new byte[1024];
        while (read != -1) {
            read = in.read(buffer);
            if (read != -1)
                out.write(buffer,0,read);
        }
        out.close();
        return out.toByteArray();
    }

Convert Byte array to 3gp file and play – Android

Get stored byte array from sqlite database

byte[] soundBytes=db.getSound(); 
// db.getSound() returns byte array from database


String outputFile=Environment.getExternalStorageDirectory().getAbsolutePath() + "/output.3gp";
File path = new File(outputFile);
FileOutputStream fos = null;
try {
     fos = new FileOutputStream(path);
     fos.write(soundBytes);
     fos.close();
} catch (Exception e) {
     e.printStackTrace();
}
MediaPlayer mediaPlayer = new MediaPlayer();

try {
     mediaPlayer.setDataSource(outputFile);
     mediaPlayer.prepare();
     mediaPlayer.start();
} catch (IOException e) {
     e.printStackTrace();
}