简体   繁体   中英

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. I downloaded the JLayer 1.0.1 version. 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 :

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); and asks for a Player class. I assume this means that the Player class is not defined. On the other hand I have tried to download and implement the JLayer library by going to Window->Preferences->Java->Build Path->User Libraries . 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. I proceed to write my code in a new class I created called 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. 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. All ideas on what went wrong and how one can play an audio file with the JLayer library would be much appreciated. 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.

1, download this jar file from here 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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