1

これを投稿する前に、すでにフォーラムを検索しました(問題に対する回答が見つからなかったため、この投稿を作成しました)。シャウトキャスト サーバーからオーディオ ストリームを再生する Java アプリを作りたいです。Web を調べたところ、やりたいことに javazoom が役立つことがわかりました。だから私は彼らのパッケージをダウンロードして実験を始めました。これを思いついたのですが、「javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input URL」が発生します。

コードは次のとおりです。

    /*
 * 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;
} 
}
4

1 に答える 1

1

お持ちの URL は MP3 データの URL です。ストリームから一部のデータをローカルに保存し、標準のメディア プレーヤー アプリから MP3 ファイルとして再生できるはずです。そうでない場合は、ストリームが不良であるか、別の形式のストリームである可能性があります。

また、MP3 サポートを取得するために、JavaZoom が提供する Audio ライブラリの実装を使用していますか? Java にはデフォルトではありません。

また、shoutcast ストリームから HTTP 応答ヘッダーを読み取ってみると、提供されているコンテンツに関するより詳細な情報が含まれている可能性があります。

幸運を。

于 2011-07-13T12:56:15.320 に答える