@@ -15,6 +15,19 @@ public class AudioDataSaver implements AudioDataReceivedListener {
15
15
16
16
private static final String TAG = AudioDataSaver .class .getSimpleName ();
17
17
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
+
18
31
private File saveFile = null ;
19
32
private DataOutputStream dataOutputStreamInstance = null ;
20
33
@@ -25,7 +38,7 @@ public AudioDataSaver() {
25
38
26
39
@ Override
27
40
public void start () {
28
- if (null != saveFile ) {
41
+ if (null != saveFile ) {
29
42
if (saveFile .exists ()) {
30
43
saveFile .delete ();
31
44
}
@@ -36,7 +49,7 @@ public void start() {
36
49
}
37
50
38
51
try {
39
- BufferedOutputStream bufferedStreamInstance = new BufferedOutputStream (
52
+ BufferedOutputStream bufferedStreamInstance = new BufferedOutputStream (
40
53
new FileOutputStream (this .saveFile ));
41
54
dataOutputStreamInstance = new DataOutputStream (bufferedStreamInstance );
42
55
} catch (FileNotFoundException e ) {
@@ -48,8 +61,14 @@ public void start() {
48
61
@ Override
49
62
public void onAudioDataReceived (byte [] data , int length ) {
50
63
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
+ }
52
70
dataOutputStreamInstance .write (data , 0 , length );
71
+ recordingFileSizeCounterInBytes += length ;
53
72
}
54
73
} catch (IOException e ) {
55
74
Log .e (TAG , "IO Exception on saving audio file " + saveFile .toString (), e );
@@ -58,7 +77,7 @@ public void onAudioDataReceived(byte[] data, int length) {
58
77
59
78
@ Override
60
79
public void stop () {
61
- if (null != dataOutputStreamInstance ) {
80
+ if (null != dataOutputStreamInstance ) {
62
81
try {
63
82
dataOutputStreamInstance .close ();
64
83
} catch (IOException e ) {
0 commit comments