MP3 Player or Converter

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 14
Reputation: edensigauke is an unknown quantity at this point 
Solved Threads: 0
edensigauke edensigauke is offline Offline
Newbie Poster

MP3 Player or Converter

 
0
  #1
Nov 4th, 2008
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);

}
}
}
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: MP3 Player or Converter

 
0
  #2
Nov 4th, 2008
please put code in code tags, give us a specific error or problem you are having, and show us what piece of code you are having trouble with
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: MP3 Player or Converter

 
0
  #3
Nov 4th, 2008
why trying to re-invent the wheel?
http://www.javazoom.net/index.shtml
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 14
Reputation: edensigauke is an unknown quantity at this point 
Solved Threads: 0
edensigauke edensigauke is offline Offline
Newbie Poster

Re: MP3 Player or Converter

 
0
  #4
Nov 7th, 2008
Yes i do want to reinvent the wheel, because if i know how it was invented then i will be able to develop it further and also know where it is going.

well the problem snippets is in the AudioFormat, which is supposed to return the type of audio that it is, but it always bounces when trying to read MP3's, so i put the converter, but that was to no avail, because that readaudiostreaminput still fails to capture the audio content on Mp3's

so please help, i read about the Clip, but it is not clear on how it works, there are more examples and research over the AudioStream than this one, so help.

if you have an idea of how Clips work, an example would be appreciated.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: MP3 Player or Converter

 
0
  #5
Nov 7th, 2008
you could take a look at the open source java player at the link I posted earlier, or you could take a look at the JMF library
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 1057 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC