2012-02-16 2 views
0

원격 뮤직 파일 (HTTP)로 재생 목록을 수신 한 뮤직 플레이어에서 작업하고 있습니다.MP3 파일을 Java로 미리 버퍼링하는 방법

첫 번째 트랙을 스트리밍 시작하고 싶습니다. 노래를 충분히 버퍼링하여 재생하면, 다음 노래를 메모리에 버퍼링하기 시작해야합니다. 이것은 프로그램이 실행해야하는 불안정한 인터넷 연결을 보충하는 것입니다.

전체 파일을 다운로드하기 위해 BufferedInputStream에 어떻게 말합니까?

이 문제를 해결하는 방법에 대한 다른 제안 사항도 기쁩니다.

저는 오디오를 재생할 때 JLayer/BasicPlayer 라이브러리를 사용하고 있습니다. 이것이 코드입니다.

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

봐와 OutputStream ByteArrayOutputStream을 사용하여 시작하는 것이 좋습니다. 파일 크기를 걱정하고 메모리에 보관하려면 FileOutputStream에 파일을 작성한 다음 파일 afterwoods를 열어 읽습니다. common-io의 CopyUtils를 참조하십시오. – Steven

+0

와우 아주 좋은 질문이 찾고 있었어요! – GOXR3PLUS

답변

0

좋아, 내 질문에 대답하기 위해, 여기에 작동 구현 :의 InputStream 사이의 복사에

/** 
* <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

Here 당신이 자바에서 오디오 파일을 사전 버퍼링하는 방법에 대한 예제를 얻을 것이다

관련 문제