播放MP3的java

时间:2013-07-20 14:23:05

标签: java mp3

我正在尝试在桌面上播放MP3文件。我有一些播放MP3的东西,但我无法调节音量。即使它应该根据文档工作。我的代码:

我用这个: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();
    }
}

这:

player.setCurrentVolume(volume);

似乎没有工作,因为无论我设置什么作为参数,它仍然是相同的,音量不会改变。我曾经问过一个问题,但没有得到回应,我仍在寻找答案,问题是;我可以使用什么API来播放音量调节的MP3以及暂停和停止音乐的能力,非常感谢Sam /

1 个答案:

答案 0 :(得分:1)

JLayer是使用Java播放音乐(包括mp3)文件的不错选择。它可以暂停,停止,寻找曲目等。您可以让玩家通过对其中一个JLayer类的小修改来更改线增益/音量 - 请参阅Changing volume in Java when using JLayer。可能有另一种更简洁的方式来改变JLayer的音量,但上述工作。