fix resource leak(s) on exception
This commit is contained in:
+10
-2
@@ -141,9 +141,12 @@ class MetadataStripper {
|
||||
val tempOutputFile = File.createTempFile("stripped_video_", ".mp4", context.cacheDir)
|
||||
|
||||
val extractor = MediaExtractor()
|
||||
var muxer: MediaMuxer? = null
|
||||
var muxerStarted = false
|
||||
try {
|
||||
extractor.setDataSource(tempInputFile.absolutePath)
|
||||
|
||||
val muxer = MediaMuxer(tempOutputFile.absolutePath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4)
|
||||
muxer = MediaMuxer(tempOutputFile.absolutePath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4)
|
||||
|
||||
val trackIndexMap = mutableMapOf<Int, Int>()
|
||||
for (i in 0 until extractor.trackCount) {
|
||||
@@ -153,6 +156,7 @@ class MetadataStripper {
|
||||
}
|
||||
|
||||
muxer.start()
|
||||
muxerStarted = true
|
||||
|
||||
val bufferSize = 1024 * 1024
|
||||
val buffer = java.nio.ByteBuffer.allocate(bufferSize)
|
||||
@@ -179,9 +183,13 @@ class MetadataStripper {
|
||||
}
|
||||
|
||||
muxer.stop()
|
||||
muxer.release()
|
||||
muxerStarted = false
|
||||
} finally {
|
||||
if (muxerStarted) runCatching { muxer?.stop() }
|
||||
muxer?.release()
|
||||
extractor.release()
|
||||
tempInputFile.delete()
|
||||
}
|
||||
|
||||
Log.d("MetadataStripper", "Stripped metadata from video")
|
||||
tempOutputFile.toUri()
|
||||
|
||||
Reference in New Issue
Block a user