how to play sound

hi everybody
i want to play different sounds according to some action i am using the bellow code

public class Music {

private Player player;

public static Music

p = new Music(" /pfast.wav");
public Music(String s){
try{
inputStream is = getClass().getResourceAsStream(s);
String type = "audio/x-wav";
player = Manager.createPlayer(is, type);
}catch(Exception e){
System.out.println("errror" + s);
}

}
public static void play(Music m){
try{
m.player.stop();
m.player.start();
}catch(Exception e){}
}

}

but it is not working...
if i replace s by file name in the line
....inputStream is = getClass().getResourceAsStream(s);.....
it is working..
how to make this method general.
so that i can use it to play different sounds..
please any one help me it is urgent...

Comments

  • : hi everybody
    : i want to play different sounds according to some action i am using the bellow code
    :
    : public class Music {
    :
    : private Player player;
    :
    : public static Music
    :
    : p = new Music(" /pfast.wav");
    : public Music(String s){
    : try{
    : inputStream is = getClass().getResourceAsStream(s);
    : String type = "audio/x-wav";
    : player = Manager.createPlayer(is, type);
    : }catch(Exception e){
    : System.out.println("errror" + s);
    : }
    :
    : }
    : public static void play(Music m){
    : try{
    : m.player.stop();
    : m.player.start();
    : }catch(Exception e){}
    : }
    :
    : }
    :
    : but it is not working...
    : if i replace s by file name in the line
    : ....inputStream is = getClass().getResourceAsStream(s);.....
    : it is working..
    : how to make this method general.
    : so that i can use it to play different sounds..
    : please any one help me it is urgent...
    :
    :
    Remove the line "p = ....", and try to provide a null for the media type. Then the class is already general. You can use lines like this to create different sounds:
    [code]
    Music.play(new Music("hello.mp3"));
    Music.play(new Music("greensleeves.wav"));
    Music.play(new Music("ping.wav"));
    Music.play(new Music(someTextField.getText()));
    [/code]
    It might also be good to add a play() method, which has a Music object and a type string as parameters. That way the user of this class can specify the type.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion