PAGES

Tuesday, October 19, 2010

Using List and Gauge (AudioPlayer continued...)

This tutorial is an extension to the previous one. To view that click here.
In this tutorial we will learn 2 more display items.First is a list which itself creates a single instance of screen display just like form.LIST are of 3 types:-
  • EXCLUSIVE 
  • IMPLICIT 
  • MULTIPLE
In this tutorial we will use implicit type. Syntax of list is:-
List(String title, int listType, String[] stringElements, Image[] imageElements)

Also we learn how to create a gauge.
We are creating the object of Gauge class that contains the following parameters label, interactive, maxValue and initialValue and it has been set with the form.  We can also set the new volume label, if the interactive  status is true. But if the status is set as  false then we can not change the value of bar label.
Syntax:-
gauge = new Gauge(label, interactive, maxValue ,initialValue);
ex:-gauge = new Gauge("Volume", true, 52);

We also added more functionality to the player like pause ,resume and volume control.
By adding list we now can choose from more than 1 sounds to be played.For that download the following .wav sounds and put it in src folder as explained in earlier tutorial. 


1.wav
2.wav
3.wav


To view video tutorial online click here


To download click here


Source Code:-


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

public class j2MEAudioPlayer extends MIDlet implements CommandListener ,ItemStateListener  {

    public String[] elements ={"1","2","3"};
    public List list =new List("Select Audio File", List.IMPLICIT, elements,null);
    public StringItem si,time;
    private Gauge gauge = new Gauge("Volume: 50", true, 100, 50);
    private VolumeControl volume;
    private Form volume1 =new Form("Adjust volume");

    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 Command vol =new Command("Volume",Command.SCREEN,1);
    private Command back =new Command("back",Command.BACK,2);
    private Command pause =new Command("Pause",Command.SCREEN,1);
    private Command Resume =new Command("Resume",Command.SCREEN,1);

    private Player player;

    public j2MEAudioPlayer()
    {
    list.addCommand(Play);
    list.addCommand(exitCommand);
    list.setCommandListener(this);
    form.addCommand(pause);
    form.addCommand(Resume);
    form.addCommand(vol);
    form.addCommand(back);
    form.setCommandListener(this);
    volume1.append(gauge);
    volume1.addCommand(back);
    volume1.setItemStateListener(this);
    volume1.setCommandListener(this);

    }
    public void startApp() {
        Display.getDisplay(this).setCurrent(list);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
       if (c == exitCommand)
       {
       cleanUp();
       notifyDestroyed();
       return;
       }
       if (c==Play)
       {
           String s=list.getString(list.getSelectedIndex());
           s="/"+s+".wav";
           si=new StringItem("Playing",s);
           playMedia(s);
           form.append(si);
           Display.getDisplay(this).setCurrent(form);
       }
       if(c==vol)
       {
           Display.getDisplay(this).setCurrent(volume1);
       }
       if((c==back)&&(d==form))
       {
           cleanUp();
           Display.getDisplay(this).setCurrent(list);
       }
       if((c==back)&&(d==volume1))
       {
           Display.getDisplay(this).setCurrent(form);
       }
           if(c==pause)
       {
        try
        {
            player.stop();
        }
        catch (Exception e)
        {
        }
       }
        if(c==Resume)
       {
         try {
         player.start();
         } catch (Exception e) {
         }
       }
    }
    public void cleanUp() {
    if (player != null) {
      player.close();
      player = null;
    }
  }

     public void playMedia(String locator)
     {
         try{
             player = Manager.createPlayer(getClass().getResourceAsStream(locator),"audio/x-wav");
             player.realize();
             volume = (VolumeControl) player.getControl("VolumeControl");
             volume.setLevel(70);
             gauge.setValue(volume.getLevel());
             gauge.setLabel("Volume: " + volume.getLevel());
             player.setLoopCount(2);
             float t= player.getMediaTime();
             String c=String.valueOf(t);
             time=new StringItem("Time",c);
             form.append(time);
             player.start();
         }
         catch (Exception e) {
             e.printStackTrace();
         }
     }

    public void itemStateChanged(Item item) {
            volume.setLevel(gauge.getValue());
            gauge.setLabel("Volume: " + volume.getLevel());
    }
}                                                                                                                          NEXT TUTORIAL

3 comments:

  1. nice tutorial sir ..lot of things learnt from this tutorial

    ReplyDelete
  2. Nice tutorial... How to display current mediaTime on screen while playing the player so that a user can come to know that this much amount of time is being left.

    ReplyDelete
  3. I want to fetch the wav files from the memory card and want to create a play list. is it possible ?

    ReplyDelete

customised by Vaibhav