用Java启动和停止音乐文件?

时间:2015-05-09 22:12:07

标签: java audio javasound

这是我的健全课。

Mean:   10.9033333333
std :   1.27881021092

这是我的ActionListener,我想在用户点击按钮时停止音乐。音乐在启动时播放。

public class Sounds extends Thread { 

private String filename;
private Position curPosition;
private final int EXTERNAL_BUFFER_SIZE = 524288; 

enum Position { 
    LEFT, RIGHT, NORMAL
};

public Sounds(String wavFile) { 
    filename = wavFile;
    curPosition = Position.NORMAL;
} 

public Sounds(String wavfile, Position p) { 
    filename = wavfile;
    curPosition = p;
} 

public void run() { 

    File soundFile = new File(filename);
    if (!soundFile.exists()) { 
        System.err.println(".wav file not found: " + filename);
        return;
    } 

    AudioInputStream audioInputStream = null;
    try { 
        audioInputStream = AudioSystem.getAudioInputStream(soundFile);
    } catch (UnsupportedAudioFileException e1) { 
        e1.printStackTrace();
        return;
    } catch (IOException e1) { 
        e1.printStackTrace();
        return;
    } 

    AudioFormat format = audioInputStream.getFormat();
    SourceDataLine auline = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

    try { 
        auline = (SourceDataLine) AudioSystem.getLine(info);
        auline.open(format);
    } catch (LineUnavailableException e) { 
        e.printStackTrace();
        return;
    } catch (Exception e) { 
        e.printStackTrace();
        return;
    } 

    if (auline.isControlSupported(FloatControl.Type.PAN)) { 
        FloatControl pan = (FloatControl) auline
                .getControl(FloatControl.Type.PAN);
        if (curPosition == Position.RIGHT) 
            pan.setValue(1.0f);
        else if (curPosition == Position.LEFT) 
            pan.setValue(-1.0f);
    } 

    auline.start();
    int nBytesRead = 0;
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

    try { 
        while (nBytesRead != -1) { 
            nBytesRead = audioInputStream.read(abData, 0, abData.length);
            if (nBytesRead >= 0) 
                auline.write(abData, 0, nBytesRead);
        } 
    } catch (IOException e) { 
        e.printStackTrace();
        return;
    } finally { 
        auline.drain();
        auline.close();
    } 
} 
}

我可以让音乐播放得很好,但我无法弄清楚如何阻止它。我尝试使用.stop()和.end(),但我被告知他们已被弃用。任何人都知道我该怎么做?感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用剪辑类。这是我读取.wav文件并播放它的代码:

try {
    File file = new File(src); // src is the location of the file
    AudioInputStream stream = AudioSystem.getAudioInputStream(file);
    Clip clip = AudioSystem.getClip();
    clip.open(stream);
    clip.start();
} catch (Exception e) {
    e.printStackTrace();
}

也就是说,clip.stop()将停止剪辑。要重置剪辑,请使用clip.setFramePosition(0)。请注意,clip不需要 关闭,因为如果在clip.close()之后调用clip.start(),则剪辑将无法播放。

希望这就是你要找的东西!

编辑:范围

在Java中使用作用域,如果在方法中声明局部变量,则无法在方法之外访问它。如果要访问main方法之外的剪辑,请将其解压缩到字段:

class Main {
    static Clip clip;
    public static void main (String... args) {
        try {
            File file...
            clip = AudioSystem.getClip();
            ...
        } catch...
    }
}