devfemibadmus / mediasaver

Crafted using Flutter for a seamless UI/UX, backed by Kotlin for efficient performance. Our primary focus is on delivering fast responses, aesthetic appeal, and a lightweight user experience in this Media Saver app.

Home Page:https://devfemibadmus.blackstackhub.com/mediasaver

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I/Choreographer(25094): Skipped 717 frames! The application may be doing too much work on its main thread

devfemibadmus opened this issue · comments

Issue Description

I am opening this issue due to an error encountered in my web application. After extensive research and investigation, I failed to find a solution for a similar error. However, after delving deeper into the documentation, I discovered a resolution.

Error Message

I/Choreographer(25094): Skipped 717 frames! The application may be doing too much work on its main thread

Problem Overview

The error message suggests that the application is performing excessive work on the main thread, resulting in skipped frames and potential performance issues. This issue specifically arises when generating thumbnails from videos in Kotlin. Here are common reasons for encountering this error:

  1. Heavy Computation on Main Thread:
    If your application performs intensive tasks like video decoding or thumbnail extraction on the main thread, it can lead to UI freezes. Ensure that CPU-intensive tasks are offloaded to background threads or handled asynchronously.

  2. Bitmap Decoding:
    Decoding bitmaps from video frames on the main thread can be resource-intensive. Consider moving the bitmap decoding process to a background thread or utilizing a library that supports asynchronous decoding.

  3. I/O Operations:
    File-related operations, such as reading video files or accessing the file system, can cause delays on the main thread. Ensure that file I/O operations are performed asynchronously or on a separate thread.

  4. Memory Issues:
    Handling large bitmaps or maintaining a high memory footprint can lead to performance problems. Be cautious of memory usage and consider techniques like bitmap recycling or downsampling if necessary.

Code Snippet

Below is an example of code that may cause the issue:

private fun getVideoThumbnail(absolutePath: String): ByteArray {

        fun createVideoThumbnail(videoPath: String): Bitmap? {
            val retriever = MediaMetadataRetriever()

            try {
                retriever.setDataSource(videoPath)
                return retriever.getFrameAtTime()
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                retriever.release()
            }

            return null
        }
        try {
            val bitmap = createVideoThumbnail(absolutePath);
            if (bitmap != null){
                val stream = ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                bitmap.recycle();
                return stream.toByteArray();
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return ByteArray(0)
    }

Recommended Solution

Consider adapting your code to the following, which utilizes coroutines and moves the heavy computation to a background thread:

private suspend fun getVideoThumbnailAsync(absolutePath: String): ByteArray {
    return withContext(Dispatchers.Default) {
        try {
            val bitmap = createVideoThumbnailAsync(absolutePath)
            if (bitmap != null) {
                val stream = ByteArrayOutputStream()
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
                bitmap.recycle()
                return@withContext stream.toByteArray()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return@withContext ByteArray(0)
    }
}

While your specific logic might differ, addressing the issues mentioned above should aid in debugging and resolving the problem.