TCP流异常

时间:2019-04-06 15:46:49

标签: java tcp streaming

我想制作一个服务器-客户端程序,通过TCP传输音乐,但是运行代码后出现问题。

我的程序给了我这个例外:

Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1121) at online.AudioClient.main(AudioClient.java:18)

这是我的服务器代码:

public static void main(String[] args) throws IOException {
    String zenecim = "C:/Users/valaki/Music/Zene/Axel Thesleff - Bad Karma.wav";
    File soundFile = AudioUtil.getSoundFile(zenecim);
    System.out.println("Létrejött a soundfile");

    System.out.println("server: " + soundFile);

    ServerSocket serverSocker = new ServerSocket(6666);
    System.out.println("Socket");
    FileInputStream in = new FileInputStream(soundFile);
    if (serverSocker.isBound()) {
        System.out.println("if-ben");
        Socket client = serverSocker.accept();
        OutputStream out = client.getOutputStream();

        byte buffer[] = new byte[2048];
        int count;
        while ((count = in.read(buffer)) != -1) {
            out.write(buffer, 0, count);
            out.flush();
            System.out.println("while-ban");
        }
    }

    System.out.println("server: shutdown");
}

还有我的客户代码:

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

    // play soundfile from server
    System.out.println("Client: reading from 127.0.0.1:6666");
    try (Socket socket = new Socket("127.0.0.1", 6666)) {
        if (socket.isConnected()) {
            InputStream in = new BufferedInputStream(socket.getInputStream());
            AudioInputStream ais = AudioSystem.getAudioInputStream(in);
            Player playMP3 = new Player(ais);
            playMP3.play();

        }
    }
    System.out.println("Client: end");
}

您怎么看,这是什么问题?有想法吗?

0 个答案:

没有答案