×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    play wav file in j2me

    i have problem with playing wav file in j2me

    i have tried this code

    AudioPlayer java file in AudioSample package
    Code:
    package AudioSample;
    
    import javax.microedition.lcdui.*;
    import javax.microedition.media.*;
    
    public class AudioPlayer extends Form implements CommandListener, PlayerListener {
          PlayAudioMidlet midlet;
          private Display display;
          private Command play, stop, exit;
          private Player player;
    
          public AudioPlayer(PlayAudioMidlet midlet, Display display) {
                super("");
                this.midlet = midlet;
                this.display = Display.getDisplay(midlet);
                play = new Command("Play", Command.OK, 0);
                stop = new Command("Stop", Command.STOP, 0);
                exit = new Command("Exit", Command.EXIT, 0);
                addCommand(play);
                addCommand(stop);
                addCommand(exit);
                setCommandListener(this);
          }
    
          public void commandAction(Command c, Displayable d) {
                if (c == play) {
                      try {
                            playAudio();
                      } catch (Exception e) {
                      }
                } else if (c == stop) {
                      player.close();
                } else if (c == exit) {
                      if (player != null) {
                            player.close();
                      }
                      midlet.destroyApp(false);
                }
          }
    
          public void playerUpdate(Player player, String event, Object eventData) {
          }
    
          public void playAudio() {
                try {
                      player = Manager.createPlayer(getClass().getResourceAsStream("/helloworld.wav"), "audio/x-wav");
                      player.addPlayerListener(this);
                      player.setLoopCount(-1);
                      player.prefetch();
                      player.realize();
                      player.start();
                } catch (Exception e) {
                }
          }
    }
    and PlayAudioMidlet java file in AudioSample package
    Code:
    package AudioSample;
    
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    public class PlayAudioMidlet extends MIDlet {
          private Display display;
          AudioPlayer ap;
    
          public void startApp() {
                display = Display.getDisplay(this);
                ap = new AudioPlayer(this, display);
                display.setCurrent(ap);
          }
    
          public void pauseApp() {
          }
    
          public void destroyApp(boolean unconditional) {
                notifyDestroyed();
          }
    }
    and i put helloworld.wav in the project folder,,but when i run,,,there is nothing happened

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: play wav file in j2me

    Quote Originally Posted by fayed View Post
    Code:
          public void playAudio() {
                try {
                      player = Manager.createPlayer(getClass().getResourceAsStream("/helloworld.wav"), "audio/x-wav");
                      player.addPlayerListener(this);
                      player.setLoopCount(-1);
                      player.prefetch();
                      player.realize();
                      player.start();
                } catch (Exception e) {
                }
          }
    Avoid empty "catch" blocks like this... the device could be throwing you an exception to tell you the problem, and you're just ignoring it.

    Code:
    
                } catch (Exception e) {
                    append(e.toString());
                }
    
    See what appears.

    Graham.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Re: play wav file in j2me

    Hi!!
    First check where you are storing the songs ie .wav file. You should store it in the resource folder. Give a correct path in your code.If this does not work I can give you the code I implemented.
    Following is a part of code see if u can use:

    try
    { InputStream in =getClass().getResourceAsStream("/music.wav");
    Player player=Manager.createPlayer(in,"audio/x-wav");
    player.start();
    }
    catch(Exception e)
    {

    System.out.println("Error");
    return;
    }

    The song music.wav is included in the resource folder.
    I hope its of some help

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    Re: play wav file in j2me

    Quote Originally Posted by anjumara View Post
    Hi!!
    First check where you are storing the songs ie .wav file. You should store it in the resource folder. Give a correct path in your code.If this does not work I can give you the code I implemented.
    Following is a part of code see if u can use:

    try
    { InputStream in =getClass().getResourceAsStream("/music.wav");
    Player player=Manager.createPlayer(in,"audio/x-wav");
    player.start();
    }
    catch(Exception e)
    {

    System.out.println("Error");
    return;
    }

    The song music.wav is included in the resource folder.
    I hope its of some help
    thanks alot anjumara
    but i do what you say and i put helloworld.wav in the project folder and in the res folder in the project folder,,,and there is nothing happened

    i really don't know where is the error

    when i run ,,there is nothing happened....please help

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    Re: play wav file in j2me

    do anyone have answer for my problem?
    Last edited by fayed; 2010-04-25 at 10:01.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Thumbs up Re: play wav file in j2me

    Which IDE u R Using..!!!???Playing Audio IS very Simple In J2me...Put Ur Complete Code i can help u..else Follow this Simple PGM http://www.java2s.com/Tutorial/Java/.../Playaudio.htm ...Cheers

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: play wav file in j2me

    Quote Originally Posted by fayed View Post
    but i do what you say and i put helloworld.wav in the project folder and in the res folder in the project folder,,,and there is nothing happened

    i really don't know where is the error

    when i run ,,there is nothing happened....please help
    Check the JAR file that is created by your program, rename it as zip, open it using WINZIP and see if "helloworld.wav" is available inside it and at what path ? Let us know your findings and cut&paste your codelet matching to -

    try
    { InputStream in =getClass().getResourceAsStream("/music.wav");
    Player player=Manager.createPlayer(in,"audio/x-wav");
    player.start();
    }
    catch(Exception e)
    {

    System.out.println("Error");
    return;
    }
    Thanks,
    Ekta

  8. #8
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: play wav file in j2me

    Hey fayed,
    Welcome to Forum Nokia Discussion Boards..

    Well I have seen your code and I found a couple of issues with the same..as follows:

    1.Please remember that while you play any of the multimedia file, run in the thread.Never start a player inside the commandAction() of the keypressed().
    2.Why have you design and define the two midlets?
    That is not required...
    You take the AudionPlayer class and inside the commandAction(), if the command is play you start the thread and inside the run() you start the player and see if this works for you...
    Well you can follow like this;
    1
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

Similar Threads

  1. application error with 3gp file in J2ME
    By ruchig_iic in forum Mobile Java Media (Graphics & Sounds)
    Replies: 10
    Last Post: 2012-01-16, 21:40
  2. wav file to Byte[] array in J2ME (+playing it)
    By norrissmith in forum Mobile Java General
    Replies: 3
    Last Post: 2011-04-16, 14:24
  3. J2ME Beginner: Can not play WAV file on cellphone! Help me, please!
    By tonthatthienchuong in forum Mobile Java Media (Graphics & Sounds)
    Replies: 9
    Last Post: 2008-05-21, 13:29
  4. cannot able to play a video file.
    By shalini.mishra in forum Mobile Java Media (Graphics & Sounds)
    Replies: 3
    Last Post: 2006-11-13, 16:54
  5. Replies: 5
    Last Post: 2004-07-03, 05:26

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×