错误播放的AudioInputStream [英] Error playing AudioInputStream

查看:197
本文介绍了错误播放的AudioInputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建2 的JMenuItem ,可以启动和停止背景音乐。

I want to create 2 JMenuItem that can start and stop the background audio.

下面是我的code:

public class MainClass extends JFrame
{
    private AudioInputStream audioInputStream;
    private Clip clip;

    public MainClass(String title)
    {
        try
        {
            audioInputStream = AudioSystem.getAudioInputStream(new File("Background.wav"));
            clip = AudioSystem.getClip();
            clip.loop(Clip.LOOP_CONTINUOUSLY);
            clip.open(audioInputStream);
        }
        catch(Exception e)
        {
            System.out.println("Error with playing sound.");
            e.printStackTrace();
        }
    }
    public void startSound()
    {   
        clip3.start();
        settingSubMenuItem1.setEnabled(false);
        settingSubMenuItem2.setEnabled(true);
    }

    public void stopSound()
    {
        clip3.stop();
        settingSubMenuItem1.setEnabled(true);
        settingSubMenuItem2.setEnabled(false);
    }

    private class MenuItemListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == settingSubMenuItem1)
            {
                startSound();
            }
            if(e.getSource() == settingSubMenuItem2)
            {
                stopSound();        
            }
        }
    }
}

当我点击 settingSubMenuItem1 ,它的做工精细,音频播放。

When I click the settingSubMenuItem1, it's work fine, audio is played.

但是,当我点击 settingSubMenuItem2 ,有错误,如果再次点击 settingSubMenuItem1 ,将没有更多的声音

But when I click the settingSubMenuItem2, there is errors and if click again settingSubMenuItem1, there will no more sound.

下面是错误的:

Error with playing sound.
java.lang.IllegalStateException: Clip is already open with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian and frame lengh of 7658

什么是我的程序的错误?

What is the error of my program?

推荐答案

这SSCCE是一个'空结果在这里,在音频重新启动,没有例外(试过至少3次)。

This SSCCE is a 'null result' here, in that the audio restarts (tried at least 3 times) with no exceptions.

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

public class RestartableLoopSound {

    public static void main(String[] args) throws Exception {
        URL url = new URL(
            "http://pscode.org/media/leftright.wav");
        final Clip clip = AudioSystem.getClip();
        AudioInputStream ais = AudioSystem.
            getAudioInputStream( url );
        clip.open(ais);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                final JToggleButton b = new JToggleButton("Loop");
                ActionListener listener = new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        if (b.isSelected()) {
                            // loop continuously
                            clip.loop(Clip.LOOP_CONTINUOUSLY);
                        } else {
                            clip.stop();
                        }
                    }
                };
                b.addActionListener(listener);
                JOptionPane.showMessageDialog(null, b);
            }
        });
    }
}

这篇关于错误播放的AudioInputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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