Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8122807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:57:14+00:00 2026-06-06T05:57:14+00:00

I want to record voice as long as holding the record button and save

  • 0

I want to record voice as long as holding the record button and save that voice into the raw folder in my project. I used the code below. Altough there appears no errors, i couldn’t get any output. What can be the problem ? Do you have any suggestions? Thanks,

public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub


    Runnable mAction = new Runnable() {
        public void run() {
            System.out.println("Performing action...");

            int frequency=11025;
            int channelConfiguration=AudioFormat.CHANNEL_CONFIGURATION_MONO;
            int audioEncoding= AudioFormat.ENCODING_PCM_16BIT;
            File file=new File(Environment.getExternalStorageDirectory(),"raw.pcm");

            try{
                file.createNewFile();
            }catch(IOException e){}

            try{
                OutputStream os=new FileOutputStream(file);
                BufferedOutputStream bos=new BufferedOutputStream(os);
                DataOutputStream dos=new DataOutputStream(bos);

                int bufferSize=AudioRecord.getMinBufferSize(frequency, channelConfiguration,
                        audioEncoding);

                short[] buffer=new short[bufferSize];
                audioRecorder=new AudioRecord(MediaRecorder.AudioSource.MIC,
                        frequency, channelConfiguration, audioEncoding, bufferSize);

                audioRecorder.startRecording();

                isRecording=true;

                while(isRecording){

                    int                                    bufferReadResult=audioRecorder.read(buffer, 0,bufferSize);

                    for(int i=0;i<bufferReadResult;i++){

                        dos.writeShort(buffer[i]);

                    }

                }

                audioRecorder.stop();
                dos.close();

            }catch(Throwable t){}

        }
    };

    switch(event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        if (myHandler != null) return true;
        myHandler = new Handler();
        myHandler.postDelayed(mAction, 500);
        break;
    case MotionEvent.ACTION_UP:

        if (myHandler == null) return true;


        isRecording=false;

        myHandler.removeCallbacks(mAction);
        myHandler = null;
        break;
    }

    return false;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:57:15+00:00Added an answer on June 6, 2026 at 5:57 am

    here is your answer…

    import java.io.File;
    import java.io.IOException;
    import android.app.Activity;
    import android.media.MediaRecorder;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.Button;
    
    public class AudioOnTouchActivity extends Activity {
        Button b1;
        private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
        private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
        private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
        private MediaRecorder recorder = null;
        private int currentFormat = 0;
        private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4,             MediaRecorder.OutputFormat.THREE_GPP };
        private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP }; 
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            b1=(Button)findViewById(R.id.button1);
            b1.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    switch(event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        AppLog.logString("Start Recording");
                        startRecording();
                        return true;
                    case MotionEvent.ACTION_UP:
                        AppLog.logString("stop Recording");
                        stopRecording();
                        break;
                    }
                    return false;
                }
            });
        }
    
        private String getFilename(){
            String filepath = Environment.getExternalStorageDirectory().getPath();
            File file = new File(filepath,AUDIO_RECORDER_FOLDER);
    
            if(!file.exists()){
                file.mkdirs();
            }
    
            return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
        }
    
        private void startRecording(){
            recorder = new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(output_formats[currentFormat]);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setOutputFile(getFilename());
            recorder.setOnErrorListener(errorListener);
            recorder.setOnInfoListener(infoListener);
    
            try {
                recorder.prepare();
                recorder.start();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
               @Override
                public void onError(MediaRecorder mr, int what, int extra) {
                    AppLog.logString("Error: " + what + ", " + extra);
            }
        };
    
        private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
            @Override
            public void onInfo(MediaRecorder mr, int what, int extra) {
                    AppLog.logString("Warning: " + what + ", " + extra);
            }
        };
    
        private void stopRecording(){
            if(null != recorder){
                recorder.stop();
                recorder.reset();
                recorder.release();
    
                recorder = null;
            }
        }
    }
    

    AppLog.java file is

    import android.util.Log;
    
    public class AppLog {
    private static final String APP_TAG = "AudioRecorder";
    
    public static int logString(String message){
         return Log.i(APP_TAG,message);
    }
    

    the xml is

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" 
            android:layout_gravity="center"
            android:layout_marginTop="10dp"/>
    
    </LinearLayout>
    

    Add these permission to your manifest file

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
            <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to record human voice on my Android phone. I noticed that Android
I want to record own voice or any through mic(In any way) into a
I want to record voice and after that get .flac format file, can I
I want to record all the user that browse into my web site, in
I want to record voice and sync that up with animation on the iphone
I want to record the handshake messages between server and client into the file
I want to record voice online and I guess I need to use FMS
i want to record someones voice and then from information i get about his/her
I want to record the Voice on my android mobile and I have no
I want to make a voice recorder app, which can record, even when the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.