AudioInputStream()不会停止 [英] AudioInputStream() does not stop

查看:193
本文介绍了AudioInputStream()不会停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用AudioInputStream将背景音频插入了我的程序.

I have inserted a background audio into my program using AudioInputStream.

当我按下On时,将播放背景音频,而Off将停止播放背景音频.

When I press the On, the background audio will play, and Off will stop playing the background audio.

这是我的音频播放部分代码的一部分:

Here is the part of my code for the audio playing part :

public void playSound(int i)
{
    try 
    {
        .  //Others buttons sound
        .  //Others buttons sound
    if(i == 3 || i == 4)
    {
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("Background.wav"));
        Clip clip3 = AudioSystem.getClip( );
        clip3.open(audioInputStream);
        if(i == 3)
        {
            clip3.loop(Clip.LOOP_CONTINUOUSLY);
            clip3.start();
            settingSubMenuItem3.setEnabled(false);
            settingSubMenuItem4.setEnabled(true);
        }
        if(i == 4)
        {
            clip3.stop();
            settingSubMenuItem3.setEnabled(true);
            settingSubMenuItem4.setEnabled(false);
        }
    }
    catch(Exception e)
    {
        System.out.println("Error with playing sound.");
        e.printStackTrace();
    }
}

这是ButtonListener部分:

private class MenuItemListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == settingSubMenuItem3)
        {
            playSound(3);
        }
        if(e.getSource() == settingSubMenuItem4)
        {
            playSound(4);       
        }
    }
}

当我按下On按钮时,我的代码可以播放音频,但是当我按下Off按钮时,它不起作用.

My code can play the audio when I press On button, but when I press Off button, it does not work.

在编译和运行过程中没有错误.

There is no error during compile and run.

我的代码有什么错误?

推荐答案

您的AudioInputStream变量audioInputStream和Clip变量clip3在该方法中是本地的.您尝试停止的对象与当前正在播放的对象不同.

Your AudioInputStream variable, audioInputStream, and Clip variable, clip3, are local to the method. The object you are trying to stop is not the same as the one that's playing currently.

使它们成为类字段,在对其调用方法之前检查它们是否不为null;如果要停止当前正在播放的对象,则不要创建新对象,并且应该没事.

Make them class fields, check that they're not null before calling methods on them, don't create a new object if you're trying to stop the currently playing object, and you should be OK.

类似的东西:

public void playSound(int i) {
  try {
     // ...
     if (i == 3 || i == 4) {
        if (i == 3) {
           audioInputStream = AudioSystem
                 .getAudioInputStream(new File("Background.wav"));
           clip3 = AudioSystem.getClip();
           clip3.open(audioInputStream);
           clip3.loop(Clip.LOOP_CONTINUOUSLY);
           clip3.start();
           settingSubMenuItem3.setEnabled(false);
           settingSubMenuItem4.setEnabled(true);
        }
        if (i == 4) {
           if (clip3 != null && clip3.isActive()) {
              clip3.stop();
              settingSubMenuItem3.setEnabled(true);
              settingSubMenuItem4.setEnabled(false);
           }
        }
     }
  } catch (Exception e) {
     System.out.println("Error with playing sound.");
     e.printStackTrace();
  }
}

再次,使audioInputStream和clip3为非静态类字段.

Again, making audioInputStream and clip3 non-static class fields.

顺便说一句,我将避免使用魔术"数字(例如3和4),因为从现在起6个月内调试可能会变成魔鬼.而是给每个JButton自己的动作.应该得到的.

As an aside, I would avoid using "magic" numbers such as 3 and 4 as you are doing as this may become a devil to debug 6 months from now. Instead give each JButton its own Action. It deserves no less.

这篇关于AudioInputStream()不会停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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