Java 如何播放 MP3 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19603450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:42:20  来源:igfitidea点击:

How can I play an MP3 file?

javajplayer

提问by aburime gregory

I am trying to access an MP3audio file saved in my source folder, so that I can play it in a Java program (application, not applet).

我试图访问保存在我的源文件夹中的MP3音频文件,以便我可以在 Java 程序(应用程序,而不是小程序)中播放它。

The problem I seem to be having is that I cannot import javax.media. I have been trying to install the Java media framework, but it gives me an error report:

我似乎遇到的问题是我无法导入javax.media. 我一直在尝试安装 Java 媒体框架,但它给了我一个错误报告:

"unable to execute...".

“无法执行...”。

I use JDK v7 u45 and had tried it on JDK v7 u40, but I can't seem to figure out what to do. I tried calling

我使用 JDK v7 u45 并在 JDK v7 u40 上尝试过,但我似乎不知道该怎么做。我试着打电话

import javax.media.*;

and it returns a syntax error on the "media", claiming import is not available. Does jmf not work in Java 7?

它在“媒体”上返回一个语法错误,声称导入不可用。jmf 在 Java 7 中不起作用吗?

回答by Dinesh Kumar

The file MP3.java is a bare bones Java program that uses JLayer to play the MP3 file specified on the command line. It plays the MP3 file in a separate thread, so you are free to perform other computation while the song plays. In order to compile and execute it, you need to put the file jl1.0.jar in your current working directory (or classpath). Instructions for compiling and executing (depending on your operating system) are included in the .java file.

文件 MP3.java 是一个基本的 Java 程序,它使用 JLayer 播放命令行上指定的 MP3 文件。它在单独的线程中播放 MP3 文件,因此您可以在播放歌曲时自由执行其他计算。为了编译和执行它,您需要将文件 jl1.0.jar 放在您当前的工作目录(或类路径)中。.java 文件中包含编译和执行的说明(取决于您的操作系统)。

回答by adesh singh

Try the following way. It's working and also import the following classes.

试试下面的方法。它正在工作并且还导入了以下类。

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

try{
    AudioInputStream audioInputStream =
        AudioSystem.getAudioInputStream(
            this.getClass().getResource("<Path of relative sound file in src folder>"));
    Clip clip = AudioSystem.getClip();
    clip.open(audioInputStream);
    clip.start();
}
catch(Exception ex)
{
}

回答by Justin

Your problem is likely that javax.mediais not in your library. Make sure that you include the correct JARfile in your libraries (See Eclipse missing imports).

您的问题很可能javax.media不在您的图书馆中。确保在库中包含正确的JAR文件(请参阅Eclipse 缺少导入)。



I previously did a bunch of searching to attempt to find out how to play audio in Java. What I found is that JavaFXis the easiest to use for playing MP3 files. For playing other inputs, I created a SourceDataLine(under the direction of this tutorial) and used that.

我之前进行了大量搜索,试图找出如何在 Java 中播放音频。我发现JavaFX最容易用于播放 MP3 文件。为了播放其他输入,我创建了一个SourceDataLine(在本教程的指导下)并使用了它。



How to play MP3 music:

如何播放 MP3 音乐:

回答by Torvaldy

For another method that I think is fairly simple and the most up-to-date method in playing MP3 files, use the JavaFXlibrary. A very easy way to use JavaFX is to install the e(fx)clipseplugin. On that website, go to "install" at the top and follow the instruction on how to install the plugin into Eclipse. In Eclipse (after the plugin is installed), go to File-> New-> Other... -> JavaFXProject, and then your JavaFX imports will work.

对于我认为相当简单且最新的播放 MP3 文件的另一种方法,请使用JavaFX库。使用 JavaFX 的一种非常简单的方法是安装e(fx)clipse插件。在该网站上,转到顶部的“安装”并按照有关如何将插件安装到 Eclipse 中的说明进行操作。在 Eclipse 中(安装插件后),转到File-> New-> Other... -> JavaFXProject,然后您的 JavaFX 导入将起作用。

The answer to the question Playing audio using JavaFX MediaPlayer in a normal Java application?(which was already linked to in another answer) was very helpful to me, and may also be helpful to you.

在普通 Java 应用程序中使用 JavaFX MediaPlayer 播放音频问题的答案(已在另一个答案中链接到)对我很有帮助,也可能对您有帮助。