Efficient way to search for streaming audio over a network using InputStream

I play an audio file using jlGui BasicPlayer (it is based on Javasound). The file is in the Samba public folder, and I use Jcifs to access it. This gives me an InputStream .

 NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication( ... ); SmbFile f = new SmbFile( ... ); SmbFileInputStream audioIn = new SmbFileInputStream(f); int bufSize = 8096;//should I use f.length() here? audioBIS = new BufferedInputStream(audioIn, bufSize); audioBIS.mark(f.length()); //call BasicPlayer play(audioBIS); 

I need to be able to position the pointer anywhere in the file, like any regular player. The only solution I thought of is to use a BufferedInputStream and a combination of mark / reset / skip every time I need to move the pointer. As soon as I open the file and get Stream, I call the mark() method, so that the subsequent reset() will change at the beginning. Then with skip() I can go wherever I want.

 audioBIS.reset(); audioBIS.skip(newBytePosition); 

My problem is that the skip () call works as desired only if I specify a buffer large enough to contain the entire file.

Is there a more efficient way to do this?

+6
source share
2 answers

I was on the same path as now. The thing was that we had a server (and a shared SMB resource) containing thousands of audio files. These files must be played in the application.

I started with jCifs and modified the BasicPlayer source to work with SmbFile in the same way as with a file. This worked well, but when it comes to searches / skips, it really does not bother you. As long as you have good communication with the server, you should be fine.

I finished this solution, and instead installed tomcat6 on the server and deployed a small and simple servlet that would allow me to make requests for the file at a given position. The client machine will then accept the response as an InputStream and pass it on to BasicPlayer. It works much better and playback is instantaneous. The code is a little more than reasonable to insert here, but I would like to share it with you if you are interested.

+2
source

As an alternative. You can always just close the stream and recreate it. It seems you need to go fast, and if necessary you will return to position 0.

So keep an eye on where you are. While you are moving forward, hold the stream and jump with skip (). As soon as you need to go back, close this thread and create a new one and skip () to the desired position.

If the application does not work on backskipping all the time, you should be fine.

Wrap this in a nice new user thread, and you have a thread that supports moving forward and backward.

0
source

Source: https://habr.com/ru/post/921475/


All Articles