Click to See Complete Forum and Search --> : Embedded Java Program Walkthrough Requested


Mr Initial Man
10-04-2009, 06:44 PM
Will someone please walk me through building two Java audio players?

One will play MIDI files, one will play mp3 files.

They must:


Start on demand, rather than running automatically
Have a pause button

JavaServlet
10-24-2009, 09:43 PM
Will someone please walk me through building two Java audio players?

One will play MIDI files, one will play mp3 files.

They must:


Start on demand, rather than running automatically
Have a pause button


Use Java Media Framework.
The below shows something that should get you started using MediaLocator class. MIDI would be similiar. For the pause you can try and use stop and reset the media time to whatever it was before stop was called.


import javax.media.*;
...

public class MP3 extends Thread {

private URL myUrl;

public MP3(File mp3)
{
//put a try/catch for this.url to assign it to mp3: this.url = mp3.toURI().toURL();
......
}

public void run()
{
try
{
MediaLocator media = new MediaLocator(myUrl);
final Player player = Manager.createPlayer(media);
player.addControllerListener(new ControllerListener()
{
public void controllerUpdate(ControllerEvent event)
{
if (event instanceof EndOfMediaEvent)
{
player.stop();
player.close();
}
}
}
);
// add realize and start methods here
..

Mr Initial Man
10-25-2009, 08:34 AM
What's with the dots there? Please understand that I have very little experience with Java.

JavaServlet
10-25-2009, 10:02 AM
Here are the imports statements you can use with swing:

import javax.media.*;
import javax.swing.JFileChooser;
import java.io.*;
import java.net.URL;


Your try/block could look like this:

try {
this.url = mp3.toURI().toURL();
}
catch (java.net.MalformedURLException mux) {}
}

Call methods:

player.realize(); //helps connect to a media file or create a new one.
player.start();

Then create a main method for your JFileChooser and File objects.

I recommend you check Java Media Framework API (http://java.sun.com/javase/technologies/desktop/media/jmf/)to provide more guidance if needed.