如何在Java中播放来自TCP流的声音 [英] How to play sound from tcp stream in java

查看:136
本文介绍了如何在Java中播放来自TCP流的声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

如何播放来自TCP流的声音?考虑到客户端无法从音乐的开头开始听音乐.

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

推荐答案

打开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.

这篇关于如何在Java中播放来自TCP流的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆