1

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();
        }
    }

}
Zachary Vincze
  • 427
  • 7
  • 24
  • you are leaching those files before they finished - show all the code - AudioStream is not deprecated it works – gpasch May 03 '16 at 02:05
  • The only other code I could think of that may be causing the problem would be `AudioPlayer.player.start(failAudioStream)`. Also, are you sure that it's not deprecated? It gives me this warning every time I run it. `2016-05-02 22:08:52.656 java[22281:3708317] 22:08:52.656 WARNING: 140: This application, or a library it uses, is using the deprecated Carbon Component Manager for hosting Audio Units. Support for this will be removed in a future release. Also, this makes the host incompatible with version 3 audio units. Please transition to the API's in AudioComponent.h.` – Zachary Vincze May 03 '16 at 02:11
  • @gpasch Tell me if you actually need the full code. Are you trying to say that the AudioPlayer is starting again before it had a chance to finish? – Zachary Vincze May 03 '16 at 02:12
  • @gpasch I've added the full code in the question if you need to look at it. – Zachary Vincze May 03 '16 at 02:17
  • Carbon Component Manager: I dont know what that is it may be deprecated the main javax.sound is not - oh ok you are using wierd packages - I mean this javax.sound.sampled.AudioInputStream - I cant look at it you better use the main javax.sound package - my guess is that they finish but never closed so they cant restart you need to close the streams - in any case switch to javax.sound - or wait for some one else – gpasch May 03 '16 at 02:41
  • @gpasch Ok, I'll take your answer into consideration. It may be that they were never closed, that could be the problem. – Zachary Vincze May 03 '16 at 02:43
  • The `sun.audio` classes are not part of the official Java API and should not be used. Use the `javax.sound` APIs, the [javasound](http://stackoverflow.com/tags/javasound/info) tag has links to info on these. – greg-449 May 03 '16 at 06:55
  • @ZacharyVincze Any updates on this? I'm doing a project, and one component of it is very similar to this. I can't seem to get it to work. – Aaron Esau Jan 04 '17 at 05:11

0 Answers0