In my android application(Audio Processing) i use a long type variable to calculate the position where it is being paused, but the application gave me strange results for large audio files. After putting some effort i found out that the position calculation is going out of limits in android. Though a long type in java has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807, i wondered why my calculation went out of limits.
After going through some posts i found that android support limited value ranges unlike java. But my question is how i can perform this calculation in android? Is there any other android specific data types?
This is the calculation i perform in my app:
position=((bufferSize*currentPosition)/duration);
As noted in my comment, the post you’re taking your information from is simply incorrect.
longis always 64 bits. There may be some limitations in NIO, but that’s a different matter.However, your problem can easily be explained without any trickery. Here’s your code again:
You haven’t said what the types of
bufferSizeandcurrentPositionare, but I’ll assume they’relong. If you’ve got a very large buffer and a “late”currentPositionvalue, it’s possible that the result of the multiplication will overflow into a negative number. When you divide that negative number byduration, you’ll still get a negative (but smaller in magnitude) number.If
bufferSizeandcurrentPositionareintvariables instead oflong, that overflow will occur much sooner. Indeed, if that’s the case then you may well just be able to fix your problem just by forcing the arithmetic to be done in 64 bits:You should work out what range of
currentPositionthis would work up to, based on your buffer size – I’d expect it to be fine for any sensibly-sized file length and buffer size combination, to be honest.If that doesn’t help, please post more information – including the variable types and what values you’re observing (for all variables, including the results).