"Don't tell me how hard you work. Tell me how you get done."
- James J. Ling

Record and Play Audio in S60

November 18, 2006

Today, I and Marlon started our thesis programming together. We were able to now record and play audio. Thanks to our joined power! hehe:) We were able to fix the code by creating a separate thread for recording…Today, we also learned about the different audio formats…

package record;

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

/**
* This form shows how the cellphone’s microphone can be used to record audio and how the recorded audio can be played on the cellphone. The original author of this code is Alexander De Luca. There are some problems regarding possible deadlock when recording audio. As a solution, we opted to create another thread just handling the recording of audio.
* modified on November 18, 2006
*/

public class RecordForm extends Form implements CommandListener{
    private StringItem messageItem;
    private StringItem errorItem;
    private final Command recordCommand, playCommand;
    private Player p;
    private byte[] recordedSoundArray = null;
   
    public RecordForm() {
        super(”Record Audio”);
       
        messageItem = new StringItem(”Record”, “Click record to start recording.”);
        this.append(messageItem);
        errorItem = new StringItem(”", “”);
        this.append(errorItem);
       
        recordCommand = new Command(”Record”, Command.SCREEN, 1);
        this.addCommand(recordCommand);
        playCommand = new Command(”Play”, Command.SCREEN, 2);
        this.addCommand(playCommand);
       
        StringBuffer inhalt = new StringBuffer();
       
        this.setCommandListener(this);
    }
   
    public void commandAction(Command comm, Displayable disp) {
        if(comm==recordCommand) {
            try {
                Record record1= new Record();                     //thread
                record1.start();
            } catch(Exception e){
                   System.err.println(e);
            }
                 
        } else if(comm == playCommand) {
            try {
                ByteArrayInputStream recordedInputStream = new ByteArrayInputStream(recordedSoundArray);
                Player p2 = Manager.createPlayer(recordedInputStream,”audio/basic”);
                p2.prefetch();
                p2.start();
            }  catch (IOException ioe) {
                errorItem.setLabel(”Error”);
                errorItem.setText(ioe.toString());
            } catch (MediaException me) {
                errorItem.setLabel(”Error”);
                errorItem.setText(me.toString());
            }
        }
    }
   
    public class Record extends Thread
    {
       private boolean quit=false;
   
       public void run ()
       {
           while (!quit)
           {
               try
               {
                   // create the Player object using amr encoding
                p=Manager.createPlayer(”capture://audio?encoding=pcm”);
                p.realize();
                // get the RecordControl over this Player
                RecordControl rc = (RecordControl)p.getControl(”RecordControl”);
               
                // create an OutputStream which the RecordControl will use
                // to write write the recorded data.
                ByteArrayOutputStream output = new ByteArrayOutputStream();
                rc.setRecordStream(output);
                // start the recording
                rc.startRecord();
                p.start();
                messageItem.setText(”recording…”);
               
                Thread.currentThread().sleep(5000); //record for 5 seconds
                messageItem.setText(”done!”);
                rc.commit();
                // save the recordedData in a byte array
                recordedSoundArray = output.toByteArray();
                // close the player
                p.close();
                quit=true;
               } catch (IOException ioe) {
                errorItem.setLabel(”Error”);
                errorItem.setText(ioe.toString());
            } catch (MediaException me) {
                errorItem.setLabel(”Error”);
                errorItem.setText(me.toString());
            } catch (InterruptedException ie) {
                errorItem.setLabel(”Error”);
                errorItem.setText(ie.toString());
            }
               }
           }
       }
    }

Posted by phengpheng at 7:36 pm | permalink

Previous Comments

Hai Sir,
I am ramachandaran,from salem,tamilnadu,INDIA.I am a B.E student and now i am doing my B.E project on database management of students using visual basic 6.0. as front end and MS Access for storing database.

Now, I want to add VOICE RECORDING(audio profile) of individual members in that project.I don’t know how to do it.Can you please send me the idea and source code so that i can effectively enhance my project.

Thank you,

Posted by Ramachandran.P at August 21, 2008, 6:59 pm

Hai Sir,
I am a B.E student and now i am doing my B.E project on database management of students using visual basic 6.0. as front end and MS Access for storing database.

Now, I want to add VOICE RECORDING(audio profile) of individual members in that project.I don’t know how to do it.Can you please send me the idea and source code so that i can effectively enhance my project.

Thank you,

Posted by Ramachandran.P at August 21, 2008, 7:00 pm

hi… i am amey.
i am trying to execute your code on Nokia 6233 mobile but i got error like Javax.microedition.midlet.MediaException:invalid encoding… like this so could you please tell me that whether this code will execute on any mobile which have support for JSR 82 and JSR 135 after generating the jar file. cause i having a problem in yhat…. please tell me

Posted by Amey at October 23, 2008, 1:34 pm

All comments are moderated. Your comments will not appear here unless approved by the blog owner. Thank you.

Add a comment








     

November 2006
M T W T F S S
« Oct   Mar »
 12345
6789101112
13141516171819
20212223242526
27282930  

About Me

A Computer Science graduate who is a fanatic in assembly, java, and j2me programming. Developed BlueVoice system (Direct Voice Communication in Mobile Phones), this system allows mobile users to experience FREE VOICE CALLS

Subscribe

Technorati
Bloglines

Tagboard

phengpheng:

Haven’t managed my blog for a long time already because I’m so busy now… Sorry to cause you guyz trouble…

Jun Mark:

hoW tO creaTE a PrOGram thAT woULD oUTpuT ascii table???

Jun Mark:

pLZ anSwer mY qUEstions

cutieGurl:

how to convert binary to hexadecimal, decimal and octal?????????please. answer me??????????

mary ann:

kuya, ung binary to decimal converter nyo. mali man? ung sa mga compare mo. ung may 0,9,A,F etc… bad character lumalabaspls rep asap po. tnx

carol:

how to convert decimal to binary to octal and hexadecimal

Galwin:

been here!

galwin:

nice site. hope you could visit mine, too.

pathy:

FRIENDS FOREVER!! Miss you na!!!

ridvan:

aus, i seldom see lady programmers. hehe. keep it up. and oh yeah, i guess i saw you during the MICT Quiz Show held in cdo. :)
keep up the codes!

phengpheng:

I miss updating this blog… Sana may time and pc ako to learn new stuffs aside from my work…

support:

Congratulations, you’ve just completed the installation of this shoutbox.

pheng:

uy! you’re here again!

Jay:

Woooohhhh orange…sweat… nakakasilaw! hehe :)

jay:

tagboard flood hehe sensya… had nothing fun to do. :)

pheng:

jaaaaayyyyy….:) ketal man?:)

niel:

no spamming please :p hehehe joke lang jay.

Jay:

Uyyyyyy!!!! Grraaaaaaaiiiinnnneeee
:)
Sssshhhh, el sikret! :)
Niiiiiiiiiiiiieeeeeel
Sup dude!!!!
Paaaaaaaaaaaaaathy
Ketal!?

niel:

ei pheng! :D slick-elegant layout! love it!

yue:

phengpheng!

Leave a message ▼