Computer Science Canada

JAVA MUSIC

Author:  Master [ Wed May 05, 2004 7:09 pm ]
Post subject:  JAVA MUSIC

How do you put music into a java file
or what is the code to play music while an applet is running?
NEEEED HELp

Author:  rizzix [ Wed May 05, 2004 8:55 pm ]
Post subject: 

so if its only applets that ur worried about: thats easy, else no.

there is a  public AudioClip getAudioClip(URL url); of a JApplet or Apple object. since u most likley will be extending one of these classes, u may directly call the method, or simply like this:
code:
URL audiourl = this.getAudioClip(URL url);


Note the url is a URL object. You may get a reference to a url object by directly creating one urself with the proper _url_string_ passed to its constructor or u may get it from a File object by calling the public URL toURL(); public method.

either way, once u get a url to the audio file u can start playing the file by calling the public void play(URL url); method of the JApplet/Applet class, passing it the newly created url object.

NOTE: there's no way of stopping the music once started. for more control over ur music, u'll need to use a more advanced set of api, which is available, but not so easy to use. Also, i'm not sure if it plays mp3's heh, try it out. if it dosen't, its best u go for a third party _easier_ solution available at: http://www.javazoom.net/javalayer/javalayer.html

Author:  Master [ Wed May 05, 2004 9:04 pm ]
Post subject: 

what if i want to create it within the main of the program and i dont want to create new methods, is that possible

Author:  rizzix [ Wed May 05, 2004 9:08 pm ]
Post subject: 

hm.. yea i see what u mean.. anyways.. that solution up there is only for applets. so the above code will not work elsewhere.

now for simply creating a _program_ that will play the files.. try out the JLayer api @ javazoom. its ur best shot if ur new to java since is quite easy to use.

Author:  Dan [ Wed May 05, 2004 10:38 pm ]
Post subject: 

can not java meaid api not play muscik?

like:

code:

        private String mediaFile;
        private MediaLocator mrl = null;
        private Player player = null;
        private String nameOfFile = "some file";

File myFile = new File(fileName);
               
try
{       
 URL url = myFile.toURL();
 mediaFile = url.toExternalForm();
}
catch (MalformedURLException mue)
{                     
}

try
{
if ((mrl = new MediaLocator(mediaFile)) == null)
{
Fatal("Can't build URL for " + mediaFile);
}
               
try
{
        player = Manager.createPlayer(mrl);
}
catch (NoPlayerException e)
{
                                System.out.println(e);
                                Fatal("Could not create player for " + mrl);
}
               
player.addControllerListener(this);
}
catch (MalformedURLException e)
{
Fatal("Invalid media file URL!");
}
catch (IOException e)
{
Fatal("IO exception creating player for " + mrl);
}

player.start();


note i just copyed pices out of somting i am working on now, just sposted to be an ex of what i am talking about.

i think somting like that could play it, have yet to test tho

Author:  rizzix [ Thu May 06, 2004 12:03 am ]
Post subject: 

yea the media framework can play all music.. but does it play mp3s and such.. and its quite messy eh? JLayer is quite clean, and kinda easy to use.. of course JLayer is only an abstraction of the mediaframework.. so ur indirectly using the java media framework anyways

Author:  rizzix [ Thu May 06, 2004 12:06 am ]
Post subject: 

here r some examples using the Java Sound api: http://www.jsresources.org/examples/index.html

Author:  Dan [ Thu May 06, 2004 5:12 pm ]
Post subject: 

rizzix wrote:
but does it play mp3s and such..


It dose. Also plays vids but not all codecs.


: