2

I'm using media player and I need to query current position once a 50ms interval, however, getCurrentPosition seems to be updated only once a second or so if I play .ogg format. Here is extract from the log:

05-16 13:29:28.551    9275-9275/com.some.app D/highlight﹕ Current position is: 0
05-16 13:29:28.603    9275-9275/com.some.app D/highlight﹕ Current position is: 1014
05-16 13:29:28.651    9275-9275/com.some.app D/highlight﹕ Current position is: 1014
05-16 13:29:28.703    9275-9275/com.some.app D/highlight﹕ Current position is: 1014
...
05-16 13:29:29.343    9275-9275/com.some.app D/highlight﹕ Current position is: 1014
05-16 13:29:29.391    9275-9275/com.some.app D/highlight﹕ Current position is: 1014
05-16 13:29:29.443    9275-9275/com.some.app D/highlight﹕ Current position is: 1014
05-16 13:29:29.491    9275-9275/com.some.app D/highlight﹕ Current position is: 1014
05-16 13:29:29.543    9275-9275/com.some.app D/highlight﹕ Current position is: 2033
05-16 13:29:29.595    9275-9275/com.some.app D/highlight﹕ Current position is: 2033
05-16 13:29:29.643    9275-9275/com.some.app D/highlight﹕ Current position is: 2033
...
05-16 13:29:30.555    9275-9275/com.some.app D/highlight﹕ Current position is: 3037
05-16 13:29:30.623    9275-9275/com.some.app D/highlight﹕ Current position 

But when I use .mp3 format the position is updated once in 20 ms or so, which works for me:

05-17 05:45:04.459    5132-5144/com.some.app D/highlight 104
05-17 05:45:04.475    5132-5144/com.some.app D/highlight 104
05-17 05:45:04.479    5132-5144/com.some.app D/highlight 104
05-17 05:45:04.491    5132-5144/com.some.app D/highlight 131
05-17 05:45:04.499    5132-5144/com.some.app D/highlight 131
05-17 05:45:04.511    5132-5144/com.some.app D/highlight 131
05-17 05:45:04.519    5132-5144/com.some.app D/highlight 157
05-17 05:45:04.535    5132-5144/com.some.app D/highlight 157
05-17 05:45:04.539    5132-5144/com.some.app D/highlight 157
05-17 05:45:04.555    5132-5144/com.some.app D/highlight 157
05-17 05:45:04.559    5132-5144/com.some.app D/highlight 183
05-17 05:45:04.575    5132-5144/com.some.app D/highlight 183
05-17 05:45:04.579    5132-5144/com.some.app D/highlight 183
05-17 05:45:04.595    5132-5144/com.some.app D/highlight 183

I'm going to use mp3 now but I'm wondering why the difference, in case I'd need to use .ogg how can I get the same udpate rate?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • I have a feeling this has to do with buffers getting flushed at a lower level. If you are playing audio, you could try [AudioTrack](http://developer.android.com/reference/android/media/AudioTrack.html) – pathfinderelite May 16 '15 at 13:47
  • @pathfinderelite, thanks, I play `.ogg` format. Audio track supports only PCM, so I'd need to convert `.ogg` into `PCM` before playing. That's an additional work. But maybe that would be the only way if no one suggests a better solution – Max Koretskyi May 16 '15 at 14:02
  • @pathfinderelite, I've found the cause of the problem, see my update, but now different questions arises. – Max Koretskyi May 17 '15 at 06:54
  • I do not know why there is a difference in behavior. However, this may also vary by Android OS version, and so I would be very careful about relying on frequent updates of the position. – CommonsWare May 18 '15 at 22:54
  • @CommonsWare, thanks! I'm trying to implement the feature described in [my this question](http://stackoverflow.com/questions/30263709/is-using-countdowntimer-to-add-styles-every-few-milliseconds-a-correct-approach) so I need to have updated time every 50ms. Do you think that keeping own `currentPosition` variable, setting it to media player's current position when play starts and then updating it in my code on looper is a better and doable approach? – Max Koretskyi May 19 '15 at 08:23
  • AFAIAC, the right answer is to not be writing this like an Android app, but rather like a high-end Android game, using C/C++, OpenSL, OpenGL, and kin. – CommonsWare May 19 '15 at 10:37
  • @CommonsWare, I see, thanks. That approach would probably work for a large corporation, but for a small startup this is an overkill I guess – Max Koretskyi May 19 '15 at 10:42
  • Well, you're certainly welcome to use the approaches in that other question, except doing your highlights based on the number of milliseconds since you asked for playback to begin. However, that just adds to the inaccuracies. Now, in addition to the fact that you might be as much as 16ms off from the 60fps `View` hierarchy, you have whatever gap there is between when you request playback to start and when it actually does start. You seem to want "every few milliseconds" accuracy; that is not going to be possible in Java/`View`/`MediaPlayer`-based stuff. – CommonsWare May 19 '15 at 11:17
  • It may be that, in your experiments, that the accuracy you get is deemed good enough, and in that case, you're fine. I am just trying to take your "every few milliseconds" objective into account, and I can't see how to accomplish that without dropping down to the NDK and side-stepping the `View` hierarchy and `MediaPlayer`. – CommonsWare May 19 '15 at 11:18
  • @CommonsWare, yeah, it seems that the accuracy I get is enough. In my webapp, I query for `currentPosition` every 4ms, that's why I wanted to do the same in android. However, I know see that having 50ms interval still works for me since no words are pronounced faster than 50ms. Thanks for your help! And a great book! Best! – Max Koretskyi May 19 '15 at 12:35
  • "I know see that having 50ms interval still works for me since no words are pronounced faster than 50ms" -- clearly, I'm not the one speaking on the recording. :-) Thanks for the kind words! – CommonsWare May 19 '15 at 12:37
  • @CommonsWare, _clearly, I'm not the one speaking on the recording_ - I think I'm lucky then with a guy making recordings) – Max Koretskyi May 19 '15 at 12:48

0 Answers0