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 6185117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:39:23+00:00 2026-05-24T01:39:23+00:00

I am recording audio using AudioRecord class.I want to record audio into a particular

  • 0

I am recording audio using AudioRecord class.I want to record audio into a particular file in my asset folder or resource folder.I think there is no problem in recording.but while reading buffer it is showing some problem(it is throwing NullPointerException).Can anyone suggest what may be the problem?

  • 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-05-24T01:39:24+00:00Added an answer on May 24, 2026 at 1:39 am

    You can not save file inside Asset folder. Assets folder is read only instead of it you will have to save it in the internal or external storage of your device

    Below there is a core to record the media file.

    package com.example.media.record;
    
        import java.io.File;
        import java.io.IOException;
    
        import android.app.Activity;
        import android.media.MediaPlayer;
        import android.media.MediaPlayer.OnCompletionListener;
        import android.media.MediaPlayer.OnErrorListener;
        import android.media.MediaRecorder;
        import android.os.Bundle;
        import android.os.Environment;
        import android.os.Handler;
        import android.os.Handler.Callback;
        import android.os.Message;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.ProgressBar;
        import android.widget.Toast;
    
        public class MediaRecorderActivity extends Activity implements OnClickListener {
            Button btnPlay;
            Button btnRecord;
            ProgressBar progress;
            MediaPlayer mPlayer;
            MediaRecorder mRecorder;
            String mFileName;
            boolean mStartRecording = true;
            boolean mStartPlaying = true;
            Thread mThreadProgress;
            int duration = 1;
            private void onRecord(boolean start) {
                if(start) {
                    startRecording();
                }else {
                    stopRecording();
                }
            }
    
            private void onPlay(boolean start) {
                if(start) {
                    startPlaying();
                }else {
                    stopPlaying();
                }
            }
    
            private void startRecording() {
                mRecorder = new MediaRecorder();
                mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mRecorder.setOutputFile(mFileName);
                mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                mRecorder.setOnErrorListener(errorListenerForRecorder);
    
                try {
                    mRecorder.prepare();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    mRecorder.start();
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Error :: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }
    
            }
    
    
            private void stopRecording() {
                if(mRecorder != null) {
                    mRecorder.stop();
                    mRecorder.release();
                    mRecorder = null;
                }
            }
    
            private void startPlaying() {
                mPlayer = new MediaPlayer();
                try {
                    mPlayer.setDataSource(mFileName);
                    mPlayer.setOnCompletionListener(completionListener);
                    mPlayer.setOnErrorListener(errorListenerForPlayer);
                    mPlayer.prepare();
                    mPlayer.start();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
            private void stopPlaying() {
                if(mPlayer != null) {
                    mPlayer.stop();
                    mPlayer.release();
                    mPlayer = null;
                }
            }
    
            OnCompletionListener completionListener = new OnCompletionListener() {
    
                @Override
                public void onCompletion(MediaPlayer mp) {
                    btnRecord.setEnabled(true);
                    btnPlay.setText("Start playing");
                    mStartPlaying = !mStartPlaying;
                }
            };
    
            OnErrorListener errorListenerForPlayer = new OnErrorListener() {
    
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    Toast.makeText(getApplicationContext(), "Error during playing file", 3000).show();
                    return false;
                }
            };
    
            android.media.MediaRecorder.OnErrorListener errorListenerForRecorder = new android.media.MediaRecorder.OnErrorListener() {
    
                @Override
                public void onError(MediaRecorder mr, int what, int extra) {
                    Toast.makeText(getApplicationContext(), "Error during recoding file", 3000).show();
    
                }
            };
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                btnPlay = (Button)findViewById(R.id.btnPlay);
                btnRecord = (Button)findViewById(R.id.btnRecord);
                progress = (ProgressBar)findViewById(R.id.progressRecorder);
                mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
                mFileName += "/audiorecordtest.3gp";
                File file = new File(mFileName);
                if(!file.exists()) btnPlay.setEnabled(false); 
                btnPlay.setOnClickListener(this);
                btnRecord.setOnClickListener(this);
            }
    
            @Override
            protected void onPause() {
                super.onPause();
                if(mRecorder != null) {
                    mRecorder.stop();
                }
                if(mPlayer != null) {
                    mPlayer.pause();
                }
            }
    
    
            @Override
            protected void onResume() {
                super.onResume();
                if(mRecorder != null) {
                    mRecorder.start();
                }
                if(mPlayer != null) {
                    mPlayer.start();
                }
            }
    
            @Override
            protected void onStop() {
                super.onStop();
                if(mRecorder != null) {
                    mRecorder.stop();
                }
                if(mPlayer != null) {
                    mPlayer.stop();
                }
            }
    
            @Override
            protected void onDestroy() {
                super.onDestroy();
                if(mRecorder != null) {
                    mRecorder.release();
                    mRecorder = null;
                }
                if(mPlayer != null) {
                    mPlayer.release();
                    mPlayer = null;
                }
            }
    
            @Override
            public void onClick(View v) {
    
                if(v == btnPlay) {
    
                    onPlay(mStartPlaying);
                    if(mStartPlaying) {
                        duration = mPlayer.getDuration();
                        mThreadProgress = new ThreadProgress();
                        mThreadProgress.start();
                        ((Button)v).setText("Stop Playing");
                        btnRecord.setEnabled(false);
    
                    }
                    else {
                        ((Button)v).setText("Start Playing");
                        btnRecord.setEnabled(true);
                        if(mThreadProgress != null && !mThreadProgress.isAlive()) mThreadProgress.stop();
    
                        //  t.interrupt();
                    }
                    mStartPlaying = !mStartPlaying;
    
                } else if(v == btnRecord) {
                    onRecord(mStartRecording);
                    if(mStartRecording) {
                        mThreadProgress = new ThreadProgress();
                        mThreadProgress.start();
                        ((Button)v).setText("Stop Recording");
                        btnPlay.setEnabled(false);
                        //  t.start();
                    }
                    else {
                        ((Button)v).setText("Start Recording");
                        btnPlay.setEnabled(true);
                        //  t.interrupt();
                        if(mThreadProgress != null && !mThreadProgress.isAlive()) mThreadProgress.stop();
                    }
                    mStartRecording = !mStartRecording;
                }
            }
    
    
            Handler handler = new Handler(new Callback() {
    
                @Override
                public boolean handleMessage(final Message msg) {
                    if(msg.what == 0) {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                progress.setProgress(msg.arg1);
                            }
                        });
    
                    }
                    return false;
                }
            });
    
            public class ThreadProgress extends Thread implements Runnable {
    
                public int i = 0;
                @Override
                public void run() {
                    while((!this.isInterrupted() && mPlayer != null && mPlayer.isPlaying()) || (!this.isInterrupted() && mRecorder != null)) {
                        try {
                            if(duration == 1) i+=1;
                            else i += 100000 /duration;
                            Message message = new Message();
                            message.what = 0;
                            message.arg1 = i;
                            handler.sendMessage(message);
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }       
                    }
                }
    
            }
        }
    

    This is the example you can record audio as well as play audio

    You can store recorded file in following places
    1) File directory of your app
    2) External directory(SD card)
    3) Network
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written code for recording audio file using AudioRecord and while writing file
I am trying to play audio after immediate recording of audio using audiorecord class.
I m using a microphone class to record audio from user. I want to
How does one record audio using iOS? Not the input recording from the microphone,
I have been using the code in http://opensebj.blogspot.com/2009/04/naudio-tutorial-5-recording-audio.html to record audio. Basically this code:
I am recording audio using AVAudioRecorder ,and now i want to get exact time
I am developing simple producer-consumer example. One thread records audio samples using AudioRecord class
I' m having some issues using AudioRecord class. I want to store recorded data
I am working on audio recording. I can store the file using file.getAbosolutPath() method
I am using a thread which records the audio using AudioRecord class and placed

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.