So I've been trying to play sound files using the AudioStream class. It works fine on the first try but doesn't work again after that. Instead, it gives me an IOException error.
Here's the code I used. Note that this code is being ran in a Thread that is executed by an ActionListener form a JButton.
public void run() {
try {
failAudioStream = new AudioStream(failInputStream);
successAudioStream = new AudioStream(successInputStream);
waitingAudioStream = new AudioStream(waitingInputStream);
} catch(IOException e) {
e.printStackTrace();
}
}
This Thread is run by a class implementing ActionListener. Here's the code that I used for that class.
package com.gmail.zacharyvincze.pifiles.listeners;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SendFileListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
(new SendThread()).start();
}
}
As I've said in the beginning, the AudioStream works the first press of the button but on the second press it gives me that error with the following output.
java.io.IOException: could not create audio stream from input stream
at sun.audio.AudioStream.<init>(AudioStream.java:80)
at com.gmail.zacharyvincze.pifiles.listeners.SendThread.run(SendThread.java:36)
java.io.IOException: could not create audio stream from input stream
at sun.audio.AudioStream.<init>(AudioStream.java:80)
at com.gmail.zacharyvincze.pifiles.listeners.SendThread.run(SendThread.java:36)
java.io.IOException: could not create audio stream from input stream
at sun.audio.AudioStream.<init>(AudioStream.java:80)
at com.gmail.zacharyvincze.pifiles.listeners.SendThread.run(SendThread.java:36)
Also, it seems that the AudioStream class in deprecated. So, if you can, could you tell me another way to play audio files in Java?
Here's the full code of the class just in case you actually need it.
package com.gmail.zacharyvincze.pifiles.listeners;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.ImageIcon;
import com.gmail.zacharyvincze.pifiles.PiFiles;
import com.gmail.zacharyvincze.pifiles.windows.Display;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class SendThread extends Thread {
//Streams for sounds
private static InputStream waitingInputStream = SendThread.class.getResourceAsStream("/com/gmail/zacharyvincze/pifiles/sounds/waiting.wav");
private static AudioStream waitingAudioStream;
private static InputStream successInputStream = SendThread.class.getResourceAsStream("/com/gmail/zacharyvincze/pifiles/sounds/success.wav");
private static AudioStream successAudioStream;
private static InputStream failInputStream = SendThread.class.getResourceAsStream("/com/gmail/zacharyvincze/pifiles/sounds/fail.wav");
private static AudioStream failAudioStream;
//Void for starting Thread
public void run() {
//Play waiting music
try {
failAudioStream = new AudioStream(failInputStream);
successAudioStream = new AudioStream(successInputStream);
waitingAudioStream = new AudioStream(waitingInputStream);
} catch(IOException e) {
e.printStackTrace();
}
AudioPlayer.player.start(waitingAudioStream);
//Set loading icon
Display.loading.setIcon(new ImageIcon(Display.class.getResource("/com/gmail/zacharyvincze/pifiles/images/wifi-load.gif")));
Display.loading.setVisible(true);
//Setting variables used for file transfer setup
String SFTPHOST = Display.hostname.getText();
int SFTPPORT = 22;
String SFTPUSER = Display.username.getText();
char[] pass = Display.password.getPassword();
String SFTPPASS = new String(pass);
String SFTPWORKINGDIR = "Documents/";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
//Start the transfer
try {
try {
File f = new File(ChooseFileListener.chooser.getSelectedFile().toString());
channelSftp.put(new FileInputStream(f), f.getName());
channelSftp.get(f.getName(), SFTPWORKINGDIR, new SystemOutProgressMonitor());
} catch(NullPointerException e) {
Display.loading.setIcon(new ImageIcon(Display.class.getResource("/com/gmail/zacharyvincze/pifiles/images/no-file.png")));
AudioPlayer.player.start(failAudioStream);
return;
}
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
channelSftp.cd(SFTPWORKINGDIR);
//Stop the waiting stream
AudioPlayer.player.stop(waitingAudioStream);
//Play the success stream
AudioPlayer.player.start(successAudioStream);
//Set the icon to the success icon
Display.loading.setIcon(new ImageIcon(Display.class.getResource("/com/gmail/zacharyvincze/pifiles/images/success.png")));
} catch(Exception ex) {
//Stop the waiting stream
AudioPlayer.player.stop(waitingAudioStream);
//Play the fail stream
AudioPlayer.player.start(failAudioStream);
Display.loading.setIcon(new ImageIcon(Display.class.getResource("/com/gmail/zacharyvincze/pifiles/images/error.png")));
ex.printStackTrace();
}
}
}