Skip to content

Commit 86660dd

Browse files
knight9999shazron
authored andcommitted
CB-12849: checking mediaState in destroy method, and moving file by stream when renameTo failing (#168)
1 parent 524c337 commit 86660dd

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/android/AudioPlayer.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ public void destroy() {
130130
this.player = null;
131131
}
132132
if (this.recorder != null) {
133-
this.stopRecording(true);
133+
if (this.state != STATE.MEDIA_STOPPED) {
134+
this.stopRecording(true);
135+
}
134136
this.recorder.release();
135137
this.recorder = null;
136138
}
@@ -197,8 +199,44 @@ public void moveFile(String file) {
197199
if (size == 1) {
198200
String logMsg = "renaming " + this.tempFile + " to " + file;
199201
LOG.d(LOG_TAG, logMsg);
202+
200203
File f = new File(this.tempFile);
201-
if (!f.renameTo(new File(file))) LOG.e(LOG_TAG, "FAILED " + logMsg);
204+
if (!f.renameTo(new File(file))) {
205+
206+
FileOutputStream outputStream = null;
207+
File outputFile = null;
208+
try {
209+
outputFile = new File(file);
210+
outputStream = new FileOutputStream(outputFile);
211+
FileInputStream inputStream = null;
212+
File inputFile = null;
213+
try {
214+
inputFile = new File(this.tempFile);
215+
LOG.d(LOG_TAG, "INPUT FILE LENGTH: " + String.valueOf(inputFile.length()) );
216+
inputStream = new FileInputStream(inputFile);
217+
copy(inputStream, outputStream, false);
218+
} catch (Exception e) {
219+
LOG.e(LOG_TAG, e.getLocalizedMessage(), e);
220+
} finally {
221+
if (inputStream != null) try {
222+
inputStream.close();
223+
inputFile.delete();
224+
inputFile = null;
225+
} catch (Exception e) {
226+
LOG.e(LOG_TAG, e.getLocalizedMessage(), e);
227+
}
228+
}
229+
} catch (Exception e) {
230+
e.printStackTrace();
231+
} finally {
232+
if (outputStream != null) try {
233+
outputStream.close();
234+
LOG.d(LOG_TAG, "OUTPUT FILE LENGTH: " + String.valueOf(outputFile.length()) );
235+
} catch (Exception e) {
236+
LOG.e(LOG_TAG, e.getLocalizedMessage(), e);
237+
}
238+
}
239+
}
202240
}
203241
// more than one file so the user must have pause recording. We'll need to concat files.
204242
else {

0 commit comments

Comments
 (0)