3

I'm developing a programm that streams music from the internet. When i stream a song, i don't have access to it's URL. I only get an input stream and some information like length in bytes and so forth. I want to implement a seekbar similar to those on youtube etc. Until now i've managed to display the seekbar as a JSlider with maximum being the length of the stream in bytes and the value changing according to the current position in bytes in the song.

Now BasicPlayer has a function called seek(long bytesToSkip) and i tried calling that, when the slider gets moved and released by user input. However seek() won't do anything. No log message, nothing. So i downloaded the source of BasicPlayer and found out, that seek(long bytesToSkip) calls skipBytes(long bytesToSkip) which checks if the data to be played is a file. If it's not, it'll do nothing.

So i removed that if clause to see where that would take me. Now I'm getting an exception at the line

initAudioInputStream();

This checks what kind of data it's dealing with and calls an appropriate method for that type of data. In my case

initAudioInputStream(InputStream inputStream) {
    m_audioInputStream = AudioSystem.getAudioInputStream(inputStream);
    m_audioFileFormat = AudioSystem.getAudioFileFormat(inputStream);
}

and there in the first of those two lines i get my exception. It's an EOFException, so i take it the stream hasn't "buffered" enough to get the audioInputStream. If that method requires the whole file, i guess i'm out of luck.

I really hope someone could help me make this work. I've been working on this for so long..

user778440
  • 31
  • 4
  • Well, you fundamentally can't skip to a portion of the audio that you haven't downloaded yet. And you also can't skip to a specific percentage of an MPEG stream without decoding it, because the relation between encoded and raw data is non-constant (and in current VBR streams, *very* non-constant). Youtube can offer skipping because they already know the entire data and can pre-compute approximate skip targets, but you can't do that either. So what are you really hoping to do? – Kilian Foth Sep 16 '11 at 12:37
  • ah.. Okay that makes sense. Too bad. I'm writing a program to stream from GrooveShark. With the LastFM Api i think it's even better than just streaming from their site. (They don't have a similar artists function or artist bio) But because I'm not GrooveShark i can't implement skipping, because i don't already know the entire data, right? Damn.. I thought i read something about mp3frames being encoded in a way that one can start from whatever frame and it still plays okay, so that you don't have to know the rest of the frames.. – user778440 Sep 16 '11 at 13:19
  • Yes, but first you have to hit a frame boundary! I suppose you could hunt around for a frame boundary by trying out adjacent offsets, and maybe the inaccuracy isn't too bad on the average, but it's a bit more work than you would expect at first. – Kilian Foth Sep 16 '11 at 13:24
  • You lost me there.. :) How would i even notice i've hit a frame boundary? – user778440 Sep 16 '11 at 14:36
  • Basically, when you restart the MPEG decoding, you don't get a format exception... – Kilian Foth Sep 16 '11 at 16:54

0 Answers0