Friday, October 8, 2010

Simple Audio player mobile application using J2ME

In this tutorial we will learn  how to create a simple audio player which plays .wav sound files.
For this we use javax.microedition.media.* package.
This provides us with Player class which allows us to create audio player.
To run this program you need to have a .wav file named as "1.wav".
Download sample .wav file from here.

To start with this tutorial you need to go through all previous tutorials .        First Tutorial

To view video tutorial online click here


To download click here

Sorce code:-

J2MEAudioPlayer.java 


import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.media.*;

public class J2MEAudioPlayer extends MIDlet implements CommandListener {

    public Form form = new Form("AudioPlayer");
    private Command Play =new Command("Play",Command.SCREEN,1);
    private Command exitCommand = new Command("Exit", Command.EXIT, 1);
    private Player player;

    public J2MEAudioPlayer()
    {
    form.addCommand(Play);
    form.addCommand(exitCommand);
    form.setCommandListener(this);
    }
    public void startApp() {
        Display.getDisplay(this).setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
       if (c == exitCommand)
       {
       cleanUp();
       notifyDestroyed();
       return;
       }
       if (c==Play)
       {
            playMedia("/1.wav");
            Display.getDisplay(this).setCurrent(form);
       }
    }
    public void cleanUp() {
    if (player != null) {
      player.close();
      player = null;
    }
  }

     public void playMedia(String locator)
     {
         try{
             player = Manager.createPlayer(getClass().getResourceAsStream("/1.wav"),"audio/x-wav");
             player.realize();
             player.start();
         }
         catch (Exception e) {
             e.printStackTrace();
         }
     }
}

No comments:

Post a Comment

customised by Vaibhav