如何在java中从tcp流播放声音

发布于 2024-12-09 06:16:21 字数 472 浏览 0 评论 0原文

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

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

我得到javax.sound.sampled.UnsupportedAudioFileException:无法从输入流获取音频输入流

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

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

There is another app that writes raw wav file on this socket.
The client starts and begins listening to the song which is currently playing.

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

I get javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream

Doc about AudioSystem.getAudioInputStream: "Obtains an audio input stream from the provided input stream. The stream must point to valid audio file data."

How to play sound from a TCP stream? Considering the client could not start listening from beginnig of the music.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽袖吟 2024-12-16 06:16:21

打开 AudioInputStream 不会播放来自该流的声音。您需要打开剪辑才能播放来自流的声音。我根本不是专家,但您应该通过阅读 来了解如何做到这一点Java 教程

Opening an AudioInputStream doesn't play the sound coming from this stream. You need to open a Clip to play the sound coming from the stream. I'm not an expert at all, but you should learn how to do it by reading the Java tutorial.

扎心 2024-12-16 06:16:21
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

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();
        }
    }
}

Refer to this project for a working TCP audio streaming project:
https://github.com/sam016/J-Mic-Stream-Over-Socket/blob/master/Edit 00/

Refer to this specific file to get an idea on how to use TCP stream to play audio:
https://github.com/sam016/J-Mic-Stream-Over-Socket/blob/master/Edit%2000/SockMicClient/src/av/Client.java

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文