Код: Выделить всё
val videoUri = Uri.parse("android.resource://" + packageName + "/" + R.raw.video_h)
captureVideoFrames(this@OutsideCamActivity, videoUri)
Код: Выделить всё
private fun captureAndDisplayFrames(videoUri: Uri) {
lifecycleScope.launch(Dispatchers.IO) {
val retriever = MediaMetadataRetriever()
try {
retriever.setDataSource(this@OutsideCamActivity, videoUri)
// Get the video duration
val videoDuration =
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toLong() ?: 0L
var currentPosition = 0L
// Capture frames at regular intervals
while (currentPosition < videoDuration) {
// Extract frame at the current position
val frame: Bitmap? = retriever.getFrameAtTime(currentPosition * 1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC)
// Check if the frame was successfully retrieved
if (frame != null) {
// Update the ImageView on the main thread
withContext(Dispatchers.Main) {
imgOutH.setImageBitmap(frame)
}
} else {
Log.e("FrameExtraction", "No frame found at time: $currentPosition")
}
// Increment position (e.g., by 1 second or as per your requirement)
currentPosition += 1000 // Move to the next second
}
} catch (e: IllegalArgumentException) {
Log.e("FrameExtraction", "Invalid video source: ${e.message}")
} catch (e: IllegalStateException) {
Log.e("FrameExtraction", "Retriever not configured properly: ${e.message}")
} catch (e: Exception) {
Log.e("FrameExtraction", "Error retrieving frame: ${e.message}")
} finally {
retriever.release() // Always release the retriever resource
}
}
}
Вместо установки в ImageView мне нужно выполнить некоторую обработку с помощью изображение, которое также работает нормально. Проблема с захватом всех кадров.
Подробнее здесь: https://stackoverflow.com/questions/790 ... -video-mp4
Мобильная версия