How to keep track of audio playback position?
<p>I created a thread to play an mp3 file in Java by converting it to an array of bytes. </p>

<p>I'm wondering if I can keep track of the current play position as the mp3 is being played.</p>

<p>First, I set up my music stream like so:</p>

<pre><code>try {
        AudioInputStream in = AudioSystem.getAudioInputStream(file);

        musicInputStream = AudioSystem.getAudioInputStream(MUSIC_FORMAT, in);

        DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, MUSIC_FORMAT);
        musicDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
        musicDataLine.open(MUSIC_FORMAT);
        musicDataLine.start();            
        startMusicThread();
    } catch(Exception e) {
        e.printStackTrace();
    }
</code></pre>

<p>Next, my music thread looks like this:</p>

<pre><code>private class MusicThread extends Thread {      
    byte musicBuffer[] = new byte[BUFFER_SIZE];
    public void run() {
        try {
            int musicCount = 0;
            while(writeOutput){
                if(writeMusic &amp;&amp; (musicCount = musicInputStream.read(musicBuffer, 0, musicBuffer.length)) &gt; 0){
                    musicDataLine.write(musicBuffer, 0, musicCount);
                }
            }
        } catch (Exception e) {
            System.out.println("AudioStream Exception - Music Thread"+e);
            e.printStackTrace();
        }
    }
}
</code></pre>

<hr>

<p>I thought of one possibility, to create another thread with a timer that slowly ticks down, second by second, to show the remaining amount of time for the mp3 song. But that doesn't seem like a good solution at all.</p>