简单Java:如何在后台创建静音和取消静音按钮?

时间:2015-05-28 00:01:42

标签: java audio mute

我正在开发一个不断更改页面并重新绘制页面的项目,我需要静音并取消静音以在后台播放音乐。我的问题是每当它改变页面和重新绘制时,音乐会重置并停止。无论何时按下静音/取消静音按钮上的鼠标,音乐都会播放或停止,具体取决于它是否静音。

任何人都可以使用任何简单的编码,它会创建一个静音/取消静音按钮,在后台播放音乐,每次页面更改时都不会中断?

我正在applet上的eclipse上使用java进行此操作。

请为我愚蠢一点,因为我对java很新,所以我不太了解。感谢。

以下是页面更改的一些代码:

public void paint(Graphics g){   
    switch (roomPage){
    case 0: homeScreen(g); break;
    case 1: instructionsPage(g); break;
    case 2: startPage(g); break;
    }
}

以下是我想要发生的一些代码:

public void mousePressed(MouseEvent me) {
    if (xPos > 0 && xPos < 0+64 && yPos >0 &&  
            yPos < 0+64){
        if(soundMuted == false){
         //since it's not muted and it is clicked then the music will change
         //to "soundMuted = true" and the music will stop
         soundMuted = true;
        }
        else {
        //since it will be changing to "soundMuted = false" the music will
        //start again
        soundMuted = false;
        }
    }
}

^这个问题是,无论何时我使用它并且它进入不同的页面它由于某种原因将其重置回它的默认状态,这是错误的。我只是想要一个简化静音/取消静音按钮的例子,除非点击按钮,否则它可以在后台不间断播放。

1 个答案:

答案 0 :(得分:0)

所以,基于这个example

我将暂停/恢复功能包装成单独的方法。

您需要将路径粘贴到JTextField中的音频文件并按下加载,然后您可以按“Click me to play”标签开始播放,然后按“Click me to pause”暂停播放

(在任何人因为不使用按钮而跳过我之前,OP正在使用MouseListener,所以我“试图”模仿我可以看到他们的工作流程。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class SimplyPlayer {

    public static void main(String[] args) {
        new SimplyPlayer();
    }

    public SimplyPlayer() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Clip clip;
        private JTextField audioFile;
        private int lastFrame;

        public TestPane() {
            setLayout(new BorderLayout());

            ActionListener loadAction = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (clip != null) {
                        clip.stop();
                        clip.close();
                    }

                    try {
                        loadClip(new File(audioFile.getText()));
                        System.out.println("loaded");
                    } catch (LineUnavailableException | IOException | UnsupportedAudioFileException ex) {
                        ex.printStackTrace();
                    }
                }
            };

            audioFile = new JTextField(20);
            audioFile.addActionListener(loadAction);

            JButton load = new JButton("Load");
            load.addActionListener(loadAction);

            JPanel loadPanel = new JPanel(new BorderLayout());
            loadPanel.add(audioFile);
            loadPanel.add(load, BorderLayout.EAST);

            add(loadPanel, BorderLayout.NORTH);

            JPanel panel = new JPanel(new GridBagLayout());

            JLabel play = new JLabel("Click me to play");
            JLabel pause = new JLabel("Click me to pause");

            play.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(50, 50, 50, 50)));
            pause.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(50, 50, 50, 50)));

            panel.add(play);
            panel.add(pause);

            add(panel);

            MouseListener handler = new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getSource() == play) {
                        resume();
                    } else if (e.getSource() == pause) {
                        pause();
                    }
                }

            };

            play.addMouseListener(handler);
            pause.addMouseListener(handler);

        }

        protected void loadClip(File audioFile) throws LineUnavailableException, IOException, UnsupportedAudioFileException {

            AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);

            AudioFormat format = audioStream.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, format);
            this.clip = (Clip) AudioSystem.getLine(info);
            this.clip.open(audioStream);

        }

        public void pause() {

            if (clip != null && clip.isRunning()) {
                lastFrame = clip.getFramePosition();
                System.out.println("Stop");
                clip.stop();
            }

        }

        public void resume() {

            if (clip != null && !clip.isRunning()) {
                // Make sure we haven't passed the end of the file...
                if (lastFrame < clip.getFrameLength()) {
                    clip.setFramePosition(lastFrame);
                } else {
                    clip.setFramePosition(0);
                }
                System.out.println("Start");
                clip.start();
            }

        }
    }

}

在applet中加载音频文件稍微复杂一些,但已经过演示here