1
我正在使用音樂播放器,MusicPlayer類中會發生什麼情況是每當您單擊特定歌曲時,ActionListener抓取文件位置並將其傳遞給我要播放的音頻課程。只有在Java中使用文件類編碼的硬編碼文件位置
在MusicPlayer類
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
JList list = (JList)evt.getSource();
if (evt.getClickCount() == 2) {
String trackLoc = parseToFileURL(currentTrackSelection.getLocation());
audioAccess.startFromBeginning(trackLoc);
}
}
});
的位置是在parseToFileURL方法解析。
public String parseToFileURL(String file) {
//THIS -------file://localhost/D:/iTunes%20Music%20Library/Music/The%20Birthday%20Party/Prayers%20on%20Fire/06%20Figure%20of%20Fun.mp3
//TO THIS ---- D:\\/iTunes Music Library/Music/The Birthday Party/Prayers on Fire/06 Figure of Fun.mp3
URLDecoder dec = new URLDecoder();
file = dec.decode(file);
file = file.substring(file.indexOf(":") + 1);
file = file.substring(file.indexOf(":")-1);
file = file.substring(0,2) + "\\\\/" + file.substring(3);
System.out.println(file);
return file;
}
這樣做的輸出始終是一樣的東西:
d:上Fun.mp3
消防/ 06圖\\/iTunes音樂庫/音樂/生日派對/祈禱C:\\ 6_Gold Blade.mp3
C:\\ /遊戲/ 6_Gold Blade.mp3
並且在將它傳遞到我的音頻類使用startFromBeginning()方法是我開始遇到問題的地方。
package main;
import java.io.*;
import javazoom.jl.decoder.*;
import javazoom.jl.player.*;
public class Audio {
Player player;
FileInputStream fs;
BufferedInputStream bs;
public Audio(){
}
public void startFromBeginning(String trackLocation){
//THE FORMAT FOR ACCESSING FILES
//C:\\6_Gold Blade.mp3
//C:\\/Games/6_Gold Blade.mp3
//C:\\/Games/AnotherphuckingFolder/6_Gold Blade.mp3
File file = new File(trackLocation);
System.out.println(file.exists());
try {
FileInputStream fs = new FileInputStream(file);
BufferedInputStream bs = new BufferedInputStream(fs);
try {
Player player = new Player(bs);
player.play();
} catch (JavaLayerException ex){
}
} catch (Exception ex){ System.out.println("Stream problem"); }
System.out.println("Could not stream from " + trackLocation);
}
}
如果我硬編碼本節文件的位置(請注意,我需要4個反斜槓,因爲它被解析到2)...
File file = new File("D:\\\\/iTunes Music Library/Music/The Birthday Party/Prayers on Fire/06 Figure of Fun.mp3");
System.out.println(file.exists());
然後打印出真實和然後它開始播放歌曲。
但如果我把它抓住從傳入,因爲它是目前trackLocation字符串變量的值,它打印出假和歌曲從來沒有開始,因爲它無法初始化流。
我檢查過比較的值,不知道爲什麼它不工作。