2013-07-20 162 views
4

我试图播放MP3文件,我的桌面上。我有些东西可以播放MP3,但我无法调节音量。尽管它应该按照文档工作。我的代码:播放MP3的Java

我用这个: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带音量调节和暂停和停止音乐的能力,非常感谢,萨姆/

+0

音量值'0F'和'1.0F',你确定你没有用错误的值设置之间? – 2013-07-20 15:43:39

+0

是啊,我0F和1.0F之间设定,当我将它设置,我已经试过印刷播放器的音量值和它说0.0 – user1009569

+0

嗯,你的M $ Windows机器上运行?所以它可能很有趣,但尝试以管理员模式运行'java.exe',我不知道这个lib如何改变音量(应用程序或全局系统声音)! – 2013-07-20 16:23:10

回答

1

JLayer是用于播放音乐(包括mp3)文件与Java一个不错的选择。它可以暂停,停止,找道,等等。你可以给玩家一个小的修改,以更改线路增益/音量到JLayer类之一 - 见Changing volume in Java when using JLayer。用JLayer改变音量可能还有另一种更清晰的方法,但上面的工作。

+0

这将改变输出设备的音量完全不是吗?我不是在寻找这个,好像任何不是来自我的应用程序的声音都会被静音......或者我错了吗? – user1009569