×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    May 2008
    Posts
    68

    problem in audio playback

    hi all ,

    i m getting following problem in my code for playing recorded audio

    javax.microedition.media.MediaException: Failed to realize Player: Malformed wave media: expected 'RIFF'
    at com.sun.mmedia.WavPlayer.doRealize(+341)
    at com.sun.mmedia.BasicPlayer.realize(+84)
    at audio.RecordAudioCanvas.viewAudio(RecordAudioCanvas.java:240)
    at audio.RecordAudioCanvas$ViewRecord.run(RecordAudioCanvas.java:319)


    here is the code of my application:
    Code:
    package audio;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import javax.microedition.io.Connector;
    import javax.microedition.io.file.FileConnection;
    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Font;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.media.Manager;
    import javax.microedition.media.MediaException;
    import javax.microedition.media.Player;
    import javax.microedition.media.control.RateControl;
    import javax.microedition.media.control.RecordControl;
    import javax.microedition.media.control.VolumeControl;
    
    public class RecordAudioCanvas extends Canvas implements CommandListener,Runnable{
        FileConnection fc;
        RecordMidlet rmidlet;
        private Player player,recordPlayer;
        private int SPACE = 5;
        private RecordControl recorder;
        private VolumeControl volume;
        private int videoHeight;
        private int videoWidth ;
        private int xorigin = 2* SPACE;
        private int yorigin = 15* SPACE;
        private int xnew = 0;
        private int ynew = 2* SPACE;
        private final static int RECORDINGTIME = 9000000;   
        private int color1 = 0xffffff;
        private int color2 = 0xffffff;
        private static int vlevel = 50;
        private long time;
        private String mtime = "";
        private String mvolume ="50";
        private Thread repaintthread;
        private boolean timeflag;
        private Command cmdRecord; //
        private Command cmdExit;
        private Command cmdPlay;   //
            
       public RecordAudioCanvas(RecordMidlet midlet) {
            try {
                fc = (FileConnection) Connector.open("file:///root1/audio/my.wav");
                if( !fc.exists())
                    fc.create();
                
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            videoHeight  = getHeight();
            videoWidth   = getWidth();
            rmidlet = midlet;
            cmdRecord = new Command("Record", Command.SCREEN, 0);
            cmdExit   = new Command("Exit", Command.EXIT, 0);
            cmdPlay = new Command("Play", Command.SCREEN, 1);
           
            addCommand(cmdPlay);
            addCommand(cmdRecord);
            addCommand(cmdExit);
            setCommandListener(this);
            repaintthread = new Thread((Runnable) this);
            InputStream is = getClass().getResourceAsStream("/music.wav");
            try {
                player = Manager.createPlayer(is, "audio/x-wav");
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (MediaException ex) {
                ex.printStackTrace();
            }
            new CameraThread().start();
        }
       
        protected void paint(Graphics g) {
            g.setColor(0x005945);
            g.fillRect(0, 0, videoWidth, videoHeight);
            g.setColor(0xffffff);
            g.drawLine(SPACE, SPACE, SPACE, videoHeight - SPACE );
            g.drawLine(SPACE, SPACE, videoWidth - SPACE, SPACE );
            g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE));
            g.setColor(0xffffff);
            g.drawString("Recording Audio", 2*SPACE, 2*SPACE, Graphics.TOP | Graphics.LEFT);
            g.setFont(Font.getDefaultFont());
            g.drawString(mtime, 2*SPACE, 10*SPACE, Graphics.TOP | Graphics.LEFT);
            g.setColor(0xffffff);
            g.drawRect(xorigin, yorigin, getWidth()/2, ynew);
            g.fillRect(xorigin, yorigin, xnew, ynew);
            g.drawString(mvolume, 7* xorigin, 20* SPACE, 2* xorigin);
            g.setColor(color1);
            g.drawString("<", xorigin, 20* SPACE, 2* xorigin );
            g.setColor(color2);
            g.drawString(">", 4* xorigin, 20* SPACE, 2* xorigin );
          }
    
        public  void startAudio(){
            try {
                player.setLoopCount(1);
                player.realize();
                player.prefetch(); 
                RateControl rc = (RateControl)player.getControl("RateControl");
                rc.getRate();
                System.out.println("rate"+rc.getRate());
                volume = (VolumeControl) player.getControl("VolumeControl");
                volume.setLevel(50);
                player.start();
              } catch (MediaException ex) {
                ex.printStackTrace();
            }
        }
            
        public  void recordAudio(){
            try{
                recorder = (RecordControl) player.getControl("RecordControl");
            }catch(IllegalStateException e){
            }
            try {
                recorder.setRecordLocation("file:///root1/audio/my.wav");
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (MediaException ex) {
                ex.printStackTrace();
            }
            recorder.startRecord();
            time =player.getDuration();    
            try {
                Thread.currentThread().sleep(player.getDuration());
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            try {
                recorder.stopRecord();
                recorder.commit();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
         }
        
        
        public synchronized void viewAudio(){
            if(player!= null)
                try {
                    player.stop();
                    player.close();
            } catch (MediaException ex) {
                ex.printStackTrace();
            }
                try {
                    fc = (FileConnection) Connector.open("file:///root1/audio/my.wav");
                    if(fc.exists()){
                        InputStream fis = fc.openInputStream();
                        recordPlayer = Manager.createPlayer(fis, "audio/x-wav");
                        recordPlayer.realize();
                        recordPlayer.prefetch();
                        recordPlayer.setMediaTime(time);
                        volume = (VolumeControl)recordPlayer.getControl("VolumeControl");
                        volume.setLevel(vlevel);
    + recordPlayer.getDuration());
                        recordPlayer.start();
    time =" + recordPlayer.getMediaTime());
                   }else
                        repaintthread.start(); 
                } catch (IOException ex) {
                ex.printStackTrace();
            } catch (MediaException ex) {
                ex.printStackTrace();
            }
        }
     
        class CameraThread extends Thread{
            public void run(){
                startAudio();
            }
        }
        
        class RecordThread extends Thread{
            public void run(){
                recordAudio();
            }
        }
        
        class ViewRecord extends Thread{
            public void run(){
                viewAudio();
            }
        }
        
        private String recordingTime(long val) {
            String ret = "";
            int mval = (int)(val )/ 1000;
            int sec = mval / 1000;
            int min = sec / 60;
    
            if (min >= 10) {
                ret = ret + min + ":";
            } else if (min > 0) {
                ret = "0" + min + ":";
            } else {
                ret = "00:";
            }
    
            if (sec >= 10) {
                ret = ret + sec + ".";
            } else if (sec > 0) {
                ret = ret + "0" + sec + ".";
            } else {
                ret = ret + "00.";
            }
            mval = (mval % 1000) / 100;
            if(sec!=0)
                ret = ret + mval;
            return (ret);
        }
    
        public void run() {
            int pixsize = 0;
            int count = 0;
            
            while( !timeflag ){
                mtime = recordingTime(recordPlayer.getMediaTime());
                count++;
                if(count % 10 == 0){
                   pixsize = (((getWidth() / 2 ) / (int)(recordPlayer.getDuration() / 1000000)));   
                   xnew += pixsize;
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                if(recordPlayer.getMediaTime() >= recordPlayer.getDuration()- 750  ){
                    timeflag = true;
                }
                repaint();
                serviceRepaints();
             }
        }
        
        public void commandAction(Command cmd, Displayable arg1) {
            if(cmd == cmdRecord){
                try {
                    Thread.currentThread().sleep(1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                new RecordThread().start();
            }
            if(cmd == cmdExit){
                rmidlet.destroyApp(true);
            }
        
            if(cmd == cmdPlay){
                new ViewRecord().start();
            }
        }
            
    }
    [/CODE]

  2. #2
    Registered User
    Join Date
    Sep 2007
    Location
    Bangalore
    Posts
    868

    Re: problem in audio playback


  3. #3
    Regular Contributor
    Join Date
    May 2008
    Posts
    68

    Re: problem in audio playback

    hi,
    i get the problem.
    when i m recording the .wav file the thread is for recording is exicuting after some time hence , all the header information of .wav file is not recorded , and while playing back the recoded data it is not getting any header.

    so now information " to add header data to my recorded .wav file"
    will be appriciated.

  4. #4
    Regular Contributor
    Join Date
    May 2008
    Posts
    68

    Re: problem in audio playback

    hi,

    all it is because i can't record .wav file format more than approximately 126k bytes on my device.

    is there any way to compress file size. in order to record it completely.

Similar Threads

  1. N70 Audio recording problem
    By codsw2 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2008-05-22, 01:06
  2. Video with audio recording delay problem on N90
    By vasya0 in forum Symbian Media (Closed)
    Replies: 2
    Last Post: 2007-12-03, 16:44
  3. Problem regarding Nokia 6630 Audio....
    By skumar_rao in forum Mobile Java General
    Replies: 4
    Last Post: 2007-07-09, 10:00
  4. Replies: 7
    Last Post: 2007-02-07, 18:12
  5. problem with playback of audio
    By bps4484 in forum Symbian
    Replies: 0
    Last Post: 2006-12-19, 20:43

Posting Permissions

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