androidx / media

Jetpack Media3 support libraries for media use cases, including ExoPlayer, an extensible media player for Android
https://developer.android.com/media/media3
Apache License 2.0
1.72k stars 413 forks source link

Get duration and size of an mp3 file without playback? #1885

Open akshdeep-singh opened 1 week ago

akshdeep-singh commented 1 week ago

There is an option to retrieve metadata without playback as mentioned in the docs.

Is it possible to get duration and size of an mp3 file (https in my case) without playback?

My use case is that I want to show duration and size of all my playlist items.

icbaker commented 1 day ago

For duration, you could try MediaExtractorCompat, using getTrackFormat(0) and looking for KEY_DURATION.

This will only work on the main branch for now (because you need https://github.com/androidx/media/commit/df07fa35d835c1226a6bfeb4783f944c7f00e858), so you will need to build the library locally. It will be included in 1.6.0.

You could also use the framework MediaExtractor implementation for this if you don't need to use media3.


By size, I assume you mean file size? We don't have an easy way to expose that in media3. You can probably do some combination of (configuring this via passing a custom DataSource.Factory instance to the MediaExtractorCompat constructor):

  1. Use StatsDataSource wrapped around DefaultDataSource and grab getBytesRead() after loading each file.
  2. Use StatsDataSource.getLastResponseHeaders() after loading each file instead, and read the Content-Length HTTP header.