Results 1 to 7 of 7
Like Tree3Likes
  • 1 Post By DarrylBurke
  • 1 Post By gimbal2
  • 1 Post By gimbal2

Thread: how to convert bytes(or String) to 3gp file??

  1. #1
    M0TRIX is offline Member
    Join Date
    Dec 2013
    Posts
    28
    Rep Power
    0

    Default how to convert bytes(or String) to 3gp file??

    hello.

    i'm coding an App. which records the voice(voice recorder) , converts it to array of bytes then Encodes it to base64 .

    so i decode the base64 file to normal String or bytes.

    now i need to convert normal String(or bytes) variable to a voice file.

    summary :

    1_record a voice

    2_convert to arrays of byte

    3_encoded to base64

    4_decode the arrays of byte

    5_convert it to a playable sound file

    here is my code :

    PHP Code:
    protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          
          text = (TextView) findViewById(R.id.text1);
          // store it to sd card
          outputFile = Environment.getExternalStorageDirectory().
        		  getAbsolutePath() + "/recordfile.3gpp";
    
          myRecorder = new MediaRecorder();
          myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
          myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
          myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
          myRecorder.setOutputFile(outputFile);
          
          startBtn = (Button)findViewById(R.id.start);
          startBtn.setOnClickListener(new OnClickListener() {
    		
    		@Override
    		public void onClick(View v) {
    			// TODO Auto-generated method stub
    			start(v);
    		}
          });
          
          stopBtn = (Button)findViewById(R.id.stop);
          stopBtn.setOnClickListener(new OnClickListener() {
      		
      		@Override
      		public void onClick(View v) {
      			// TODO Auto-generated method stub
      			try {
    				stop(v);
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
      		}
          });
          
          playBtn = (Button)findViewById(R.id.play);
          playBtn.setOnClickListener(new OnClickListener() {
      		
      		@Override
      		public void onClick(View v) {
      			// TODO Auto-generated method stub
    				play(v);	
      		}
          });
          
          stopPlayBtn = (Button)findViewById(R.id.stopPlay);
          stopPlayBtn.setOnClickListener(new OnClickListener() {
      		
      		@Override
      		public void onClick(View v) {
      			// TODO Auto-generated method stub
      			stopPlay(v);
      		}
          });
       }
    
       public void start(View view){
    	   try {
              myRecorder.prepare();
              myRecorder.start();
           } catch (IllegalStateException e) {
              // start:it is called before prepare()
        	  // prepare: it is called after start() or before setOutputFormat() 
              e.printStackTrace();
           } catch (IOException e) {
               // prepare() fails
               e.printStackTrace();
            }
    	   
           text.setText("Recording Point: Recording");
           startBtn.setEnabled(false);
           stopBtn.setEnabled(true);
           
           Toast.makeText(getApplicationContext(), "Start recording...", 
        		   Toast.LENGTH_SHORT).show();
       }
       String encoded;
       File file ;
       public void stop(View view) throws IOException{
    	   
    	   try {
    	      myRecorder.stop();
    	      myRecorder.release();
    	      myRecorder  = null;
    	     // file = new File(Environment.getExternalStorageDirectory() + "/recordfile.3gpp");
    	   	
    	   	
    	   	FileInputStream in=new FileInputStream(file=new File(outputFile+"/recordfile.3gp"));
    	   	byte fileContent[] = new byte[(int)file.length()];
    	   	
    	   	in.read(fileContent,0,fileContent.length);
    	   	
    	     encoded = Base64.encodeToString(fileContent,0);                                       
    	 //  Utilities.log("~~~~~~~~ Encoded: ", encoded);
    	      
    	      
    	      stopBtn.setEnabled(false);
    	      playBtn.setEnabled(true);
    	      text.setText("Recording Point: Stop recording");
    	      
    	      Toast.makeText(getApplicationContext(), "Stop recording...",
    	    		  Toast.LENGTH_SHORT).show();
    	   } catch (IllegalStateException e) {
    			//  it is called before start()
    			e.printStackTrace();
    	   } catch (RuntimeException e) {
    			// no valid audio/video data has been received
    			e.printStackTrace();
    	   }
       }
      
       public void play(View view) {
    	   try{
    		   myPlayer = new MediaPlayer();
    		   
    		   FileOutputStream out=new FileOutputStream(outputFile+"/decoded.3gp");
    		   byte[] decoded = Base64.decode(encoded, 0);
    		   
    		   out.write(decoded);
    		   out.close();
    		   myPlayer.setDataSource(outputFile+"/decoded.3gp");
    		   myPlayer.prepare();
    		   myPlayer.start();
    		   
    		   playBtn.setEnabled(false);
    		   stopPlayBtn.setEnabled(true);
    		   text.setText("Recording Point: Playing");
    		   
    		   Toast.makeText(getApplicationContext(), "Start play the recording...", 
    				   Toast.LENGTH_SHORT).show();
    	   } catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
       }
    the output: records the voice but not playing. and it makes a file recordfile.3gp on SDcard which it play from sdcard corectly....

    the output should be :

    records the voice and playes it. make two files :recordfile.3gp and decoded.3gp

    dosen't make the secode file??!!!
    Last edited by M0TRIX; 04-13-2015 at 01:03 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: how to convert bytes(or String) to 3gp file??

    Did you have a question?

    db
    M0TRIX likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    M0TRIX is offline Member
    Join Date
    Dec 2013
    Posts
    28
    Rep Power
    0

    Default Re: how to convert bytes(or String) to 3gp file??

    make mistake. i edited the first post.

    sorry if my english is bad

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: how to convert bytes(or String) to 3gp file??

    You say "the file is not being created". Well then I assume the file creation code is not being executed. Your only error handling is to print out exception stacktraces to the console; since this is a Swing application I wonder if you can even see any errors that your application is producing.
    Last edited by gimbal2; 04-13-2015 at 01:58 PM.
    M0TRIX likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: how to convert bytes(or String) to 3gp file??

    Quote Originally Posted by gimbal2 View Post
    since this is a Swing application
    Um, no, it's Android.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: how to convert bytes(or String) to 3gp file??

    Scary, I was so convinced it was Swing I didn't even see the simple truth in the code.
    M0TRIX likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    M0TRIX is offline Member
    Join Date
    Dec 2013
    Posts
    28
    Rep Power
    0

    Default Re: how to convert bytes(or String) to 3gp file??

    hello.

    tnx for helping guys. but my problem soulved ...!

    do you know why my App dosen't work on some of phones??

    will you test it on your phone for me??

    i've test it on android 4.4, 4.2.1 and 4.2.2

    just didn't work on 4.4.2.1.

    so tell me your version of OS and test it in this way :

    1_install the App
    2_record your voice
    3_play it if it is working corectly...
    4_look at your memory card it must be 2 file : recordfile.3gpp and decoded.3gpp

    tnx a lot...

Similar Threads

  1. Replies: 4
    Last Post: 01-15-2013, 10:23 AM
  2. Getting all the bytes in a file
    By Proshitness in forum New To Java
    Replies: 5
    Last Post: 06-15-2012, 11:17 PM
  3. Removing thr last two bytes from string
    By KarlNorway in forum New To Java
    Replies: 3
    Last Post: 03-13-2012, 09:29 AM
  4. Replies: 3
    Last Post: 02-10-2012, 06:26 PM
  5. Replies: 1
    Last Post: 04-10-2010, 04:37 PM

Posting Permissions

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