@@ -15,6 +15,19 @@ public class AudioDataSaver implements AudioDataReceivedListener {
1515
1616 private static final String TAG = AudioDataSaver .class .getSimpleName ();
1717
18+ // file size of when to delete and create a new recording file
19+ private final float MAX_RECORDING_FILE_SIZE_IN_MB = 50f ;
20+
21+ // initial file size of recording file
22+ private final float INITIAL_FILE_SIZE_IN_MB = 1.3f ;
23+
24+ // converted max file size
25+ private final float MAX_RECORDING_FILE_SIZE_IN_BYTES
26+ = (MAX_RECORDING_FILE_SIZE_IN_MB - INITIAL_FILE_SIZE_IN_MB ) * 1024 * 1024 ;
27+
28+ // keeps track of recording file size
29+ private int recordingFileSizeCounterInBytes = 0 ;
30+
1831 private File saveFile = null ;
1932 private DataOutputStream dataOutputStreamInstance = null ;
2033
@@ -25,7 +38,7 @@ public AudioDataSaver() {
2538
2639 @ Override
2740 public void start () {
28- if (null != saveFile ) {
41+ if (null != saveFile ) {
2942 if (saveFile .exists ()) {
3043 saveFile .delete ();
3144 }
@@ -36,7 +49,7 @@ public void start() {
3649 }
3750
3851 try {
39- BufferedOutputStream bufferedStreamInstance = new BufferedOutputStream (
52+ BufferedOutputStream bufferedStreamInstance = new BufferedOutputStream (
4053 new FileOutputStream (this .saveFile ));
4154 dataOutputStreamInstance = new DataOutputStream (bufferedStreamInstance );
4255 } catch (FileNotFoundException e ) {
@@ -48,8 +61,14 @@ public void start() {
4861 @ Override
4962 public void onAudioDataReceived (byte [] data , int length ) {
5063 try {
51- if (null != dataOutputStreamInstance ) {
64+ if (null != dataOutputStreamInstance ) {
65+ if (recordingFileSizeCounterInBytes >= MAX_RECORDING_FILE_SIZE_IN_BYTES ) {
66+ stop ();
67+ start ();
68+ recordingFileSizeCounterInBytes = 0 ;
69+ }
5270 dataOutputStreamInstance .write (data , 0 , length );
71+ recordingFileSizeCounterInBytes += length ;
5372 }
5473 } catch (IOException e ) {
5574 Log .e (TAG , "IO Exception on saving audio file " + saveFile .toString (), e );
@@ -58,7 +77,7 @@ public void onAudioDataReceived(byte[] data, int length) {
5877
5978 @ Override
6079 public void stop () {
61- if (null != dataOutputStreamInstance ) {
80+ if (null != dataOutputStreamInstance ) {
6281 try {
6382 dataOutputStreamInstance .close ();
6483 } catch (IOException e ) {
0 commit comments