繁体   English   中英

Java:从服务器到客户端的 .mp3 音频文件流提供 UnsupportedAudioFileException

[英]Java: .mp3 audio file streaming from server to client gives UnsupportedAudioFileException

所以我试图从服务器到客户端实时流式传输.mp3文件(如 spotify)。 当我运行客户端时,我Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream得到Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream ,我看过很多类似的帖子,但我不知道出了什么问题,我坚持天。 我试图从名为AllDay.mp3服务器接收的示例文件是 3.8 mb,如果这有帮助的话。

客户

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

public class AudioClient {
    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());
                play(in);
            }
        }
    }
    
    private static synchronized void play(final InputStream in) throws Exception {
        AudioInputStream ais = AudioSystem.getAudioInputStream(in);
        try (Clip clip = AudioSystem.getClip()) {
            clip.open(ais);
            clip.start();
            Thread.sleep(100); // given clip.drain a chance to start
            clip.drain();
        }
    }
}

服务器

import java.io.*;
import java.net.*;

public class AudioServer {
    public static void main(String[] args) throws IOException {
        
        File soundFile = new File("C:\\ServerMusicStorage\\AllDay.mp3");
        System.out.println("Streaming to client : " + soundFile);
        
        try (ServerSocket serverSocker = new ServerSocket(6666); 
            FileInputStream in = new FileInputStream(soundFile)) {
            if (serverSocker.isBound()) {
                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);
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM