| | |
MP3 Player or Converter
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2007
Posts: 14
Reputation:
Solved Threads: 0
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);
}
}
}
}
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);
}
}
}
}
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
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
www.houseshark.net
•
•
Join Date: Nov 2007
Posts: 14
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- trojans...now nothing opens and I get a paint can't open error (Viruses, Spyware and other Nasties)
- How Add Music Files to a MP3 Player (Windows 95 / 98 / Me)
- Please Help: System32 folder opens at start up (Viruses, Spyware and other Nasties)
- mpeg-4 audio to mp3? (Windows NT / 2000 / XP)
- Freeprodtb!! (Viruses, Spyware and other Nasties)
- What is all this about MP3 USB Keys ? (USB Devices and other Peripherals)
Other Threads in the Java Forum
- Previous Thread: JCanvas3D :-) or Canvas3D :-(
- Next Thread: Allocating larger memory through command line
Views: 1057 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android api apple applet application arguments array arrays automation binary block bluetooth chat class classes client code compile component database developmenthelp draw eclipse encode error event exception file fractal freeze game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer iphone j2me j2seprojects java javac javaprojects jmf jni jpanel julia lego linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number object online oracle print problem program programming project recursion scanner screen server set singleton size sms socket sort sql string swing system template test textfields threads time title transfer tree tutorial-sample update windows working






