Attempt to fix inaccurate track positions #42

cleanup
This commit is contained in:
fd 2015-03-10 22:55:58 -04:00
parent f27744ceaa
commit ab9ea57a4c

View File

@ -98,12 +98,20 @@ public class OpenALStreamPlayer {
/** The music length. */ /** The music length. */
long musicLength = -1; long musicLength = -1;
/** The time of the last update, in ms. */
long lastUpdateTime = getTime(); /** The assumed time of when the music position would be 0 */
long syncStartTime;
/** The offset time. */
long offsetTime = 0; /** The last value that was returned the music position */
float lastUpdatePosition = 0;
/** The average difference between the sync time and the music position */
float avgDiff;
/** The time when it was paused */
long pauseTime;
/** /**
* Create a new player to work on an audio stream * Create a new player to work on an audio stream
* *
@ -252,7 +260,6 @@ public class OpenALStreamPlayer {
int bufferLength = AL10.alGetBufferi(bufferIndex, AL10.AL_SIZE); int bufferLength = AL10.alGetBufferi(bufferIndex, AL10.AL_SIZE);
playedPos += bufferLength; playedPos += bufferLength;
lastUpdateTime = getTime();
if (musicLength > 0 && playedPos > musicLength) if (musicLength > 0 && playedPos > musicLength)
playedPos -= musicLength; playedPos -= musicLength;
@ -377,7 +384,6 @@ public class OpenALStreamPlayer {
AL10.alSourceQueueBuffers(source, bufferNames); AL10.alSourceQueueBuffers(source, bufferNames);
AL10.alSourcePlay(source); AL10.alSourcePlay(source);
lastUpdateTime = getTime();
} }
/** /**
@ -391,24 +397,19 @@ public class OpenALStreamPlayer {
return timePosition; return timePosition;
} }
float lastUpdatePosition = 0; /**
long syncStartTime = getTime(); //the assumed start time of the music * Return the current playing position in the sound
float avgDiff; *
long pauseTime; * @return The current position in seconds.
*/
public float getPosition() { public float getPosition() {
float thisPosition = getALPosition(); float thisPosition = getALPosition();
long thisTime = getTime(); long thisTime = getTime();
float dxPosition = thisPosition - lastUpdatePosition; float dxPosition = thisPosition - lastUpdatePosition;
long dxTime = thisTime - syncStartTime; long dxTime = thisTime - syncStartTime;
//hard reset //hard reset
if (Math.abs(thisPosition - dxTime / 1000f) > 1 / 2f) { if (Math.abs(thisPosition - dxTime / 1000f) > 1 / 2f) {
System.out.println("Time HARD Reset" + " " + thisPosition + " "
+ (dxTime / 1000f) + " "
+ (int) (thisPosition * 1000 - (dxTime)) + " "
+ (int) (thisPosition * 1000 - (thisTime - syncStartTime)));
syncStartTime = thisTime - ((long) (thisPosition * 1000)); syncStartTime = thisTime - ((long) (thisPosition * 1000));
dxTime = thisTime - syncStartTime; dxTime = thisTime - syncStartTime;
avgDiff = 0; avgDiff = 0;
@ -419,18 +420,14 @@ public class OpenALStreamPlayer {
syncStartTime -= (int) (avgDiff/2); syncStartTime -= (int) (avgDiff/2);
dxTime = thisTime - syncStartTime; dxTime = thisTime - syncStartTime;
lastUpdatePosition = thisPosition; lastUpdatePosition = thisPosition;
//System.out.println(diff);
} }
//System.out.println("AL2Tme:"+(dxTime/1000f)+" "+thisPosition+" time:"+getTime()+" "+avgDiff+" "+syncStartTime);
return dxTime / 1000f; return dxTime / 1000f;
} }
/** /**
* Processes a track pause. * Processes a track pause.
*/ */
public void pausing() { public void pausing() {
offsetTime = getTime() - lastUpdateTime;
pauseTime = getTime(); pauseTime = getTime();
} }
@ -438,7 +435,6 @@ public class OpenALStreamPlayer {
* Processes a track resume. * Processes a track resume.
*/ */
public void resuming() { public void resuming() {
lastUpdateTime = getTime() - offsetTime;
syncStartTime += getTime() - pauseTime; syncStartTime += getTime() - pauseTime;
} }