【问题标题】:How to play sound from tcp stream in java如何在java中从tcp流中播放声音
【发布时间】:2011-12-04 20:07:58
【问题描述】:

有另一个应用程序在此套接字上写入原始 wav 文件。 客户端启动并开始收听当前正在播放的歌曲。

Socket clientSocket = new Socket("localhost", 9595);
AudioInputStream stream = AudioSystem.getAudioInputStream(clientSocket.getInputStream());

我得到 javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream

关于 AudioSystem.getAudioInputStream 的文档:“从提供的输入流中获取音频输入流。流必须指向有效的音频文件数据。”

如何从 TCP 流中播放声音?考虑到客户无法从一开始就开始收听音乐。

【问题讨论】:

  • 您需要添加更多细节。有很多不同的方法可能无法正常工作。你听到静电了吗?什么都没有?你有没有抛出任何异常?
  • @MusiGenesis 更新!谢谢!

标签: java audio streaming avaudioplayer audio-streaming


【解决方案1】:

打开 AudioInputStream 不会播放来自该流的声音。您需要打开一个剪辑来播放来自流的声音。我根本不是专家,但你应该通过阅读 the Java tutorial 来学习如何做到这一点。

【讨论】:

    【解决方案2】:
    import javax.sound.sampled.*;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.Socket;
    
    public class Client {
    
        public static int PORT = 3000;
    
        SourceDataLine _speaker;
        InputStream _streamIn;
        Socket _server;
        String _serverName = "127.0.0.1";
        boolean _running = true;
    
        public Client(String serverName) throws IOException,LineUnavailableException {
            this._serverName = serverName;
            init();
        }
    
        private void init() throws LineUnavailableException{
            //  specifying the audio format
            AudioFormat _format = new AudioFormat(8000.F,// Sample Rate
                    16,     // Size of SampleBits
                    1,      // Number of Channels
                    true,   // Is Signed?
                    false   // Is Big Endian?
            );
    
            //  creating the DataLine Info for the speaker format
            DataLine.Info speakerInfo = new DataLine.Info(SourceDataLine.class, _format);
    
            //  getting the mixer for the speaker
            _speaker = (SourceDataLine) AudioSystem.getLine(speakerInfo);
            _speaker.open(_format);
        }
    
        public void Start() {
            try {
                System.out.println("Connecting to server @" + _serverName + ":" + PORT);
    
                //  creating the socket and connect to the server
                _server = new Socket(_serverName, PORT);
                System.out.println("Connected to: " + _server.getRemoteSocketAddress());
    
                //  gettting the server stream
                _streamIn = _server.getInputStream();
    
                _speaker.start();
    
                byte[] data = new byte[8000];
                System.out.println("Waiting for data...");
                while (_running) {
    
                    //  checking if the data is available to speak
                    if (_streamIn.available() <= 0)
                        continue;   //  data not available so continue back to start of loop
    
                    //  count of the data bytes read
                    int readCount= _streamIn.read(data, 0, data.length);
    
                    if(readCount > 0){
                        _speaker.write(data, 0, readCount);
                    }
                }
    
                //honestly.... the control never reaches here.
                _speaker.drain();
                _speaker.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    请参阅此项目以获取有效的 TCP 音频流项目: https://github.com/sam016/J-Mic-Stream-Over-Socket/blob/master/Edit 00/

    请参阅此特定文件以了解如何使用 TCP 流播放音频: https://github.com/sam016/J-Mic-Stream-Over-Socket/blob/master/Edit%2000/SockMicClient/src/av/Client.java

    【讨论】:

      猜你喜欢
      • 2010-09-06
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      相关资源
      最近更新 更多