AudioStream - IOException:无法从输入流创建音频流

时间:2016-05-03 00:28:12

标签: java audio actionlistener deprecated

所以我一直在尝试使用AudioStream类播放声音文件。它在第一次尝试时工作正常但在此之后不再起作用。相反,它给我一个IOException错误。

这是我使用的代码。请注意,此代码正在由ActionListener执行的线程中运行,形成JButton。

public void run() {

    try {
        failAudioStream = new AudioStream(failInputStream);
        successAudioStream = new AudioStream(successInputStream);
        waitingAudioStream = new AudioStream(waitingInputStream);
    } catch(IOException e) {
        e.printStackTrace();
    }
 }

此Thread由实现ActionListener的类运行。这是我用于该课程的代码。

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

    }

}

正如我在开头所说的那样,AudioStream首次按下按钮,但在第二次按下时,它会通过以下输出给出错误。

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)

另外,似乎已弃用AudioStream类。那么,如果可以的话,你能告诉我另一种用Java播放音频文件的方法吗?

以下是该类的完整代码,以防您真正需要它。

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

}

0 个答案:

没有答案