Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Importing mp3 files
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
elmocookies




PostPosted: Wed Jun 11, 2008 9:34 pm   Post subject: Importing mp3 files

Does anyone know how to import mp3 files onto Java and then play them?

Thanks.
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Thu Jun 12, 2008 9:29 pm   Post subject: RE:Importing mp3 files

Go look up the Java Media Framework (javax.media.*). Google that and you should find all the API you'll need. I've never used it myself, so I can't be of specific help, but Google should find you a tutorial just fine.
Light




PostPosted: Fri Jun 13, 2008 9:25 am   Post subject: RE:Importing mp3 files

You can get the JMF (Java media framework) with MP3 plug-in here: http://java.sun.com/javase/technologies/desktop/media/jmf/mp3/download.html

And find documentation about it here: http://java.sun.com/javase/technologies/desktop/media/jmf/reference/docs/index.html
Hendo




PostPosted: Sun Jun 15, 2008 7:08 pm   Post subject: Re: Importing mp3 files

Heres a couple neat methods I put together for playing wav files (im not sure if you have to use mp3 but this will only work with wavs)

This one is used if the wav is fairly long (usually full songs, anything longer than say 30 seconds and you should use this)
JAVA:
import javax.sound.sampled.*;
 public void playLine (String file)
    {
        try
        {
            AudioInputStream stream = AudioSystem.getAudioInputStream (new File (file));

            // From URL
            //stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile"));

            // At present, ALAW and ULAW encodings must be converted
            // to PCM_SIGNED before it can be played
            AudioFormat format = stream.getFormat ();
            if (format.getEncoding () != AudioFormat.Encoding.PCM_SIGNED)
            {
                format = new AudioFormat (
                        AudioFormat.Encoding.PCM_SIGNED,
                        format.getSampleRate (),
                        format.getSampleSizeInBits () * 2,
                        format.getChannels (),
                        format.getFrameSize () * 2,
                        format.getFrameRate (),
                        true);        // big endian
                stream = AudioSystem.getAudioInputStream (format, stream);
            }

            // Create line
            SourceDataLine.Info info = new DataLine.Info (
                    SourceDataLine.class, stream.getFormat (),
                    ((int) stream.getFrameLength () * format.getFrameSize ()));
            SourceDataLine line = (SourceDataLine) AudioSystem.getLine (info);
            line.open (stream.getFormat ());
            line.start ();

            // Continuously read and play chunks of audio
            int numRead = 0;
            byte [] buf = new byte [line.getBufferSize ()];
            while ((numRead = stream.read (buf, 0, buf.length)) >= 0)
            {
                int offset = 0;
                while (offset < numRead)
                {
                    offset += line.write (buf, offset, numRead - offset);
                }
            }
            line.drain ();
            line.stop ();
        }
        catch (Exception e)
        {
            e.printStackTrace ();
            System.exit (1);
        }
    }


    public static void delay (int ms)
    {
        try
        {
            Thread.sleep (ms);
        }


        catch (Exception e)
        {
            ;
        }
    }



and then you can simply play a song by calling it like this:
code:
playLine("mysong.wav")


This next snippet is for smaller clips like loops or short music clips (under 30 seconds long)
JAVA:

import javax.sound.sampled.*; public void playClip (String file, boolean loop)
    {
        try
        {
            // From file
            AudioInputStream stream = AudioSystem.getAudioInputStream (new File (file));

            // From URL
            // stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile"));

            // At present, ALAW and ULAW encodings must be converted
            // to PCM_SIGNED before it can be played
            AudioFormat format = stream.getFormat ();
            if (format.getEncoding () != AudioFormat.Encoding.PCM_SIGNED)
            {
                format = new AudioFormat (
                        AudioFormat.Encoding.PCM_SIGNED,
                        format.getSampleRate (),
                        format.getSampleSizeInBits () * 2,
                        format.getChannels (),
                        format.getFrameSize () * 2,
                        format.getFrameRate (),
                        true);        // big endian
                stream = AudioSystem.getAudioInputStream (format, stream);
            }

            // Create the clip
            DataLine.Info info = new DataLine.Info (
                    Clip.class, stream.getFormat (), ((int) stream.getFrameLength () * format.getFrameSize ()));
            Clip clip = (Clip) AudioSystem.getLine (info);

            // This method does not return until the audio file is completely loaded
            clip.open (stream);

            // Start playing
            if (loop)
                clip.loop (Clip.LOOP_CONTINUOUSLY);
            else
                clip.start ();
        }
        catch (MalformedURLException e)
        {
        }
        catch (IOException e)
        {
        }
        catch (LineUnavailableException e)
        {
        }
        catch (UnsupportedAudioFileException e)
        {
        }
    }


Using clips gives you the option of looping it and I added a handy loop boolean in the parameters so if you wanted to play a clip and loop it you would call it like this
code:
playClip("myloop.wav",true)



Hope that helps
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: