본문 바로가기

Java Sound/Example 1

Java Sound Example

자바에서 사운드를 출력하는 간단한 예 (Java Sound Play Example)


import java.io.*;

import javax.sound.sampled.*;



gameover_wav.file


사운드 파일의 절대경로를 사용하는 경우, (Loading sound file from project outside)

호출 예) playSound( "d:/test/gameover.wav" )


private void jButtonPlaySoundActionPerformed(java.awt.event.ActionEvent evt) {                                                 

        playSound("d:/test/gameover.wav");

 }


public void playSound(String filename) { // d:/test/gameover.wav

        try {

            File soundFile = new File(filename);

            final Clip clip = AudioSystem.getClip();

            clip.addLineListener(new LineListener()

            {

                @Override

                public void update(LineEvent event)

                {

                    //CLOSE, OPEN, START, STOP

                    if (event.getType() == LineEvent.Type.STOP)

                        clip.close();

                }

            });

            clip.open(AudioSystem.getAudioInputStream(soundFile));

            clip.start();

        }

        catch (Exception e) {

            e.printStackTrace();

        }

    }



프로젝트 내에 사운드 파일이 포함되어 있는 경우 (Loading sond file from project inside)

호출 예) playSound( "gameover.wav" )


private void jButtonPlaySoundActionPerformed(java.awt.event.ActionEvent evt) {                                                 

        playSound("gameover.wav");

   

public void playSound(String filename){

        try {

            URL url = getClass().getResource(filename);

            File file = new File(url.getPath());


            final Clip clip = AudioSystem.getClip();

            clip.addLineListener(new LineListener()

            {

                @Override

                public void update(LineEvent event)

                {

                    //CLOSE, OPEN, START, STOP

                    if (event.getType() == LineEvent.Type.STOP)

                        clip.close();

                }

            });

            clip.open(AudioSystem.getAudioInputStream(file));

            clip.start();

        }

        catch (Exception e) {

            e.printStackTrace();

        }

    }



사운드 조절 예

import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.BooleanControl;

import javax.sound.sampled.Clip;

import javax.sound.sampled.DataLine;

import javax.sound.sampled.FloatControl;


public class Main {

  public static void main(String[] argv) throws Exception {

    DataLine.Info info = null;

    Clip clip = (Clip) AudioSystem.getLine(info);


    FloatControl gainControl = (FloatControl) clip

        .getControl(FloatControl.Type.MASTER_GAIN);

    double gain = .5D; // number between 0 and 1 (loudest)

    float dB = (float) (Math.log(gain) / Math.log(10.0) * 20.0);

    gainControl.setValue(dB);


    BooleanControl muteControl = (BooleanControl) clip

        .getControl(BooleanControl.Type.MUTE);

    muteControl.setValue(true);


    muteControl.setValue(false);


  }

}