我試圖播放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帶音量調節和暫停和停止音樂的能力,非常感謝,薩姆/
音量值'0F'和'1.0F',你確定你沒有用錯誤的值設置之間? – 2013-07-20 15:43:39
是啊,我0F和1.0F之間設定,當我將它設置,我已經試過印刷播放器的音量值和它說0.0 – user1009569
嗯,你的M $ Windows機器上運行?所以它可能很有趣,但嘗試以管理員模式運行'java.exe',我不知道這個lib如何改變音量(應用程序或全局系統聲音)! – 2013-07-20 16:23:10