在Java中,你如何停止previous音频文件时,另一个音频文件开始使用key_events [英] In Java, How do you stop a previous audio file when another audio file starts using key_events

查看:232
本文介绍了在Java中,你如何停止previous音频文件时,另一个音频文件开始使用key_events的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道了clip.stop()方法,但它似乎没有当我有它key_events内内工作。它只是导致错误。嗯,我知道为什么它会导致错误。因为我要求它基本上停止不存在,直到几行后的剪辑。但是使用这个相同的逻辑或接近它如果可能的话,我怎么可能再code它们,让它们知道要选择是从previous key_event打previous剪辑。我打算的功能是:当F1是pressed,一个wav戏剧。当F2为pssed $ P $,目前WAV停止,新的WAV启动。当F3是pssed $ P $,目前WAV停止,新的WAV启动。等等等等等。

I know about the clip.stop() method, however it doesn't seem to work within when I have it inside the key_events. It just causes an Error. Well I know why it causes the error. Because I'm asking it to essentially stop a clip that doesn't exist until a few lines later. But using this same logic or close to it if possible, how could I recode that so it knows to select the previous clip that was playing from a previous key_event. The functionality I'm intending for is: When F1 is pressed, a wav plays. When F2 is pressed, current wav STOPS, new wav STARTS. When F3 is pressed, current wav STOPS, new wav STARTS. Etc etc etc.

            case KeyEvent.VK_F1:

                try {

                    //stop any sound
                    clip.stop();

                    sample = AudioSystem.getAudioInputStream(getURL(filename));

                    //create a sound buffer
                    Clip clip = AudioSystem.getClip();

                    //load the audio file
                    clip.open(sample);

                    //play sample
                    clip.start();

                } catch (MalformedURLException ez) {
                } catch (IOException ez) {
                } catch (LineUnavailableException ez) {
                } catch (UnsupportedAudioFileException ez) {
                } catch (Exception ez) { }

                break;


            case KeyEvent.VK_F2:

                try {

                    //stop any sound
                    clip.stop();

                    sample = AudioSystem.getAudioInputStream(getURL(filename2));

                    //create a sound buffer
                    Clip clip = AudioSystem.getClip();

                    //load the audio file
                    clip.open(sample);

                    //play sample
                    clip.start();

                } catch (MalformedURLException ez) {
                } catch (IOException ez) {
                } catch (LineUnavailableException ez) {
                } catch (UnsupportedAudioFileException ez) {
                } catch (Exception ez) { }

                break;

            case KeyEvent.VK_F3:

                try {

                    //stop any sound
                    clip.stop();

                    sample = AudioSystem.getAudioInputStream(getURL(filename3));

                    //create a sound buffer
                    Clip clip = AudioSystem.getClip();

                    //load the audio file
                    clip.open(sample);

                    //play sample
                    clip.start();

                } catch (MalformedURLException ez) {
                } catch (IOException ez) {
                } catch (LineUnavailableException ez) {
                } catch (UnsupportedAudioFileException ez) {
                } catch (Exception ez) { }

                break;

            case KeyEvent.VK_F4:

                try {

                    //stop any sound
                    clip.stop();

                    sample = AudioSystem.getAudioInputStream(getURL(filename4));

                    //create a sound buffer
                    Clip clip = AudioSystem.getClip();

                    //load the audio file
                    clip.open(sample);

                    //play sample
                    clip.start();

                } catch (MalformedURLException ez) {
                } catch (IOException ez) {
                } catch (LineUnavailableException ez) {
                } catch (UnsupportedAudioFileException ez) {
                } catch (Exception ez) { }

                break;

            case KeyEvent.VK_F5:

                try {

                    //stop any sound
                    clip.stop();

                    sample = AudioSystem.getAudioInputStream(getURL(filename5));

                    //create a sound buffer
                    Clip clip = AudioSystem.getClip();

                    //load the audio file
                    clip.open(sample);

                    //play sample
                    clip.start();

                } catch (MalformedURLException ez) {
                } catch (IOException ez) {
                } catch (LineUnavailableException ez) {
                } catch (UnsupportedAudioFileException ez) {
                } catch (Exception ez) { }

                break;

            case KeyEvent.VK_F6:

                try {

                    //stop any sound
                    clip.stop();

                    sample = AudioSystem.getAudioInputStream(getURL(filename6));

                    //create a sound buffer
                    Clip clip = AudioSystem.getClip();

                    //load the audio file
                    clip.open(sample);

                    //play sample
                    clip.start();

                } catch (MalformedURLException ez) {
                } catch (IOException ez) {
                } catch (LineUnavailableException ez) {
                } catch (UnsupportedAudioFileException ez) {
                } catch (Exception ez) { }

                break;

            case KeyEvent.VK_F7:

                try {

                    //stop any sound
                    clip.stop();

                    sample = AudioSystem.getAudioInputStream(getURL(filename7));

                    //create a sound buffer
                    Clip clip = AudioSystem.getClip();

                    //load the audio file
                    clip.open(sample);

                    //play sample
                    clip.start();

                } catch (MalformedURLException ez) {
                } catch (IOException ez) {
                } catch (LineUnavailableException ez) {
                } catch (UnsupportedAudioFileException ez) {
                } catch (Exception ez) { }

                break;

任何帮助将大大AP preciated :)谢谢

Any help would be greatly appreciated :) Thanks

推荐答案

这是很难100%肯定,但它看起来像你的阴影...变量

It's difficult to be 100% sure, but it looks like you are shadowing your variables...

clip.stop();

sample = AudioSystem.getAudioInputStream(getURL(filename));

//create a sound buffer
Clip clip = AudioSystem.getClip();

在事实上,我甚至不能确定这将如何编译...

In fact, I'm not even sure how this would compile...

定义剪辑作为一个实例变量,当程序被初始化,初始化剪辑在同一时间。

Define Clip as an instance variable, when you program is initialised, initialise the Clip at the same time.

您应该能够调用停止在任何时间,但这只会重置剪辑回启动电流输入。你需要什么,也能做的,就是关闭剪辑,释放由剪辑对内部资源进行管理当前输入...

You should be able to call stop at any time, but this will only reset the Clip back to the start of the current input. What you need to, also do, is close the Clip, which releases the internal resources been managed by the Clip for the current input...

的KeyListener 是出了名的也麻烦,你应该考虑使用的 ,因为他们为您提供控制,以确定可以在生成的关键事件的重点级别,例如键绑定...

KeyListeners are also notoriously troublesome and you should consider using Key Bindings as they provide you with the control to determine the focus level that the key events can be generated at, for example...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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 javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestPlayer {

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

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

                try {
                    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);
                } catch (LineUnavailableException exp) {
                    exp.printStackTrace();
                }
            }
        });
    }

    public class TestPane extends JPanel {

        private Clip clip;
        private List<File> playList;
        private int index;

        private JLabel label;

        public TestPane() throws LineUnavailableException {
            label = new JLabel("Play stuff");
            add(label);
            clip = AudioSystem.getClip();

            File[] files = new File("A folder of music files").listFiles(new FileFilter() {
                @Override
                public boolean accept(File file) {
                    return file.getName().toLowerCase().endsWith(".wav");
                }
            });
            playList = new ArrayList<>(Arrays.asList(files));
            index = -1;

            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "previous");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "next");

            ActionMap am = getActionMap();
            am.put("next", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    playNext();
                }
            });
            am.put("previous", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    playPrevious();
                }
            });
        }

        public void playNext() {
            index++;
            if (index >= playList.size()) {
                index = 0;
            }

            File file = playList.get(index);

            play(file);
        }

        public void playPrevious() {
            index--;
            if (index < 0) {
                index = playList.size() - 1;
            }

            File file = playList.get(index);

            play(file);
        }

        public void play(File file) {

            try {
                stop();
                label.setText(file.getName());
                AudioInputStream sample = AudioSystem.getAudioInputStream(file);
                clip.open(sample);
                clip.start();
            } catch (UnsupportedAudioFileException | IOException | LineUnavailableException exp) {
                exp.printStackTrace();
            }
        }

        public void stop() {

            clip.stop();
            clip.close();

        }

    }

}

这篇关于在Java中,你如何停止previous音频文件时,另一个音频文件开始使用key_events的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆