gpt4 book ai didi

java - 在java中同步音频

转载 作者:行者123 更新时间:2023-11-30 11:04:39 28 4
gpt4 key购买 nike

我有一个用于暂停和播放的音频按钮。我为此功能使用了一个按钮,即当用户按下播放时播放声音,然后按钮文本变为暂停。我似乎无法让音频暂停(其余部分有效)

private JButton playBtn;
private Boolean pauseStatus = false;

public SoundClass() throws UnsupportedAudioFileException, IOException {
setLayout(new BorderLayout());

playBtn = new JButton("Play Audio");
playBtn.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
try {

if(pauseStatus==false){
setPauseStatus(false);
play();}

if (getPlayStatus() == true) {
play();
playBtn.setText("Play Audio");
setPauseStatus(false);

} else {

playBtn.setText("Pause Audio");
setPauseStatus(true);
}

} catch (LineUnavailableException ex) {
Logger.getLogger(SoundClass.class.getName()).log(Level.SEVERE, null, ex);
}
}

});

add(playBtn, BorderLayout.NORTH);
}

public void play() throws LineUnavailableException {

try {
File soundFile = new File("/Users/joebloggs/Desktop/Java/Ensoniq-SQ-2-Electric-Piano-C4.wav"); //you could also get the sound file with an URL
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}

}



public void setPauseStatus(boolean b) {
this.pauseStatus = b;

}

public boolean getPlayStatus() {
return this.pauseStatus;

}

最佳答案

您可以获得帧位置,停止播放,当您想要继续播放时,将帧位置设置为您停止播放时的位置。

例如:

暂停

int pos = clip.getFramePosition();
clip.stop();

int pos = clip.getMicrosecondPosition();
clip.stop();

简历

clip.setFramePosition(pos);
clip.start();

clip.setMicrosecondPosition(pos);
clip.start();

关于java - 在java中同步音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29956261/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com