From 86660dd7cb76c8c4c7dd5a73c091bb8eda2a08ba Mon Sep 17 00:00:00 2001 From: Kenichi Naito Date: Fri, 22 Jun 2018 18:54:45 +0900 Subject: [PATCH] CB-12849: checking mediaState in destroy method, and moving file by stream when renameTo failing (#168) --- src/android/AudioPlayer.java | 42 ++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/android/AudioPlayer.java b/src/android/AudioPlayer.java index 861421e7..b40e281e 100644 --- a/src/android/AudioPlayer.java +++ b/src/android/AudioPlayer.java @@ -130,7 +130,9 @@ public void destroy() { this.player = null; } if (this.recorder != null) { - this.stopRecording(true); + if (this.state != STATE.MEDIA_STOPPED) { + this.stopRecording(true); + } this.recorder.release(); this.recorder = null; } @@ -197,8 +199,44 @@ public void moveFile(String file) { if (size == 1) { String logMsg = "renaming " + this.tempFile + " to " + file; LOG.d(LOG_TAG, logMsg); + File f = new File(this.tempFile); - if (!f.renameTo(new File(file))) LOG.e(LOG_TAG, "FAILED " + logMsg); + if (!f.renameTo(new File(file))) { + + FileOutputStream outputStream = null; + File outputFile = null; + try { + outputFile = new File(file); + outputStream = new FileOutputStream(outputFile); + FileInputStream inputStream = null; + File inputFile = null; + try { + inputFile = new File(this.tempFile); + LOG.d(LOG_TAG, "INPUT FILE LENGTH: " + String.valueOf(inputFile.length()) ); + inputStream = new FileInputStream(inputFile); + copy(inputStream, outputStream, false); + } catch (Exception e) { + LOG.e(LOG_TAG, e.getLocalizedMessage(), e); + } finally { + if (inputStream != null) try { + inputStream.close(); + inputFile.delete(); + inputFile = null; + } catch (Exception e) { + LOG.e(LOG_TAG, e.getLocalizedMessage(), e); + } + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (outputStream != null) try { + outputStream.close(); + LOG.d(LOG_TAG, "OUTPUT FILE LENGTH: " + String.valueOf(outputFile.length()) ); + } catch (Exception e) { + LOG.e(LOG_TAG, e.getLocalizedMessage(), e); + } + } + } } // more than one file so the user must have pause recording. We'll need to concat files. else {