×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    1

    How to play amr file in j2me?

    I'm still confused with playing amr file in j2me. I have a source code that can play midi file but I want it can play amr file too. I try to modify it but it still doesn't work.
    I use wtk 2.5.2.
    When I change :
    Code:
         
    InputStream is = getClass().getResourceAsStream("/a.mid");
    player = Manager.createPlayer(is, "audio/midi");
    to

    Code:
    InputStream is = getClass().getResourceAsStream("/b.amr");
    player = Manager.createPlayer(is, "audio/amr");
    and I compile, it always says "cannot realize player"

    Here is the full source code
    Code:
    import java.io.InputStream;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.media.*;
    import javax.microedition.media.control.VolumeControl;
    
    public class MidiPlayer extends MIDlet 
      implements CommandListener {
        
      private Display display;
      private List menu;
      private Form form;
      private Gauge volume;
      String[] types = javax.microedition.media.Manager.getSupportedContentTypes(null);
      
      private Player player;
      private VolumeControl vc;
     
      private final Command cmdKeluar = 
        new Command("Exit", Command.EXIT, 1);
      private final Command cmdMainkan =
        new Command("Play", Command.SCREEN, 1);
      private final Command cmdVolume =
        new Command("Setting Volume", Command.SCREEN, 1);
      private final Command cmdOK =
        new Command("OK", Command.OK, 1);
      private final Command cmdBerhenti =
        new Command("Stop", Command.OK, 2);
      
      private final String[] FILE = 
         {"a.amr", "b.amr", "File3.mid"};
      
      public MidiPlayer() {
        display = Display.getDisplay(this);
        
        menu = new List("MIDI Player", Choice.IMPLICIT, FILE, null);
               
        menu.addCommand(cmdKeluar);
        menu.addCommand(cmdMainkan);
        menu.addCommand(cmdVolume);
        menu.setCommandListener(this);    
      }
        
      public void startApp() {
        form = new Form("Volume");
        volume = new Gauge("Setting Volume", true, 10, 5);
        display.setCurrent(menu);
      }
        
      public void pauseApp() {
      }
        
      public void destroyApp(boolean unconditional) {
      }
        
      public void commandAction(Command c, Displayable s) {
        if (c == cmdKeluar) {
          destroyApp(false);
          notifyDestroyed();
        } else if (c == cmdMainkan) {
          String namaFile = menu.getString(menu.getSelectedIndex());
          playMIDI(namaFile);
          menu.removeCommand(cmdMainkan);
          menu.removeCommand(cmdKeluar);
          menu.removeCommand(cmdVolume);
          menu.addCommand(cmdBerhenti);
        } else if (c == cmdBerhenti) {
          stopMIDI();
        } else if (c == cmdOK) {
          display.setCurrent(menu);
        } else {
          form.append(volume);
          form.addCommand(cmdOK);
          form.setCommandListener(this);
          display.setCurrent(form);
        }
      }
      
      public void playMIDI(String namaFile) {
        try {
          InputStream is = getClass().getResourceAsStream("/b.amr");
          player = Manager.createPlayer(is, "audio/amr");
    	  player.realize();
          player.addPlayerListener(new StopListener(this));
          vc = (VolumeControl) player.getControl("VolumeControl");
          vc.setLevel(volume.getValue() * 10);
          player.start();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      
      public void stopMIDI() {
        try {
          player.stop();
          player.deallocate();
          player.close();
          player = null;
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      
      // inner class
      class StopListener implements PlayerListener {
          
        private MidiPlayer midlet;
          
        public StopListener(MidiPlayer midlet) {
          this.midlet = midlet;
        }
          
        public void playerUpdate(Player player, 
                                 String event, Object data) {
          try {
            if (event.equals(STOPPED) ||
              event.equals(STOPPED_AT_TIME) ||
              event.equals(ERROR) ||
              event.equals(END_OF_MEDIA)) {
              player.deallocate();
              player.close();
              player = null;
              midlet.menu.removeCommand(midlet.cmdBerhenti);
              midlet.menu.addCommand(midlet.cmdMainkan);
              midlet.menu.addCommand(midlet.cmdVolume);
              midlet.menu.addCommand(midlet.cmdKeluar);
            } else if (event.equals(DEVICE_UNAVAILABLE)) {
              player.stop();
            } else if (event.equals(DEVICE_AVAILABLE)) {
              player.start();
            }
          } catch (Exception e) {
            e.printStackTrace();
          }
        }          
      } 
      
    }
    regards,
    yunus

  2. #2
    Registered User
    Join Date
    Feb 2007
    Location
    India
    Posts
    1,128

    Re: How to play amr file in j2me?

    Hi yunus,

    Same discussion is already done here

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    39

    Re: How to play amr file in j2me?

    AMR play works fine for me if it is used in this way ...

    player = Manager.createPlayer ("file://"+<absolute path to AMR>);

Similar Threads

  1. Cannot Play AMR File
    By khalandar in forum Mobile Java Media (Graphics & Sounds)
    Replies: 3
    Last Post: 2007-06-13, 11:00
  2. About play amr file
    By gopal_dave in forum Symbian
    Replies: 4
    Last Post: 2007-05-11, 14:03
  3. How to play AMR file in EMULATOR
    By 83.manish in forum Symbian Media (Closed)
    Replies: 1
    Last Post: 2006-08-01, 05:37
  4. My .AMR file will not play on my 6255i
    By TravDalton in forum Audio
    Replies: 0
    Last Post: 2006-02-25, 04:36
  5. Play an AMR file
    By elena_santander in forum Symbian Tools & SDKs
    Replies: 0
    Last Post: 2003-04-24, 09:08

Posting Permissions

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