001
002// SUPPRIMER L' illegal character CI-DESSUS
003/**
004 * Permet de lancer de la musique dans le jeu.
005 * 
006 * @author OpenClassroom
007 * @version 23/03/2020
008 */
009import  java.io.*;
010/*import javax.sound.sampled.AudioInputStream;
011import javax.sound.sampled.SourceDataLine;
012import javax.sound.sampled.AudioSystem;
013import javax.sound.sampled.Audio;*/
014import javax.sound.sampled.*;
015 
016public class Audio extends Thread{
017     
018     
019    AudioInputStream audioInputStream = null;
020    SourceDataLine line;
021    /**
022     * Méthode permettant de contourner les diverses possibles exceptions en chargeant la musique à partir d'un fichier
023     * entré en paramtètre et lance la musique.
024     * @param pSon le nom de la musique.
025     */
026    public void run(final String pSon){
027        File fichier = new File(pSon);
028        
029         
030        try {
031            audioInputStream = AudioSystem.getAudioInputStream(fichier);
032        } catch (UnsupportedAudioFileException e) {
033            e.printStackTrace();
034        } catch (IOException e) {
035            e.printStackTrace();
036        }
037     
038        AudioFormat audioFormat = audioInputStream.getFormat();
039        DataLine.Info info = new DataLine.Info(SourceDataLine.class,audioFormat);
040          
041        try {
042             line = (SourceDataLine) AudioSystem.getLine(info);
043                        
044             } catch (LineUnavailableException e) {
045               e.printStackTrace();
046               return;
047             }
048          
049        try {
050                line.open(audioFormat);
051        } catch (LineUnavailableException e) {
052                    e.printStackTrace();
053                    return;
054        }
055        line.start();
056        //Fenetre.begin=true;
057        try {
058            byte bytes[] = new byte[1024];
059            int bytesRead=0;
060            while ((bytesRead = audioInputStream.read(bytes, 0, bytes.length)) != -1) {
061                 line.write(bytes, 0, bytesRead);
062                }
063        } catch (IOException io) {
064            io.printStackTrace();
065            return;
066        }
067    }
068        
069    }
070