re-playing sound in J2ME

Discussion in 'Java' started by marko, Dec 14, 2004.

  1. marko

    marko Guest

    Hi,
    I'm working on a game in MIDP 2 J2ME and have problems with playing sound.
    I used this method to start playing sound:

    public void playsnd() {
    try {
    InputStream is = getClass().getResourceAsStream("/res/test.wav");
    Player p = Manager.createPlayer(is, "audio/x-wav");
    p.prefetch();
    p.start();
    }
    catch (IOException ex) {}
    catch (MediaException ex) {}

    }

    This method works fine except it has a long delay before sound actually
    starts playing.
    Is it possible to initialize and create player in class constructor and
    then call start() method on player when needed?
    I tried this but it gives me null pointer exception when I call
    startplaying() method:


    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.game.*;
    import java.util.Random;
    import javax.microedition.media.*;
    import javax.microedition.media.control.*;
    import java.io.*;

    public class ExampleGameCanvas extends GameCanvas implements Runnable {

    private Player p;

    public ExampleGameCanvas() throws Exception {

    InputStream is = getClass().getResourceAsStream("/res/test.wav");
    Player p = Manager.createPlayer(is, "audio/x-wav");
    p.prefetch();

    }

    public void startplaying() {

    try {
    p.start();
    }
    catch (MediaException ex) {}

    }

    }


    Thank You!
    marko, Dec 14, 2004
    #1
    1. Advertising

  2. marko wrote:
    > Hi,
    > I'm working on a game in MIDP 2 J2ME and have problems with playing sound.
    > I used this method to start playing sound:
    >
    > public void playsnd() {
    > try {
    > InputStream is = getClass().getResourceAsStream("/res/test.wav");
    > Player p = Manager.createPlayer(is, "audio/x-wav");
    > p.prefetch();
    > p.start();


    You don't need to prefetch the sound file. When you call start() it
    fetches it then and it takes just as long as if you called prefetch().

    > }
    > catch (IOException ex) {}
    > catch (MediaException ex) {}
    >
    > }
    >
    > This method works fine except it has a long delay before sound actually
    > starts playing.


    That's the time the system has to spend actually loading the data from
    the input stream. Believe it or not, it can be quite slow reading data
    from the JAR.

    > Is it possible to initialize and create player in class constructor and
    > then call start() method on player when needed?


    Yes, you can instantiate the player without calling start().

    > I tried this but it gives me null pointer exception when I call
    > startplaying() method:


    That's because you've declared an instance variable:

    > private Player p;


    But then in your constructor you created a local variable with the same
    name:

    > public ExampleGameCanvas() throws Exception {
    >
    > InputStream is = getClass().getResourceAsStream("/res/test.wav");
    > Player p = Manager.createPlayer(is, "audio/x-wav");


    Here you should instead be doing:

    p = Manager.createPlayer(is,"audio/x-wav");

    And that will fix *this* problem.

    --
    Darryl L. Pierce <>
    Visit my webpage: <http://mcpierce.multiply.com>
    "By doubting we come to inquiry, through inquiry truth."
    - Peter Abelard
    Darryl L. Pierce, Dec 15, 2004
    #2
    1. Advertising

  3. marko

    marko Guest

    > That's because you've declared an instance variable:
    >
    >> private Player p;

    >
    >
    > But then in your constructor you created a local variable with the same
    > name:
    >
    >> public ExampleGameCanvas() throws Exception {
    >> InputStream is = getClass().getResourceAsStream("/res/test.wav");
    >> Player p = Manager.createPlayer(is, "audio/x-wav");

    >
    >
    > Here you should instead be doing:
    >
    > p = Manager.createPlayer(is,"audio/x-wav");
    >
    > And that will fix *this* problem.
    >


    Yes, that solved the problem and now it works without any delay! THANK
    YOU very much :)
    marko, Dec 15, 2004
    #3
  4. marko wrote:

    >> Here you should instead be doing:
    >>
    >> p = Manager.createPlayer(is,"audio/x-wav");
    >>
    >> And that will fix *this* problem.

    >
    > Yes, that solved the problem and now it works without any delay! THANK
    > YOU very much :)


    No problem, mate. Glad to be of service. :)

    --
    Darryl L. Pierce <>
    Visit my webpage: <http://mcpierce.multiply.com>
    "By doubting we come to inquiry, through inquiry truth."
    - Peter Abelard
    Darryl L. Pierce, Dec 15, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Clarke

    Sound lost when playing AVI files

    Ron Clarke, Feb 24, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    349
    Ron Clarke
    Feb 24, 2004
  2. Ike

    playing a sound

    Ike, Feb 26, 2004, in forum: Java
    Replies:
    6
    Views:
    435
    Erwin Moller
    Feb 26, 2004
  3. Replies:
    0
    Views:
    370
  4. Christian Kremser

    Problem with playing sound files

    Christian Kremser, Mar 8, 2005, in forum: Java
    Replies:
    1
    Views:
    347
    Knute Johnson
    Mar 9, 2005
  5. Replies:
    0
    Views:
    504
Loading...

Share This Page