Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7568913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:02:54+00:00 2026-05-30T15:02:54+00:00

I’m working on a music player, which receives a playlist with remote mp3 files

  • 0

I’m working on a music player, which receives a playlist with remote mp3 files (HTTP) and play them subsequently.

I want to have it start streaming the first track, if enough of the song is buffered to play it through, it should already begin to buffer the following song into memory. That is to make up for the unstable internet connection the program is supposed to run on.

How do I tell the BufferedInputStream to just download the whole file?

I’m happy to hear other suggestions on how to solve this, too.

I’m using the JLayer/BasicPlayer library to play audio, this is the code.

String mp3Url = "http://ia600402.us.archive.org/6/items/Stockfinster.-DeadLinesutemos025/01_Push_Push.mp3";
URL url = new URL(mp3Url);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

BasicPlayer player = new BasicPlayer();
player.open(bis);
player.play();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:02:56+00:00Added an answer on May 30, 2026 at 3:02 pm

    Ok, to answer my question, here’s a working implementation:

    /**
     * <code>DownloaderInputStream</code>
     */
    public class DownloaderInputStream extends InputStream {
    
        /**
         * <code>IDownloadNotifier</code> - download listener.
         */
        public static interface IDownloadListener {
    
            /**
             * Notifies about download completion.
             *
             * @param buf
             * @param offset
             * @param length
             */
            public void onComplete(final byte[] buf, final int offset, final int length);
        }
    
        /**
         * <code>ByteArrayOutputStreamX</code> - {@link ByteArrayOutputStream}
         * extension that exposes buf variable (to avoid copying).
         */
        private final class ByteArrayOutputStreamX extends ByteArrayOutputStream {
    
            /**
             * Constructor.
             *
             * @param size
             */
            public ByteArrayOutputStreamX(final int size) {
                super(size);
            }
    
            /**
             * Returns inner buffer.
             *
             * @return inner buffer
             */
            public byte[] getBuffer() {
                return buf;
            }
        }
    
        private final class Downloader extends Object implements Runnable {
            // fields
    
            private final InputStream is;
    
            /**
             * Constructor.
             *
             * @param is
             */
            public Downloader(final InputStream is) {
                this.is = is;
            }
    
            // Runnable implementation
            public void run() {
                int read = 0;
    
                byte[] buf = new byte[16 * 1024];
    
                try {
                    while ((read = is.read(buf)) != -1) {
                        if (read > 0) {
                            content.write(buf, 0, read);
    
                            downloadedBytes += read;
                        } else {
                            Thread.sleep(50);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                listener.onComplete(content.getBuffer(), 0 /*
                         * offset
                         */, downloadedBytes);
            }
        }
        // fields
        private final int contentLength;
        private final IDownloadListener listener;
        // state
        private ByteArrayOutputStreamX content;
        private volatile int downloadedBytes;
        private volatile int readBytes;
    
        /**
         * Constructor.
         *
         * @param contentLength
         * @param is
         * @param listener
         */
        public DownloaderInputStream(final int contentLength, final InputStream is, final IDownloadListener listener) {
            this.contentLength = contentLength;
            this.listener = listener;
    
            this.content = new ByteArrayOutputStreamX(contentLength);
            this.downloadedBytes = 0;
            this.readBytes = 0;
    
            new Thread(new Downloader(is)).start();
        }
    
        /**
         * Returns number of downloaded bytes.
         *
         * @return number of downloaded bytes
         */
        public int getDownloadedBytes() {
            return downloadedBytes;
        }
    
        /**
         * Returns number of read bytes.
         *
         * @return number of read bytes
         */
        public int getReadBytes() {
            return readBytes;
        }
    
        // InputStream implementation
        @Override
        public int available() throws IOException {
            return downloadedBytes - readBytes;
        }
    
        @Override
        public int read() throws IOException {
            // not implemented (not necessary for BasicPlayer)
            return 0;
        }
    
        @Override
        public int read(byte[] b, int off, int len) throws IOException {
            if (readBytes == contentLength) {
                return -1;
            }
    
            int tr = 0;
    
            while ((tr = Math.min(downloadedBytes - readBytes, len)) == 0) {
                try {
                    Thread.sleep(100);
                } catch (Exception e) {/*
                     * ignore
                     */
    
                }
            }
    
            byte[] buf = content.getBuffer();
    
            System.arraycopy(buf, readBytes, b, off, tr);
    
            readBytes += tr;
    
            return tr;
        }
    
        @Override
        public long skip(long n) throws IOException {
            // not implemented (not necessary for BasicPlayer)
            return n;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.