简体   繁体   English

Java- 如何使用库 jlayer 播放音频?

[英]Java- How to use the library jlayer to play audio?

I am trying to use an API by JZoom called JLayer which allows one to play audio files among other things.我正在尝试使用 JZoom 的一个名为 JLayer 的 API,它允许播放音频文件等。 I downloaded the JLayer 1.0.1 version.我下载了 JLayer 1.0.1 版本。 Here is the link to their website: http://www.javazoom.net/javalayer/sources.html .I have found this code which attempts to play an audio file called audio.mp3 :这是他们网站的链接: http : //www.javazoom.net/javalayer/sources.html 。我发现这个代码试图播放一个名为audio.mp3的音频文件:

import javazoom.jl.*;
import java.io.FileInputStream;

public class Sound {

    public static void main(String arg[]) {
        try {
            FileInputStream file = new FileInputStream("C:\\Users\\Sam\\Desktop\\dreamBeatsWorkspace\\dreamBeats\\audio.mp3");
            Player playMP3 = new Player(file);
            playMP3.play();
        } catch (Exception e) {
        }
    }

}

When I write this in Eclipse it underlines the Player part of Player playMP3 = new Player(file);当我在 Eclipse 中编写它时,它强调了Player playMP3 = new Player(file);Player部分Player playMP3 = new Player(file); and asks for a Player class.并要求一个 Player 类。 I assume this means that the Player class is not defined.我认为这意味着没有定义 Player 类。 On the other hand I have tried to download and implement the JLayer library by going to Window->Preferences->Java->Build Path->User Libraries .另一方面,我尝试通过转到Window->Preferences->Java->Build Path->User Libraries来下载和实现 JLayer 库。 Then I created a new folder called audio lib and went to Add External JARS... from their I chose the files jl1.0.1 in C:\\Users\\Sam\\Desktop\\jlayer1.0.1\\JLayer1.0.1 , Player.class in C:\\Users\\Sam\\Desktop\\jlayer1.0.1\\JLayer1.0.1\\classes\\javazoom\\jl\\player and Player.java in C:\\Users\\Sam\\Desktop\\jlayer1.0.1\\JLayer1.0.1\\src\\javazoom\\jl\\player then hit open for all of them and finally I hit OK.然后我创建了一个名为audio lib的新文件夹并转到Add External JARS... jl1.0.1 Add External JARS...从他们我选择了文件jl1.0.1C:\\Users\\Sam\\Desktop\\jlayer1.0.1\\JLayer1.0.1 ,在C:\\Users\\Sam\\Desktop\\jlayer1.0.1\\JLayer1.0.1\\classes\\javazoom\\jl\\player Player.class C:\\Users\\Sam\\Desktop\\jlayer1.0.1\\JLayer1.0.1\\classes\\javazoom\\jl\\player and Player.java in C:\\Users\\Sam\\Desktop\\jlayer1.0.1\\JLayer1.0.1\\src\\javazoom\\jl\\player然后为所有人打开,最后我点击 OK。 I proceed to write my code in a new class I created called Sound.我继续在我创建的名为 Sound 的新类中编写代码。 Finally when I ran this code this error message popped up in the console:最后,当我运行此代码时,控制台中弹出此错误消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Player cannot be resolved to a type
    Player cannot be resolved to a type

    at Sound.main(Sound.java:9)

I am assuming I need to find a way to make a class path to where the Player class is defined.我假设我需要找到一种方法来创建指向 Player 类定义位置的类路径。 I don't know how to do this and Ideas on how to build a class path to the location of the Player class in the Eclipse software would be very helpful.我不知道如何做到这一点,关于如何在 Eclipse 软件中构建到 Player 类位置的类路径的想法会非常有帮助。 All ideas on what went wrong and how one can play an audio file with the JLayer library would be much appreciated.关于出了什么问题以及如何使用 JLayer 库播放音频文件的所有想法将不胜感激。 Thank you for your help!感谢您的帮助!

尝试在您的项目中直接引入库(jl1.0.1.jar),右键单击您的项目 -> 属性 -> Java 构建路径 -> 库 -> 添加外部 Jar 然后确定,清理项目并再次运行

Re write the first line of your code from -重新编写代码的第一行 -

import javazoom.jl.*; 

TO 

import javazoom.jl.player.Player;

This should solve the compilation problem这应该可以解决编译问题

Your project should be a Maven project.您的项目应该是一个 Maven 项目。

1, download this jar file from here https://jar-download.com/artifacts/javazoom/jlayer/1.0.1/source-code 1、从这里下载这个jar文件https://jar-download.com/artifacts/javazoom/jlayer/1.0.1/source-code

2,click your project title, -> new -> directory -> create a folder called lib -> move the jar file you download to lib folder 2,点击你的项目标题,->新建->目录->创建一个名为lib的文件夹->将你下载的jar文件移动到lib文件夹

3, need to configure it as a dependency: click the jar file -> new-> add as a library, then it becomes your part of your project. 3、需要将其配置为依赖:点击jar文件->新建->添加为库,那么它就成为你项目的一部分了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM