簡體   English   中英

嘗試再次按下Jbutton后停止播放wav文件,但不會停止

[英]Attempting to stop wav file from playing after pressing the Jbutton a second time, but it doesn't stop

當用戶按下按鈕時,該程序應該進行健身圖起搏器測試。 第二次按下一個按鈕,音頻停止。 再次按下按鈕后,程序應從頭開始播放測試。 這是我的代碼:

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.net.URL;
import javax.sound.sampled.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class aMeme extends JFrame implements ActionListener{
    public JButton button;
    public boolean check;
    public boolean audio;

    public void paint(Graphics g){
        if (check == true){
            BufferedImage img = null;
            try{
                img = ImageIO.read(newFile("C:/Users/kebrobst18/Downloads/Fitnessgram.png"));
            } catch (IOException e){
            }
            g.drawImage(img, 0, 0, this);
        }
    }

    public void start(){
        setLayout(new BorderLayout());
        button=new JButton();
        button.setPreferredSize(new Dimension(200, 100));
        button.setText("Start/Stop"); 
        button.addActionListener(this);
        add(button, BorderLayout.SOUTH);
        setSize(500,500);
        setVisible(true);
        audio = false;
    }   

    public void actionPerformed(ActionEvent e){    
        check = true;
        repaint();
        try{
            URL fg = new URL("http://sendeyo.com/up/8658f011a4c712b5da42f74af77729fe.wav");
            Clip fitness = AudioSystem.getClip();
            AudioInputStream gram = AudioSystem.getAudioInputStream(fg);
            fitness.open(gram);
            if (audio == false){
                fitness.start();
                audio = true;
            } else if (audio == true){
                fitness.stop();
                audio = false;
            }
        } catch (LineUnavailableException f){
        }
        catch (UnsupportedAudioFileException | IOException f) {
        }
    }

    public static void main(String args[]){
        aMeme x = new aMeme();
        x.start();
    }
}

您的音頻對象是ActionPerformed方法的本地對象。 第二次按下該按鈕時, 會創建一個全新的音頻對象 ,並且在它們上調用stop根本不會產生任何有益的效果,因為您不會停止當前正在播放的音頻對象。 將重要的音頻對象放入類的實例字段中。 特別是,需要將適應性變量設為實例字段。


其他無關但重要的問題,根據我的評論:

  • 再次,將來,請改進已發布代碼的格式以提高可讀性
  • 不要將catch塊留空,至少要打印stacktrace。 否則,你會失明。
  • 不要在JFrame的paint方法中進行繪制,也不要在您的paint方法中忽略超級調用。 取而代之的是在JPanel的paintComponent方法中進行繪制,並在其第一行中調用父級的paintComponent方法,
  • 切勿從繪畫方法中讀取文件,因為這是不必要的,並且會切入程序的感知響應能力。
  • 使用完資源后,請關閉它們。

重放相同音頻文件的簡單示例:

import java.awt.event.*;
import javax.swing.*;
import javax.sound.sampled.*;
import java.net.URL;
import java.io.*;

class SoundTest {

  public static void main(String[] args) throws Exception {
    URL urlToSound = new URL("file:c:/java/gun1.wav");
//    URL urlToSound = new URL("file:c:/java/flyby1.wav");
    AudioInputStream ais = AudioSystem.getAudioInputStream(urlToSound);
    final Clip clip = AudioSystem.getClip();
    clip.open(ais);
    JButton button = new JButton("Bird Sounds");
    button.addActionListener( new ActionListener(){
        public void actionPerformed(ActionEvent ae) {
          clip.setFramePosition(0);
          clip.start();
        }
      } );
    JOptionPane.showMessageDialog(null, button);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM