JAVA, play mp3 by URL

2019-06-14 07:51发布

I would like to create player that play mp3 music from the internet by url. I tried this, but it doesn't work:

import java.net.URL;
import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;


public class Player {

  public static void main(String[] argv) throws Exception {

        URL url = new URL("http://stream10.jamendo.com/stream/247/mp31/07%20-%20Both%20-%20Je%20le%20veux%20aussi.mp3");
        AudioStream as = new AudioStream (url.openStream());
        AudioData data = as.getData();
        ContinuousAudioDataStream cas = new ContinuousAudioDataStream (data);
        AudioPlayer.player.start(cas);

  }

}

I got error:

Exception in thread "main" java.io.IOException: could not create audio stream from input stream
        at sun.audio.AudioStream.<init>(AudioStream.java:65)
        at Player.main(Player.java:27)
Java Result: 1

How could I solve this problem?

1条回答
聊天终结者
2楼-- · 2019-06-14 08:47

I believe that for this specific library you have chosen, the Sun one, there is not support for mp3 and it comes down to exactly that. As @Treebranch suggests, the better approach is to look at other APIs which could possibly handle mp3s.

From the JavaZoom page:

"MP3SPI is a Java Service Provider Interface that adds MP3 (MPEG 1/2/2.5 Layer 1/2/3) audio format support for Java Platform".

查看更多
登录 后发表回答