//
// Core Program for TETDM
// SoundPlay.java Version 1.00
// Copyright(C) 2011-2018 TETDM CORE MEMBER ALL RIGHTS RESERVED.
// This program is provided under the license.
//

package source.Tutorial;


import source.*;
import source.TextData.*;
import source.Utility.*;
import source.GameMode.*;
import source.Tutorial.*;

import java.io.File;
import java.io.*;
import java.text.*;
import java.util.*;

import javax.sound.sampled.*;


public class SoundPlay implements Runnable
{
    String url;
    
    Clip line = null;
    
    public SoundPlay(String absolutePath, String filename)
    {
        url = absolutePath+"source"+File.separator+"Tutorial"+File.separator+"sound"+File.separator+filename;
        
        Thread th = new Thread(this);
        th.start();
    }
    
    public SoundPlay(String url)
    {
        this.url = url;
        
        Thread th = new Thread(this);
        th.start();
    }
    
    public void soundStop()
    {
        line.stop();
    }
    
    public boolean isRunning()
    {
        return line.isRunning();
    }
    
    public void run()
    {
        System.out.println("sound play" + url);
        
        AudioFormat format = null;
        DataLine.Info info = null;
  
        File audioFile = null;
        
        try{
            audioFile = new File(url);
            format = AudioSystem.getAudioFileFormat(audioFile).getFormat();
            info = new DataLine.Info(Clip.class, format);
            line = (Clip)AudioSystem.getLine(info);
            line.open(AudioSystem.getAudioInputStream(audioFile));
            line.start();
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
