hello everyone. i have been trying to code an MP3 player, but Java does not support this, it only supports wav, thus wma, mp3 and other music or audio formats are denied. i thought of incoporating a converter, but still the converter is even failing to make the conversion: here is the code:
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.sound.sampled.*;
public class My_Play_Audio extends Thread
{
//global audio variables
javax.sound.sampled.AudioFormat audioFormat;
javax.sound.sampled.AudioInputStream audioInputStream;
javax.sound.sampled.SourceDataLine sourceDataLine;
boolean stopPlayback = false;
public void play(File soundFile, String fname)
{
String outputpath = "C:\\PnT-Temp\\playbackTrial.aiff";
try
{
// System.out.println("The audio Format is "+(audioInputStream.getFormat()).toString());
if(fname.endsWith("wav"))
{
audioInputStream = AudioSystem.getAudioInputStream(soundFile); //get audio stream
audioFormat = audioInputStream.getFormat(); //get format of stream
System.out.println(audioFormat.toString());
if(fname.endsWith("wav") && audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) //check that stream is proper format
{
audioInputStream = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED,audioInputStream);
audioFormat = audioInputStream.getFormat(); //fix improper wave format
}
}
else
{
System.out.println("This file ends is not wav ");
System.out.println("The input path is "+fname);
System.out.println("The output path is "+outputpath);
System.out.println("Check "+fname);
ConvertFileToAIFF(fname,outputpath);
soundFile = new File(outputpath);
audioInputStream = AudioSystem.getAudioInputStream(soundFile); //get audio stream
audioFormat = audioInputStream.getFormat(); //get format of stream
if(outputpath.endsWith("aiff") && audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) //check that stream is proper format
{
audioInputStream = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED,audioInputStream);
audioFormat = audioInputStream.getFormat(); //fix improper wave format
}
}
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class,audioFormat); //request a data line (not open)
sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo); //inform line of data type
stopPlayback = false; //allow playback
new Play_Audio().start(); //play thread
}
catch (Exception error)
{
JOptionPane.showMessageDialog(null, "The audio file cannot be opened!",
"Error! "+error.getMessage(), JOptionPane.INFORMATION_MESSAGE);
}
}
public void ConvertFileToAIFF(String inputPath, String outputPath)
{
AudioFileFormat inFileFormat;
File inFile;
File outFile;
try
{
inFile = new File(inputPath);
outFile = new File(outputPath);
}
catch (NullPointerException ex)
{
System.out.println("Error: one of the ConvertFileToAIFF" +" parameters is null!");
return;
}
try
{
// query file type
inFileFormat = AudioSystem.getAudioFileFormat(inFile);
if (inFileFormat.getType() != AudioFileFormat.Type.AIFF)
{
// inFile is not AIFF, so let's try to convert it.
AudioInputStream inFileAIS =
AudioSystem.getAudioInputStream(inFile);
inFileAIS.reset(); // rewind
if (AudioSystem.isFileTypeSupported(
AudioFileFormat.Type.AIFF, inFileAIS))
{
// inFileAIS can be converted to AIFF.
// so write the AudioInputStream to the
// output file.
AudioSystem.write(inFileAIS,
AudioFileFormat.Type.AIFF, outFile);
System.out.println("Successfully made AIFF file, "
+ outFile.getPath() + ", from "
+ inFileFormat.getType() + " file, " +
inFile.getPath() + ".");
inFileAIS.close();
return; // All done now
}
else
System.out.println("Warning: AIFF conversion of "
+ inFile.getPath()
+ " is not currently supported by AudioSystem.");
}
else
System.out.println("Input file " + inFile.getPath() +
" is AIFF." + " Conversion is unnecessary.");
}
catch (UnsupportedAudioFileException e)
{
System.out.println("Error: " + inFile.getPath()
+ " is not a supported audio file type!");
return;
}
catch (IOException e)
{
System.out.println("Error: failure attempting to read "
+ inFile.getPath() + "!");
return;
}
}
/**
*Play_Audio Class
*/
public class Play_Audio extends Thread
{
byte buffer[] = new byte[10000]; //10KB buffer, will not be too large for any system
/**
*Thread event which handles audio playback
*/
public void run()
{
try
{
sourceDataLine.open(audioFormat); //open the line
sourceDataLine.start(); //prepare line for data transfer
int continue_play;
while((continue_play = audioInputStream.read(buffer,0,buffer.length)) != -1 && stopPlayback == false)
{
if(continue_play > 0) //data still left to write
{
sourceDataLine.write(buffer, 0, continue_play); //write data to the line
}
}
sourceDataLine.drain(); //clear buffer
sourceDataLine.close(); //close line
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "The audio file cannot be opened!",
"Error!", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}