开发者

java+shoutcast stream

i've already search the forums before posting this(i did开发者_Go百科 not find an answer to my problem , thus making this post). i wanna make a java app which plays the audio stream from a shoutcast server. I;ve looked on the web and found that javazoom will be useful to what i wanna do. So i've downloaded their packages and started experimentation. I've come up with this, but i get a "javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input URL"

Here is the code :

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

/**
 *
 * @author George
 */


import  java.io.*;
import java.util.Map;
import java.net.*;
import javax.sound.sampled.*;

public class play {
    public  void testPlay(String filename)
{
  try {

   // File file = new File(filename);
      URL file= new URL(filename);
    AudioInputStream in= AudioSystem.getAudioInputStream(file);
    AudioInputStream din = null;
    AudioFormat baseFormat = in.getFormat();
    AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 
                                                                                  baseFormat.getSampleRate(),
                                                                                  16,
                                                                                  baseFormat.getChannels(),
                                                                                  baseFormat.getChannels() * 2,
                                                                                  baseFormat.getSampleRate(),
                                                                                  false);
    din = AudioSystem.getAudioInputStream(decodedFormat, in);
    // Play now. 
    rawplay(decodedFormat, din);
    in.close();


    din = AudioSystem.getAudioInputStream(decodedFormat, in);
// DecodedMpegAudioInputStream properties
if (din instanceof javazoom.spi.PropertiesContainer)
{
    Map properties = ((javazoom.spi.PropertiesContainer) din).properties(); 
    float[] equalizer = (float[]) properties.get("mp3.equalizer");
    equalizer[0] = (float) 0.5;
    equalizer[31] = (float) 0.25; 
}




  } catch (Exception e)
    {
        System.out.println(e);
    }
} 

private void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException,                                                                                                LineUnavailableException
{
  byte[] data = new byte[4096];
  SourceDataLine line = getLine(targetFormat); 
  if (line != null)
  {
    // Start
    line.start();
    int nBytesRead = 0, nBytesWritten = 0;
    while (nBytesRead != -1)
    {
        nBytesRead = din.read(data, 0, data.length);
        if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
    }
    // Stop
    line.drain();
    line.stop();
    line.close();
    din.close();
  } 
}

private SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException
{
  SourceDataLine res = null;
  DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
  res = (SourceDataLine) AudioSystem.getLine(info);
  res.open(audioFormat);
  return res;
} 
}


Are you sure the URL you have is the one for the MP3 data. You should be able to save some data from the stream locally and then play it from a standard media player app as an MP3 file. If not, you probably have a bad stream or one for some other format.

Also, are you using the Audio library implementation provided by JavaZoom to get MP3 support? Java does not have it by default.

You might also try and read the HTTP response headers from the shoutcast stream, it might have more detailed information on the content being served.

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜