hyperAura
March 20th, 2010, 09:28 PM
Hello people, I found this on the web which is a class creating an mp3 object which behaves as a thread..
import javax.media.*;
import java.io.*;
import java.net.URL;
class mp3 extends Thread
{
private URL url;
private MediaLocator mediaLocator;
private Player playMP3;
public mp3(String mp3)
{
try{
this.url = new URL(mp3);
}catch(java.net.MalformedURLException e)
{System.out.println(e.getMessage());}
}//mp3 constructor
public void run()
{
try{
mediaLocator = new MediaLocator(url);
playMP3 = Manager.createPlayer(mediaLocator);
}catch(java.io.IOException e)
{System.out.println(e.getMessage());
}catch(javax.media.NoPlayerException e)
{System.out.println(e.getMessage());}
playMP3.addControllerListener(new ControllerListener()
{
public void controllerUpdate(ControllerEvent e)
{
if (e instanceof EndOfMediaEvent)
{
playMP3.stop();
playMP3.close();
}
}
}
);
playMP3.realize();
playMP3.start();
}//run
}//mp3 class
I can create a new object with this
mp3 thread = new mp3("file location");
and start playing music by doing
thread.start();
My problem is that I cannot stop the thread as Netbeans wont let me do it by thread.stop();
Is there another way that I can stop this tread? Thnx
import javax.media.*;
import java.io.*;
import java.net.URL;
class mp3 extends Thread
{
private URL url;
private MediaLocator mediaLocator;
private Player playMP3;
public mp3(String mp3)
{
try{
this.url = new URL(mp3);
}catch(java.net.MalformedURLException e)
{System.out.println(e.getMessage());}
}//mp3 constructor
public void run()
{
try{
mediaLocator = new MediaLocator(url);
playMP3 = Manager.createPlayer(mediaLocator);
}catch(java.io.IOException e)
{System.out.println(e.getMessage());
}catch(javax.media.NoPlayerException e)
{System.out.println(e.getMessage());}
playMP3.addControllerListener(new ControllerListener()
{
public void controllerUpdate(ControllerEvent e)
{
if (e instanceof EndOfMediaEvent)
{
playMP3.stop();
playMP3.close();
}
}
}
);
playMP3.realize();
playMP3.start();
}//run
}//mp3 class
I can create a new object with this
mp3 thread = new mp3("file location");
and start playing music by doing
thread.start();
My problem is that I cannot stop the thread as Netbeans wont let me do it by thread.stop();
Is there another way that I can stop this tread? Thnx