How can i play music in java? I tried the method below (does not work), but it uses fotmat wav. How can I download music in .mp3 format (preferably sample code)

package t4; import java.applet.Applet; import java.applet.AudioClip; import java.net.URL; public class Mainn { public static void main(String[] args) { URL url = Mainn.class.getResource("l.wav"); AudioClip clip= Applet.newAudioClip(url); try { Thread.sleep(1000); clip.loop(); Thread.sleep(200); clip.stop(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("l2"); } } 
  • If you were given an exhaustive answer, then close the question by clicking on the green check mark next to the correct answer - user200192

2 answers 2

You can search for something useful here .

But in general it is better to use JLayer

I connected the library to the maven project:

 <dependencies> <dependency> <groupId>javazoom</groupId> <artifactId>jlayer</artifactId> <version>1.0.1</version> </dependency> 

Added this library to jar-nick using maven-assembly-plugin :

 <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> 

assembly.xml

 <assembly xmlns="http://maven.apache.org/plugins/maven-assembly- plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven- assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/ assembly-1.1.0.xsd"> <id>with-dependencies</id> <formats> <format>jar</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <dependencySets> <dependencySet> <unpack>true</unpack> <useTransitiveDependencies>false </useTransitiveDependencies> <excludes> <exclude>org.easytesting:*</exclude> <exclude>junit:*</exclude> <exclude>org.apache.maven.plugins:maven-surefire- report-plugin</exclude> <exclude>net.sourceforge.jexcelapi:*</exclude> </excludes> </dependencySet> </dependencySets> 

This library turned out to be very easy to use, all you had to do was create an object of the javazoom.jl.player.Player class and then call its play () method.

 FileInputStream stream = new FileInputStream(soundFile.getPath()); Player player = new Player(stream); player.play(); 
  • Everything works fine, but still, I think it will be easier to download this library and import it into the project as dDevil said in its answer. (see second answer). Plus, I still put you, because the code works. - nick
  • everything works fine - Dmitriy Mironov
  • one
    @ L'Esperanza Well, what's stopping you from just connecting the dependency in Maven and using the library? - Artem Gafarov

You cannot do this with standard tools, so connect this jarnik to the project.

How to add JAR libraries to a project? Eclipse

And then, to play the mp3-file is enough:

 import java.io.FileInputStream; import javazoom.jl.player.Player; public class Mainn { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("C:/Sleep Away.mp3"); Player playMP3 = new Player(fis); playMP3.play(); } catch(Exception e) {/* NOP */} } } 
  • one
    The link does not work - nick
  • @ L'Esperanza works, just there you need to wait 6 seconds and click on the skip in the upper right corner. The author did not specify an alternative source. - dirkgntly
  • one
    Library is not working. - nick
  • one
    @ L'Esperanza do not know what and how you have. But everything works for me. True to interrupt the main thread. But this problem is solved - dirkgntly
  • One second works and that's it. How to fix? - nick