我正在尝试将mp3文件从服务器保存到客户端并在此之后播放。我正在加载播放列表,然后尝试播放一首歌曲(该歌曲应该保存在客户端PC上并在之后播放)。我正在使用JavaFXpackage main构建应用程序;
控制器类:
public class AudioController {
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private Button nextButton;
@FXML
private Button playButton;
@FXML
private ListView<String> playlist;
@FXML
private Button previousButton;
@FXML
private Button stopButton;
@FXML
void initialize() {
final AudioClient audioClient = new AudioClient("127.0.0.1", 3000);
audioClient.setUpConnection();
ArrayList<String> songList = audioClient.getPlaylist();
ObservableList<String> songs = FXCollections
.observableArrayList(songList);
playlist.setItems(songs);
playButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String songToPlay = playlist.getSelectionModel()
.getSelectedItem();
if (songToPlay != null) {
try {
String linkToSong = audioClient.getSong(songToPlay);
Media song = new Media(linkToSong);
MediaPlayer mediaPlayer = new MediaPlayer(song);
mediaPlayer.play();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
}
客户端类
public class AudioClient {
protected BufferedReader socketReader;
protected PrintWriter socketWriter;
protected InputStream is;
protected String hostIP;
protected int hostPort;
public AudioClient(String hostIP, int hostPort) {
this.hostIP = hostIP;
this.hostPort = hostPort;
}
public ArrayList<String> getPlaylist() {
ArrayList<String> fileLines = new ArrayList<String>();
try {
socketWriter.println("Playlist");
socketWriter.flush();
String line = null;
while ((line = socketReader.readLine()) != null) {
fileLines.add(line);
}
} catch (IOException e) {
System.out.println("There was a problem reading");
}
return fileLines;
}
public String getSong(String songName) throws IOException {
socketWriter.println("Request " + songName);
socketWriter.flush();
String filePath = new String("D:\\" + songName + ".mp3");
FileOutputStream fos = new FileOutputStream(new File(filePath));
BufferedOutputStream bos = new BufferedOutputStream(fos);
int count;
byte[] buffer = new byte[4096];
while ((count = is.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, count);
}
return filePath;
}
public void setUpConnection() {
try {
Socket client = new Socket(hostIP, hostPort);
socketReader = new BufferedReader(new InputStreamReader(
client.getInputStream()));
InputStream is = client.getInputStream();
socketWriter = new PrintWriter(client.getOutputStream());
} catch (UnknownHostException e) {
System.out.println("Error setting up socket connection: unknown host at "
+ hostIP + ":" + hostPort);
} catch (IOException e) {
System.out.println("Error setting up socket connection: " + e);
}
}
public void tearDownConnection() {
try {
socketWriter.close();
socketReader.close();
} catch (IOException e) {
System.out.println("Error tearing down socket connection: " + e);
}
}
处理与服务器的连接的类
public class ConnectionHandler implements Runnable {
private Socket socketToHandle;
public ConnectionHandler(Socket aSocketToHandle) {
socketToHandle = aSocketToHandle;
}
@Override
public void run() {
try {
PrintWriter streamWriter = new PrintWriter(
socketToHandle.getOutputStream());
BufferedReader streamReader = new BufferedReader(
new InputStreamReader(socketToHandle.getInputStream()));
String songToPlay = null;
while ((songToPlay = streamReader.readLine()) != null) {
if (songToPlay.equals("Playlist")) {
File songsTxt = new File(
"D:/Universitet i dr/JAVA/AudioPlayer/src/main/media/songsList.txt");
String song = null;
try {
FileReader reader = new FileReader(songsTxt);
BufferedReader songReader = new BufferedReader(reader);
while ((song = songReader.readLine()) != null) {
streamWriter.println(song);
}
} catch (FileNotFoundException e) {
System.out.println("File dosen't exist");
} catch (IOException e) {
System.out.println("Can't read from the file");
}
} else if (songToPlay.startsWith("Request ")) {
songToPlay = songToPlay.replaceFirst("Request ", "");
File songPath = new File(
"D:/Universitet i dr/JAVA/AudioPlayer/src/main/media/"
+ songToPlay);
BufferedInputStream inStream = new BufferedInputStream(
new FileInputStream(songPath));
BufferedOutputStream outStream = new BufferedOutputStream(
socketToHandle.getOutputStream());
byte[] buffer = new byte[4096];
for (int read = inStream.read(buffer); read >= 0; read = inStream.read(buffer)) {
outStream.write(buffer, 0, read);
}
}
}
} catch (Exception e) {
System.out.println("Error handling a client: " + e);
}
}