Tuesday, October 26, 2010

Playing Audio Files

player = Manager.createPlayer
             (getClass().getResourceAsStream("/res /filename.wav"), "audio/x-wav");
                  player.prefetch();
                  player.realize();
                  player.start();

How to find maximum size of RMS supported on a device.

This method returns the maximum RMS size supported on a devive. It make use of getSizeAvailable() method which returns the remaining available space and getSize() method which return the current size of RMS. The sum of these 2 value will give the max supported RMS size.

The size of RMS is very important in case of Midlet that require to store some amount of data in RMS. This error when happened is very difficult to trace out and may take few hours to find out.

Before using this method you need to make sure RMS is supported on the device. Most J2ME device need to support RMS, But if not sure can use the method to find out if RMS is supported.



Method to get Max RMS Size (Return as KB)

public static long getMaxRMSSize()
    {
        long size = 0;
        RecordStore rs = null;
        try
        {
            rs = RecordStore.openRecordStore("textrms", true);
            size = rs.getSizeAvailable() + rs.getSize();
        }
        catch(Exception ex)
        {
           
        }
        finally
        {
            try
            {
                if(rs!=null)
                {
                    rs.closeRecordStore();
                }
                RecordStore.deleteRecordStore("textrms");
            } catch(Exception e) {}
        }
        if(size>0)
        {
            //Convert bytes to KB by dividing by 1024
            size = size / 1024;
        }
        return size;
    }


Method to find if RMS is supported


  private static boolean isRMSSupported()
    {
        try
        {
            Class temp = Class.forName("javax.microedition.rms.RecordStore");
            return true;
        }
        catch (ClassNotFoundException ex)
        {
            return "false";
        }
    }

Sms Sending

You can send sms using j2me code by following code

String address = "sms://" + destinationAddress;
       
        MessageConnection smsconn = null;
        try {
            /** Open the message connection. */
            smsconn = (MessageConnection)Connector.open(address);
           
            TextMessage txtmessage = (TextMessage)smsconn.newMessage(MessageConnection.TEXT_MESSAGE);
            txtmessage.setPayloadText(destinationMessage);
            smsconn.send(txtmessage);
           
        }
        catch(Exception e) {
            e.printStackTrace();
        }