×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    1

    Unhappy How to play & record audio byte arrays with j2me on nokia

    - How do i play a series of audio byte arrays with j2me?
    - How do i take the micro phone audio input and extra the audio as a series of byte arrays?
    been working to sort that out for a week now!
    thanks...

  2. #2
    Regular Contributor
    Join Date
    Nov 2009
    Posts
    53

    Re: How to play & record audio byte arrays with j2me on nokia

    See the code below, ( in this the mobile application get sound files from the server and runs one by one). This will be helpful for your requirement.
    import java.io.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.media.Player;
    import javax.microedition.media.Manager;
    import javax.microedition.media.PlayerListener;
    import javax.microedition.media.control.*;
    import javax.microedition.rms.*;
    import javax.microedition.io.*;


    public class LoadCanvas extends Canvas implements CommandListener,PlayerListener,Runnable
    {
    Player[] music;
    Player currentPlayer;
    private RecordStore rs;
    private DataInputStream din;
    private ContentConnection connection;
    private Command play;
    private Command exit;
    private LoadRosary midlet;
    private Thread thread;

    private int incPlay; // To increment the player..
    private static final int VOLUME_LEVEL=50; // Initial sound level
    private int volumeSetting = VOLUME_LEVEL; // Tracks the sound level
    private boolean volumeMuted=false; // Tracks whether muted or not.
    private Image rosaryImg;
    private int songArray[] = new int[88];
    private int recid;

    private final static String[] musicUrl ={
    "http://202.63.115.10/ob/rosary/INTRODUCTION.amr", //0
    "http://202.63.115.10/ob/rosary/FIRST_SORROWFUL.amr", //1
    "http://202.63.115.10/ob/rosary/SECOND_SORROWFUL.amr", //2
    "http://202.63.115.10/ob/rosary/THIRD_SORROWFUL.amr", //3
    "http://202.63.115.10/ob/rosary/FOURTH_SORROWFUL.amr", //4
    "http://202.63.115.10/ob/rosary/FIFTH_SORROWFUL.amr", //5
    "http://202.63.115.10/ob/rosary/MYSTERY_SONG.amr", //6

    "http://202.63.115.10/ob/rosary/1_IN_THE_NAME_OF.amr", //7
    "http://202.63.115.10/ob/rosary/2_I_BELIEVE_IN_GOD.amr", //8
    "http://202.63.115.10/ob/rosary/3_OUR_FATHER.amr", //9
    "http://202.63.115.10/ob/rosary/4_HAIL_MARY.amr", //10
    "http://202.63.115.10/ob/rosary/5_GLORY_BE.amr", //11
    "http://202.63.115.10/ob/rosary/6_O_MY_JESUS.amr", //12
    "http://202.63.115.10/ob/rosary/7_HAIL_HOLY_QUEEN.amr", //13
    "http://202.63.115.10/ob/rosary/8_O_GOD_WHOSE.amr" //14
    };

    public LoadCanvas(LoadRosary midlet)
    {
    this.midlet = midlet;
    play = new Command("Play",Command.SCREEN,1);
    exit = new Command("Exit",Command.EXIT,2);
    addCommand(play);
    addCommand(exit);
    setCommandListener(this);
    incPlay =0;
    try{
    rosaryImg = Image.createImage("/rosary.png");
    rs = RecordStore.openRecordStore("Ros",true);

    if(rs.getNumRecords()==0)
    {
    loadViaHttp();
    System.out.println(" Num Records ;"+rs.getNumRecords());
    }

    } catch(Exception ex){}
    thread = new Thread(this);
    thread.start();
    }

    public void run()
    {
    try{
    if(rs.getNumRecords() > 0)
    {
    playFromRms();
    }
    }catch(Exception e) {}
    }


    public void paint(Graphics g)
    {
    g.drawImage(rosaryImg,0,0,20);
    g.setColor(255,128,0);
    g.drawString("Sorrowful Mystery",(getWidth())/2,2, g.TOP|g.HCENTER);
    }

    // To load from the server...............//
    public void loadViaHttp()
    {
    try{
    byte [] byteAudio;
    for(int i=0;i<musicUrl.length;i++){
    connection = (ContentConnection)Connector.open(musicUrl[i]);
    din = connection.openDataInputStream();
    int length = -1; // (int)connection.getLength();
    if(length != -1)
    {
    byteAudio = new byte[length];
    din.readFully(byteAudio);
    recid=rs.addRecord(byteAudio,0,byteAudio.length);
    }
    else // If length not available.......
    {
    ByteArrayOutputStream bstr = new ByteArrayOutputStream();
    int ch;
    while(( ch=din.read())!=-1)
    bstr.write(ch);
    byteAudio = bstr.toByteArray();
    recid =rs.addRecord(byteAudio,0,byteAudio.length);
    }
    din.close();
    connection.close();

    }
    System.out.println(" Total records; " +rs.getNumRecords());
    }catch(Exception e) {
    e.printStackTrace();
    System.out.println("Exception in Loading.."+e);
    }
    }

    // To play files from RMS........
    public void playFromRms()
    {
    music=new Player[musicUrl.length+1];
    try {
    for(int i=1;i<=musicUrl.length;i++)
    {
    InputStream is = new ByteArrayInputStream(rs.getRecord(i));
    music[i-1] = Manager.createPlayer(is,"audio/amr");
    }
    System.out.println(" Total records;in playfrom rms " +rs.getNumRecords());
    soundInitialise();
    music[0].realize();
    music[0].prefetch();
    startPlayer(0);
    music[7].realize();
    }catch(Exception e) {System.out.println("Excepion in Play rms"+e);}
    }


    private void startPlayer(int numAmr)
    {
    if(numAmr<musicUrl.length)
    {
    try{
    // music[numAmr].realize();
    music[songArray[numAmr]].prefetch();
    if(incPlay <87)music[songArray[numAmr]].start();
    currentPlayer = music[songArray[numAmr]];
    currentPlayer.addPlayerListener(this);
    if(numAmr+1<songArray.length)music[songArray[numAmr+1]].realize();
    System.out.println(" playing..: " +numAmr + " Song: "+musicUrl[numAmr].substring(30,43));
    // To control the volume...
    VolumeControl vc = (VolumeControl)currentPlayer.getControl("VolumeControl");
    vc.setLevel(VOLUME_LEVEL);
    }catch(Exception e){e.printStackTrace();
    System.out.println("Exception in Start player"+e);}
    }
    }

    private void soundInitialise()
    {
    arrayInit(0,0,7); // In the name of
    arrayInit(1,1,8); // I believe in
    arrayInit(2,2,9); // our father .
    arrayInit(3,5,10); //.Hail mary.
    arrayInit(6,6,11); // glory be .
    arrayInit(7,7,12); // Oh my jesus..

    arrayInit(8,8,6); // Mystery song....
    arrayInit(9,9,1); // first decade announce;
    arrayInit(10,10,9); // our father.
    arrayInit(11,20,10); // Hail mary..
    arrayInit(21,21,11); // Glory be.
    arrayInit(22,22,12); // Oh my jesus..

    arrayInit(23,23,6); // Mystery song..
    arrayInit(24,24,2); // Second decade..
    arrayInit(25,25,9); // our father.
    arrayInit(26,35,10); // hail mary.
    arrayInit(36,36,11); // Glory be.
    arrayInit(37,37,12); // Oh my jesus.

    arrayInit(38,38,6); // mystey song..
    arrayInit(39,39,3); // Third decade announce.
    arrayInit(40,40,9); // our father ..
    arrayInit(41,50,10); // Hail mary..
    arrayInit(51,51,11); // Glory be.
    arrayInit(52,52,12); // Oh my jesus.

    arrayInit(53,53,6); // Mystery song......
    arrayInit(54,54,4); // Fourth decade announce..
    arrayInit(55,55,9); // our father
    arrayInit(56,65,10); // hail mary;
    arrayInit(66,66,11); // Glory be
    arrayInit(67,67,12); // oh my jesus.

    arrayInit(68,68,6); // Mystery song..
    arrayInit(69,69,5); // Fifth decade announce..
    arrayInit(70,70,9); // our father.
    arrayInit(71,80,10); // Hail mary
    arrayInit(81,81,11); // Glory be..
    arrayInit(82,82,12); // Oh my jesus.

    arrayInit(83,83,6); // Mystery song..
    arrayInit(84,84,13); // Concl: Hail Holy queen
    arrayInit(85,85,14); // Oh god whose..
    arrayInit(86,86,7); // In the name ..
    }

    private void arrayInit(int start,int end,int songId)
    {
    int i=start;
    do{
    songArray[start]=songId;
    start+=1;
    }while(start<=end);
    }

    public void playerUpdate(Player player, String event, Object data)
    {
    if(event.equals(PlayerListener.END_OF_MEDIA))
    {
    startPlayer(incPlay);//songArray[incPlay]
    incPlay +=1;
    // System.out.println(" Inc Play :"+incPlay);


    }
    }


    protected void keyPressed(int keyCode)
    {
    switch(getGameAction(keyCode))
    {
    case LEFT:
    adjustVolume(-5,false);
    break;
    case RIGHT:
    adjustVolume(+5,false);
    break;
    case FIRE:
    adjustVolume(0,true);
    break;
    }
    }

    public void commandAction(Command c, Displayable d)
    {
    if(c == exit)
    midlet.exitMidlet();
    else if(c ==play)
    {
    try{
    if(rs.getNumRecords() >0)
    playFromRms();
    }catch(Exception e) { System.out.println("Exception."+e);}

    }
    }

    // Adjust the Volume.........//
    private void adjustVolume(int incr, boolean isMute)
    {
    VolumeControl vc= (VolumeControl)currentPlayer.getControl("VolumeControl");
    if(vc!=null)
    {
    volumeSetting = vc.getLevel();
    if(isMute !=true)
    {
    volumeSetting += incr;
    volumeSetting = vc.setLevel(volumeSetting);
    System.out.println("Volume.."+volumeSetting);
    }
    else
    {
    vc.setMute(!vc.isMuted());
    volumeMuted = vc.isMuted();
    }
    }
    }

    }

Similar Threads

  1. Uploading Snapshot byte array to server.
    By krem82 in forum Mobile Java Networking & Messaging & Security
    Replies: 28
    Last Post: 2009-03-13, 20:12
  2. Cannot play the audio from the record store!
    By sweetsweet24 in forum Mobile Java General
    Replies: 3
    Last Post: 2008-10-17, 07:18

Posting Permissions

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