Ring Tones MIDlet Example

In this example only two Ring Tones are created using Player class.

Ring Tones MIDlet Example

Ring Tones MIDlet Example

     

In this example only two Ring Tones are created using Player class. Find the given methods that are used in player class :

  • addPlayerListener(PlayerListener playerListener) 
  • close()
  • deallocate()
  • getContentType()
  • getDuration()
  • getMediaTime()
  • getState()
  • prefetch()
  • realize()
  • removePlayerListener(PlayerListener playerListener)
  • setLoopCount(int count)
  • setMediaTime(long now)
  • start()
  • stop()

The Application is as follows:

 

Source Code of RingTones.java

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

public class RingTones extends MIDlet implements ItemStateListener, 
CommandListener{
  private Display display;  
  private Form form;  
  private Command exit; 
  private ChoiceGroup choice;
  private Player player1, player2;

  public void startApp(){
  try {
  player1 = Manager.createPlayer(getClass().getResourceAsStream(
"/kabhi-alvida-na-kehna.wav"), "audio/x-wav");
  player2 = Manager.createPlayer(getClass().getResourceAsStream(
"/aagepiche.wav"), "audio/x-wav");
  } catch(MediaException e) {
  e.printStackTrace();
  }catch(IOException ioe){
  ioe.printStackTrace();
  }
  display = Display.getDisplay(this);
  choice = new ChoiceGroup("List of RingTones", Choice.EXCLUSIVE);
  choice.append("Kabhi Alvida Na Kehna", null);  
  choice.append("Golmaal Return", null);  
  exit = new Command("Exit", Command.EXIT, 1);
  form = new Form("Playing song");
  form.append(choice);
  form.addCommand(exit);
  form.setCommandListener(this); 
  form.setItemStateListener(this);
  display.setCurrent(form);
  }

  public void pauseApp(){}

  public void destroyApp(boolean unconditional){
  notifyDestroyed();
  }

  public void commandAction(Command c, Displayable s){
  String label = c.getLabel();
  if(label.equals("Exit")){
  destroyApp(false);
  } 
  }

  public void itemStateChanged(Item item){
  switch(choice.getSelectedIndex()){
  case 0:
  try{
  player1.start();
  if(player2 != null) player2.stop();
  }catch(MediaException e) {
  e.printStackTrace();
  }
  break;

  case 1:
  try{
  player2.start();
  if(player1 != null) player1.stop();
  }catch(MediaException e) {
  e.printStackTrace();
  }
  break;
  }
  }
}

Download Source Code