MP3 play java

I am trying to play an MP3 file on my desktop. I have something that plays MP3, but I canโ€™t adjust the volume. Although it should work in accordance with the documents. My code is:

I use this: https://code.google.com/p/java-audio-player/

import maryb.player.Player; public class MusicPlayer { private Player player; private float volume; private String filePath; /** * Gets the location of the file being played. * @return */ public String getFileLocation() { return filePath; } /** * Gets the volume at which the music file is being played. * @return */ public float getVolume() { return volume; } /** * Sets the current volume of the music file being played. * @param volume */ public void setVolume(float volume) { this.player.setCurrentVolume(volume); } /** * Constructs a new MusicPlayer object, to use the specified music file. * @param filePath - path to the music file. * @param volume - volume the file should be played at. */ public MusicPlayer(String filePath, float volume) { try { player = new Player(); player.setCurrentVolume(volume); player.setSourceLocation(filePath); } catch(Exception e) { e.printStackTrace(); } } /** * Plays the music file. */ public void play() { if (player != null && !player.getSourceLocation().equals(null) || !player.getSourceLocation().equals("")) { player.play(); } } /** * Pauses the music file. */ public void pause() { if (player != null && !player.getSourceLocation().equals(null) || !player.getSourceLocation().equals("")) { player.pause(); } } /** * Stops the music file. */ public void stop() { if (player != null && !player.getSourceLocation().equals(null) || !player.getSourceLocation().equals("")) { player.stop(); } } public static void main( String[] args ) { MusicPlayer player = new MusicPlayer(signlink.findcachedir() + "music.mp3", 0.1f); player.play(); } } 

It:

 player.setCurrentVolume(volume); 

It seems that it does not work, since everything that I set as an argument is still the same, the volume does not change. I asked a question a while ago, but did not receive an answer, and I'm still looking for an answer, a question; which API can I use to play MP3 with volume control and the ability to pause and stop music, thanks a lot, Sam /

+4
source share
1 answer

JLayer is a good choice for playing music (including mp3) using Java. He can stop, stop, search for tracks, etc. You can force the player to change the gain / volume of the line with a slight modification of one of the JLayer classes - see Changing the volume in Java when using JLayer . There may be another cleaner way to change the volume using JLayer, but the above works.

+1
source

Source: https://habr.com/ru/post/1492469/


All Articles