使用 Java 播放音乐 [英] stream music with java

查看:24
本文介绍了使用 Java 播放音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一些聊天程序,现在想通过客户端命令播放音乐,例如:

I am currently coding some chat program and now want to play music via a client command like:

/music http://somewebsite.com/somesong.mp3

因此链接将从发送歌曲的客户端发送到服务器.然后服务器应该下载歌曲,将其流式传输到每个客户端(他们也不应该下载它).问题是,我不知道如何通过基于 TCP 的连接流式传输 mp3.

So the link would be sent from the client that sent the song, to the server. The server should then without downloading the song, stream it to every client (they shouldn't download it as well). Problem is, I don't know how to stream mp3's over a TCP based connection.

我的问题是,是否有人可以为我提供我需要的库/教程/想法/代码示例.

My question is, if someone could provide me with libraries/tutorials/ideas/code examples of what I need.

作为替代方案,直接从一个客户的计算机流式传输音乐也可以.如果我(作为客户端)在我的机器上播放一首歌(使用一些音乐播放器,如 VLC 或类似的),然后输入某个命令(可能是 /stream),我的声卡中的声音将被流式传输到服务器,然后到每个登录的客户端.

As an alternative directly streaming music from one client's computer would be okay as well. If I (as a client) play a song on my machine (using some music player like VLC or similar) and then type a certain command (maybe /stream), the sounds from my soundcard would be streamed to the server and then to every client logged in.

如果你能为我提供一些关于这两个方面的建议就好了,谷歌到目前为止并没有真正帮助,主要是因为我不太明白我应该搜索什么.

It would be awesome if you could provide me with some advice for both, Google didn't really help so far, mainly because I don't quite understand what I should search for.

提前致谢!

推荐答案

我找到了一个完全符合我想要做的功能的库:http://www.javazoom.net/javalayer/javalayer.html

I found a library that does exactly what I want to do: http://www.javazoom.net/javalayer/javalayer.html

public void play() {
        String song = "http://www.ntonyx.com/mp3files/Morning_Flower.mp3";
        Player mp3player = null;
        BufferedInputStream in = null;
        try {
          in = new BufferedInputStream(new URL(song).openStream());
          mp3player = new Player(in);
          mp3player.play();
        } catch (MalformedURLException ex) {
        } catch (IOException e) {
        } catch (JavaLayerException e) {
        } catch (NullPointerException ex) {
        }

}

希望对有类似问题的大家有所帮助:-)

Hope that helps everyone with a similar question :-)

这篇关于使用 Java 播放音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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