×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Regular Contributor
    Join Date
    Oct 2008
    Posts
    80

    Recording Audio file and saving it to file system

    Hi,
    I am trying to record audio by s40 j2me app and like to store to file system. That file I should be able to access later and play by my app.
    Here is the code I am trying but get exception when I use the following line

    dos = con.openDataOutputStream();

    Code:
    
        
        private void saveRecordedAudio(boolean aMorningAudioType)    		
        		{
        	 		String fileName  = (aMorningAudioType ==  true) ? "ownMorningAudioType" : "ownotherAudioType";
        	 		
        	 		if(iAudioFileType == null)
        	 		{
        	 			return;
        	 		}
        	 		fileName = fileName +"."+ iAudioFileType;
     
        		
        		    String fullPath = null;
        		    fullPath = System.getProperty("fileconn.dir.private");    		    
        		    if( fullPath.endsWith("/") )
        		        fullPath = fullPath + fileName;
        		    else
        		        fullPath = fullPath + "/" + fileName;
        		    
        		    System.out.println(" >< AudioManager::saveRecordedAudio()   fullPath = "  + fullPath);  
        		    FileConnection con = null;
        		    DataOutputStream dos = null;
        		    try
        		    {
        		    	con = (FileConnection)Connector.open(fullPath,Connector.READ_WRITE);
        		    	if( con.exists() )
        		    	{
        		    		 System.out.println(" >< AudioManager::saveRecordedAudio()   file already there");  
        		    		con.delete();
        		    	}
        		    }
        		    catch (IOException io)
        		    {
        		    System.out.println(">< AudioManager::saveRecordedAudio()  IOException: # 1 "+io.getMessage());
        		    }
        		    catch (SecurityException se)
        		    {
        		    System.out.println(">< AudioManager::saveRecordedAudio() Security exception: # 1" + se.getMessage());
        		    } 
        		    
        		    System.out.println(">< AudioManager::saveRecordedAudio() going to use openDataOutputStream" );
        		    try {
        		    	dos = con.openDataOutputStream();
        		    	System.out.println(">< AudioManager::saveRecordedAudio() ?????This  is throwing  exception ???????????????" );
        		    	dos.write(iRecordedSoundArray);
        		    	
        		    	dos.flush();
        		    	dos.close();
        		    	con.close();
        		    }
           		    catch (IOException io)
        		    {
        		    System.out.println(">< AudioManager::saveRecordedAudio()  openDataOutputStream IOException: # 2"+io.getMessage());
        		    }
        		    catch (SecurityException se)
        		    {
        		    System.out.println(">< AudioManager::saveRecordedAudio() Security exception: # 1" + se.getMessage());
        		    }  
        		    finally {
        		    	con = null;	
        		    	dos = null;
        		    	iUI.SaveOwnAudioFileType(iAudioFileType);    		    	
        		    	System.out.println(" << AudioManager::saveRecordedAudio()   aMorningAudioType = "  + aMorningAudioType);
        		    }
        		}  
        
        
        // support amr, mp3  and wav type
        // given  seconds will be recorded
        public void RecordAudio(boolean aMorningAudioType, int aTimeSeconds){
        	
        	String format = "audio";
        	iAudioState = 1;
        	
        	 String[] s = Manager.getSupportedContentTypes(null);
             for (int i = 0; i<s.length; i++ ) {
                 if ( s[i].indexOf(format, 0) != -1 ) {
                	 System.out.println(" <> AudioManager::RecordAudio()   s[i] = " + s[i]  ); 
                	 if(s[i].indexOf("amr") >= 5)
                	 {
                		 iAudioFileType = "amr";
                		 break;
                	 }
                	 if(s[i].indexOf("mp3") >= 5)
                	 {
                		 iAudioFileType = "mp3";
                		 break;
                	 }   
                	 if(s[i].indexOf("wav") >= 5)
                	 {
                		 iAudioFileType = "wav";
                		 break;
                	 }              	 
                 }
             }
             
        	
        	try {
        	 player = Manager.createPlayer("capture://audio");
        	  player.realize();  
        	  iRecordControl = (RecordControl)player.getControl("RecordControl");
        	  ByteArrayOutputStream output = new ByteArrayOutputStream();
        	  iRecordControl.setRecordStream(output);  
        	  iRecordControl.startRecord();
        	  player.start();
        	
              long start = System.currentTimeMillis();
              while ((System.currentTimeMillis() - start)/1000 < aTimeSeconds) {             
                  Thread.sleep(1000);
              }
                 
              player.stop();
              iRecordControl.stopRecord();          
        	  iRecordControl.commit(); 
        	  iRecordedSoundArray = output.toByteArray();  
        	  player.close();
        	  saveRecordedAudio(aMorningAudioType);
        	  
        	  } catch (IOException ioe) {
        		  System.out.println(" >< AudioManager::RecordAudio()  ioe = " + ioe.getMessage() );
    
        	  } catch (MediaException me) {
        		  System.out.println(" >< AudioManager::RecordAudio()  me = " + me.getMessage() );
    
        	  } catch (InterruptedException ie) {
        		  System.out.println(" >< AudioManager::RecordAudio()  ie = " + ie.getMessage() );
    
        	  }    	
        	System.out.println(" << AudioManager::RecordAudio() " );
        }
    Thanks for help

  2. #2
    Regular Contributor
    Join Date
    Oct 2008
    Posts
    80

    Re: Recording Audio file and saving it to file system

    Looks my code is clumsy, can anybody point me or provide me code that I can use for recording sound by microphone?

  3. #3
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,683

    Re: Recording Audio file and saving it to file system

    First and second you may want to write something into a file, without the audio, and record audio without the file. Order is optional.
    Then you could mix the two as the third step.
    For recording I would check the links from http://developer.nokia.com/Community...-in-mp3-format, but generally it seems to be a simple API to me.
    For FileConnection, http://developer.nokia.com/Community...M_API_(JSR-75) and http://developer.nokia.com/Community...ata_on_Java_ME offers some related articles.

  4. #4
    Regular Contributor
    Join Date
    Oct 2008
    Posts
    80

    Re: Recording Audio file and saving it to file system

    Thanks, I checked most of these before posting, reading and going through will take really some time. I was looking for snippet.
    Any way I have gone little bit further, now I need
    how to save bytearray of audio data to specific format supported by device (for example to mp3 or wav or amr format).
    I see if I save byte array to wav file it goes to this path, is this valid?
    file:///C:/DirectLoaded/myapp_private/ownfirstaudio.wav

  5. #5
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,683

    Re: Recording Audio file and saving it to file system

    I simply do not know if those c: and DirectLoaded are correct or not, so I would try some examples, like http://developer.nokia.com/Community...ems_in_Java_ME and http://developer.nokia.com/Community...ers_in_Java_ME, and also check the various properties, like fileconn.dir.private, what their actual content is.

  6. #6
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    678

    Re: Recording Audio file and saving it to file system

    You can only record in AMR format in Series40.
    Your test of String[] s = Manager.getSupportedContentTypes(null); is not correct since you are testing the supported content types for ALL locators (i.e. both playing and recording)
    you should use String[] s = Manager.getSupportedContentTypes("capture");

  7. #7
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,087

    Re: Recording Audio from microphone in J2me and saving it to file system

    use this code:

    player = Manager.createPlayer("capture://audio");
    player.realize();
    RecordControl rc = (RecordControl) player.getControl("RecordControl");
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    rc.setRecordStream(output);
    rc.startRecord();
    player.start();
    message.setText("Recording...");
    Thread.sleep(5000);
    message.setText("Recording Done!");
    rc.commit();
    recordedAudioArray = output.toByteArray();
    player.close();
    fc = (FileConnection) Connector.open(path+"test.wav");
    dos = fc.openDataOutputStream();
    dos.write(recordedAudioArray);
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

Similar Threads

  1. Replies: 3
    Last Post: 2011-03-06, 10:28
  2. How to create a hidden audio recording file??
    By chandran.biju007 in forum Symbian Media (Closed)
    Replies: 6
    Last Post: 2010-05-17, 19:41
  3. Saving a file at file system
    By atilacorreiacunha in forum [Archived] Flash Lite on Nokia Devices
    Replies: 2
    Last Post: 2009-05-15, 20:59
  4. Recording Audio in file using CMdaAudioRecordUtility in S60 V3
    By gpalvia in forum Symbian Media (Closed)
    Replies: 6
    Last Post: 2006-08-15, 10:40
  5. Problem in audio stream recording into a file
    By ritam.banik in forum Symbian Media (Closed)
    Replies: 1
    Last Post: 2006-06-01, 06:41

Posting Permissions

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