×

Discussion Board

Results 1 to 3 of 3

Thread: record sound

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    15

    record sound

    hello everyone,

    does anyone ever try to record sound in nokia phone using J2ME?

    I ever got the example application from J2ME WTK and try it on nokia emulator but it can't record sound. it said that it couldn't create the datasource. what does it mean? help me please, i'm in confuse....


    thanks

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    10

    i ever tried record sound

    i tried in nokia 7610, it doesn't have any error, but i don't know how to play it back (.. here are my codes, hope this help you, and please tell me if you know how to play it back. thank you.

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

    public class RecSnd extends MIDlet implements CommandListener {
    Player p =null;
    Player pr = null;
    RecordControl rc;
    ByteArrayOutputStream output;

    Command cmplay;
    Command cmrec;
    Command cmreplay;
    Command cmshowdata;
    Form fm;

    private void showdata() {
    // fm.deleteAll();
    int len = 0;
    byte [] data = output.toByteArray();
    if (data.length > 30) len = 50;
    else len = data.length;

    for (int i = 0;i<len;i++)
    fm.append(data[i] + " ");
    }
    private void playsond() {
    InputStream is = getClass().getResourceAsStream("/snd.amr");
    try {
    p = Manager.createPlayer(is, "audio/amr");
    p.start();
    }
    catch (MediaException ex) {
    }
    catch (IOException ex) {
    }
    }
    //here reply() procedure to replay recorded sound but nothings hear ( tell me if your's works, thank you.
    private void reply() {
    InputStream is = new ByteArrayInputStream(output.toByteArray());
    try {
    p = Manager.createPlayer(is, "audio/amr");
    p.start();
    }
    catch (MediaException ex) {
    }
    catch (IOException ex) {
    }
    }
    //here recordsnd() procedrue to record sound-----------------------
    private void recordsnd() {
    fm.append("record start");
    try {
    pr = Manager.createPlayer("capture://audio");
    pr.realize();
    // Get the RecordControl, set the record stream,
    // start the Player and record for 5 seconds.
    }
    catch (MediaException ex) {
    }
    catch (IOException ex) {
    }
    rc = (RecordControl)pr.getControl("RecordControl");
    output = new ByteArrayOutputStream();
    rc.setRecordStream(output);
    rc.startRecord();

    try {
    pr.start();
    Thread.currentThread().sleep(5000);
    rc.commit();
    }
    catch (MediaException ex1) {
    }
    catch (InterruptedException ex2) {
    }
    catch (IOException ex3) {
    }
    rc.stopRecord();
    pr.close();
    fm.append("record end");
    }

    public RecSnd() {
    }

    /**
    * destroyApp
    *
    * @param boolean0 boolean
    */
    protected void destroyApp(boolean boolean0) {
    }

    /**
    * startApp
    */
    protected void startApp() {
    cmplay = new Command("Ply",Command.ITEM,1);
    cmrec = new Command("rec",Command.ITEM,1);
    cmreplay = new Command("replay",Command.ITEM,1);
    cmshowdata = new Command("showdata",Command.ITEM,1);

    fm = new Form("record song test");
    fm.addCommand(cmplay);
    fm.addCommand(cmrec);
    fm.addCommand(cmreplay);
    fm.addCommand(cmshowdata);
    fm.setCommandListener(this);
    Display.getDisplay(this).setCurrent(fm);
    }

    /**
    * pauseApp
    */
    protected void pauseApp() {
    }

    /**
    * commandAction
    *
    * @param command Command
    * @param displayable Displayable
    */
    public void commandAction(Command command, Displayable displayable) {
    if (command == cmplay)
    this.playsond();
    if (command == cmrec)
    this.recordsnd();
    if (command == cmreplay)
    this.reply();
    if (command == cmshowdata)
    this.showdata();
    }
    }

    hope this help you.

    www.thepsn.co.nr

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    hi xins,

    thanks for your reply and your example. I have tried your application on emulator. it worked in J2ME WTK default emulator but it didn't work in Nokia Emulator (I used Nokia 6255 emulator). It returned NullPointerException.

    But looked at your code, I couldn't find where the sound that have been recorded stored. Is it stored in a file? If yes, what the file name is? I have tried to create an application like you have built too, and I set to stored the recorded sound to a file. Here is my code :

    private void doMain () {
    try {
    // Create a Player that captures live audio.
    Player p = Manager.createPlayer("capture://audio");
    p.realize();
    // Get the RecordControl, set the record stream,
    // start the Player and record for 5 seconds.
    rc = (RecordControl)p.getControl("RecordControl");
    rc.setRecordLocation("file:test.wav");
    //ByteArrayOutputStream output = new ByteArrayOutputStream();
    //rc.setRecordStream(output);
    rc.startRecord();
    p.start();
    Thread.currentThread().sleep(5000);
    rc.commit();
    rc.stopRecord();
    p.close();

    } catch (Exception e) {
    System.out.println (e.toString());
    }
    }

    it worked with no error at Nokia 6600. But when I tried to find "test.wav" file, it coudn't be found.

    Is it possible to record sound ??

Posting Permissions

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