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.BooleanControl;
import javax.sound.sampled.Control;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.FloatControl.Type;
import javax.swing.SwingUtilities;
public class Player
{
public Player()
{
}
public void playSong(String song)
{
final String songLocation = song;
{
try
{
File file = new File(songLocation);
fileSize = file.length();
in = AudioSystem.getAudioInputStream(file);
AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);
line = (SourceDataLine) AudioSystem.getLine(info);
if (line != null)
{
line.open(decodedFormat);
data = new byte[4096];
// Start
line.start();
long percentageOfFileRead;
long numberOfBytesRead;
while ((nBytesRead = din.read(data, 0, data.length)) != -1)
{
line.write(data, 0, nBytesRead);
System.out.println("Frame length = " + din.getFrameLength());
numberOfBytesRead = fileSize - in.available();
percentageOfFileRead = (numberOfBytesRead * 100) / fileSize;
MusicGUI.songSlider.setValue((int)percentageOfFile Read);
}
// Stop
/* line.drain();
line.stop();
line.close();
din.close();*/
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(din != null)
{
try
{
din.close();
}
catch(IOException e)
{
System.out.println("Error: " + e);
}
}
}
}
try
{
Thread.sleep(800);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
public void stopSong()
{
if(din != null)
{
try
{
line.stop();
line.close();
din.close();
}
catch(IOException e)
{
System.out.println("Error: " + e);
}
}
}
public void muteLine()
{
/* control = (BooleanControl)line.getControl(BooleanControl.Typ e.MUTE);
if (control.getValue() == true)
{
control.setValue(false);
}
else
{
control.setValue(true);
}
*/
volumeControl = (FloatControl)line.getControl(FloatControl.Type.MA STER_GAIN);
if (volumeControl.getValue() == -80)
{
volumeControl.shift(-80, 6, 400000000);
}
else
{
volumeControl.setValue(-80);
}
}
public int getElapsedTime()
{
return nBytesRead;
}
public void setElapsedTime(int newPosition)
{
if(newPosition > 0)
{
temp = (int)fileSize * newPosition / 100;
System.out.println("File Size = " + fileSize);
System.out.println("Slider Position = " + newPosition);
System.out.println("Number of bytes to skip to = " + temp);
try
{
din.skip(500);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
public String getVolume()
{
volumeControl = (FloatControl)line.getControl(FloatControl.Type.MA STER_GAIN);
return volumeControl.toString();
}
public void setVolume(int volume)
{
volumeControl = (FloatControl)line.getControl(FloatControl.Type.MA STER_GAIN);
volumeControl.setValue(volumeControl.getValue() + (float).1);
}
private long i = 0;
private int temp;
private long fileSize;
private byte[] data;
private int nBytesRead;
private SourceDataLine line = null;
private FloatControl volumeControl = null;
BooleanControl control = null;
AudioInputStream in = null;
AudioInputStream din = null;
}
Bookmarks