djbe/ElectroJam

MediaPlayer provides incorrect duration in Android 2.3

Closed this issue · 2 comments

djbe commented

getDuration() gives an incorrect duration for samples in android 2.3, namely a lower value. This leads to the skip limit being lower than necessary and thus to samples being cut short while playing.
Possible fixes:

  • some forum posts mention using 44.1KHz samples, which we might also do in #2
  • embed the skipLimit (or duration) in the application, in for example res/values/associations.xml, instead of dynamically calculating it.
djbe commented

well, option #1 did not seem to fix this (tried the new higher qual. sounds). It could be an issue with the emulator, as I never noticed this problem while testing it on a Galaxy S+ during the Athens week. Need some real world testing with 2.3 device(s).

djbe commented

Did quite a few tests:

  • Tested on 2.3 on a real device, issue does exist, it's not just the emulator.
  • According to some forum posts it also exists in 3.0
  • Using 44.1KHz samples does not help
  • Samples in other formats such as MP3 and AAC do report a correct duration, if slightly too long.

I compared the sample duration for all the supported formats and found these results:

True DurationFounddiffMP3diffAACdiff
75004223-3277757676752323
75004525-2975757676752323
37502865-885381666377323
75005711-1789757676752323
37502435-1315381666377323
37502845-905381666377323
37502505-1245381666377323
37502993-757381666377323
18752689814193358190429
18752244369193358190429
18752446571193358190429
375049751225381464378535
75006938-562757676752323
75007439-61757676752323
75005360-2140757676752323
75007769269757676752323
75004913-2587757676752323
75007083-417757676752323
75008144644757676752323
7500754343757676752323
37503213-537381666377323
37503279-471381666377323
37503279-471381666377323
37502548-1202381666377323
3466463004187237226
2517134623136227928
1875194065193358190429
1875831-1044193358190429
18751629-246193358190429
18751400-475193358190429
Average:-620,067,825,1

The duration reported for MP3s and AAC files is actually correct, the files are a few milliseconds longer after conversion. We can take this into account when creating sounds so we don't get glitches.

To sum up: use samples encoded in AAC (LC) at 64Kbps, but take into account that we lose ~25ms at the end.