JPanels和播放音乐

时间:2013-12-03 16:25:45

标签: java multithreading swing audio jpanel

我有一个在JFrame中有多个JPanel的程序。在某些时候,只有一个面板可见[setVisible(true)],而所有其他面板不是[setVisible(false)]。我想要的是播放一段音乐,而JPanel是可见的,但是当它不是时,就会暂停。我有一个播放音频的课程

public class AudioPlay extends Thread { 

private String filename;
private Position curPosition;

private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb 

enum Position { 
    LEFT, RIGHT, NORMAL
};

public AudioPlay(String wavfile) { 
    filename = wavfile;
    curPosition = Position.NORMAL;
} 

public AudioPlay(String wavfile, Position p) { 
    filename = wavfile;
    curPosition = p;
} 

public void run() { 
    while(true){
        File soundFile = new File(filename);
        if (!soundFile.exists()) { 
            System.err.println("Wave file not found: " + filename);
            return;
        } 

        AudioInputStream audioInputStream = null;
        try { 
            audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (UnsupportedAudioFileException e1) { 
            e1.printStackTrace();
            return;
        } catch (IOException e1) { 
            e1.printStackTrace();
            return;
        } 

        AudioFormat format = audioInputStream.getFormat();
        SourceDataLine auline = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        try { 
            auline = (SourceDataLine) AudioSystem.getLine(info);
            auline.open(format);
        } catch (LineUnavailableException e) { 
            e.printStackTrace();
            return;
        } catch (Exception e) { 
            e.printStackTrace();
            return;
        } 

        if (auline.isControlSupported(FloatControl.Type.PAN)) { 
            FloatControl pan = (FloatControl) auline
                    .getControl(FloatControl.Type.PAN);
            if (curPosition == Position.RIGHT) 
                pan.setValue(1.0f);
            else if (curPosition == Position.LEFT) 
                pan.setValue(-1.0f);
        } 

        auline.start();
        int nBytesRead = 0;
        byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

        try { 
            while (nBytesRead != -1) { 
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
                if (nBytesRead >= 0) 
                    auline.write(abData, 0, nBytesRead);
            } 
        } catch (IOException e) { 
            e.printStackTrace();
            return;
        } finally { 
            auline.drain();
            auline.close();
        }
    }
}

}

因此,在每个类中扩展一个JPanel并将AudioPlay作为参数。首次创建JPanel时,调用start()方法。但后来我不断播放所有的音频文件。

任何人都可以向我展示一个JPanel,它会暂停其中一个AudioPlay类的start()方法而JPanel不可见吗?

修改

好吧,所以我有了一个想法并且玩了一下,现在我可以让它只在它可见的时候播放音频!在关闭其可视性之后,它会播放前一个面板音乐一两秒,但大多数情况下应该做到这一点!对于任何需要它的人来说,这是更新的。

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.DataLine; 
import javax.sound.sampled.FloatControl; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.SourceDataLine; 
import javax.sound.sampled.UnsupportedAudioFileException; 
import javax.swing.JPanel;

public class AudioPlay extends Thread { 

private String filename;
private Position curPosition;
JPanel panel;

private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb 

enum Position { 
    LEFT, RIGHT, NORMAL
};

public AudioPlay(String wavfile, JPanel panel) { 
    filename = wavfile;
    curPosition = Position.NORMAL;
    this.panel = panel;
} 

public AudioPlay(String wavfile, Position p, JPanel panel) { 
    filename = wavfile;
    curPosition = p;
    this.panel = panel;
} 

public void run() { 
    while(true){
        File soundFile = new File(filename);
        if (!soundFile.exists()) { 
            System.err.println("Wave file not found: " + filename);
            return;
        } 

        AudioInputStream audioInputStream = null;
        try { 
            audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (UnsupportedAudioFileException e1) { 
            e1.printStackTrace();
            return;
        } catch (IOException e1) { 
            e1.printStackTrace();
            return;
        } 

        AudioFormat format = audioInputStream.getFormat();
        SourceDataLine auline = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        try { 
            auline = (SourceDataLine) AudioSystem.getLine(info);
            auline.open(format);
        } catch (LineUnavailableException e) { 
            e.printStackTrace();
            return;
        } catch (Exception e) { 
            e.printStackTrace();
            return;
        } 

        if (auline.isControlSupported(FloatControl.Type.PAN)) { 
            FloatControl pan = (FloatControl) auline
                    .getControl(FloatControl.Type.PAN);
            if (curPosition == Position.RIGHT) 
                pan.setValue(1.0f);
            else if (curPosition == Position.LEFT) 
                pan.setValue(-1.0f);
        } 

        auline.start();
        int nBytesRead = 0;
        byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
        try { 
            while (nBytesRead != -1) { 
                if(this.panel.isShowing()){
                    nBytesRead = audioInputStream.read(abData, 0, abData.length);
                    if (nBytesRead >= 0) 
                        auline.write(abData, 0, nBytesRead);
                }
            } 
        } catch (IOException e) { 
            e.printStackTrace();
            return;
        } finally { 
            auline.drain();
            auline.close();
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

你可以试试ComponentListener虽然它从未对我有效