Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

Hot Searches
struts-config.xml  web.xml  Java date format  read/write text file using javascript  ArrayList  Insert Data into Database Using Hibernat  create text file using java  insert data to text file using java  Visual Basic Date and Time Display the t  Create table and insert data by sql quer 

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

J2ME Audio Record

                         

This example is used to record the audio sound  and play the recorded sound. In this example we are trying to capture the audio sound and encoded into the pcm format, the source code is as follows:

 

 

 

 

 

Player p = Manager.createPlayer("capture://audio?encoding=pcm");

In the whole application we try to show recording and playing of the audio, To play the audio sound we are using the following source code:

Player p2 = Manager.createPlayer(recordedInputStream,"audio/basic");

 

The Application is as follows:

 

RecordMIDlet.java

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

public class RecordMIDlet extends MIDlet{
    public void startApp(){
        Display.getDisplay(this).setCurrent(new RecordForm());
    }
    
    public void pauseApp(){}
    
    public void destroyApp(boolean unconditional){}
}

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{                
                p = Manager.createPlayer("capture://audio?encoding=pcm");
                p.realize();                
                RecordControl rc = (RecordControl)p.getControl("RecordControl");
                ByteArrayOutputStream output = new ByteArrayOutputStream();
                rc.setRecordStream(output);                
                rc.startRecord();
                p.start();
                messageItem.setText("recording...");
                Thread.currentThread().sleep(5000);
                messageItem.setText("done!");
                rc.commit();               
                recordedSoundArray = output.toByteArray();                
                p.close();
            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());
            }
        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());
            }
        }
    }
}

 

Download Source Code

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Tell A Friend
Your Friend Name

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.