MediaRecorder starts with a 1 sec delay. How do I get rid of the silence?

天涯浪子 提交于 2019-12-19 04:55:15

问题


I am trying to use MediaRecorder in a Service to record sounds. But it creates a 1 sec delay (silence) at the start. how to i get rid of this? I tried using RehearsalAudioRecorder still has no luck. If anyone has fixed this problem before please advice.

Start

Intent serviceIntent = new Intent();
            serviceIntent.setAction("com.soundrecoder.RecorderService");
            serviceIntent.putExtra("audioFile", path);
            serviceIntent.putExtra("state", true);
            startService(serviceIntent);

Stop

Intent serviceIntent = new Intent();
            serviceIntent.setAction("com.soundrecoder.RecorderService");
            serviceIntent.putExtra("state", false);
            startService(serviceIntent);

RecorderService.java file

public class RecorderService extends Service
{
    private static final String TAG = null;
    private static MediaRecorder mRecorder;

    public void onCreate() {};

    public void onStart(Intent intent, int startId)
    {
        boolean isStart = intent.getBooleanExtra("state", false);

        if (isStart) {
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mRecorder.setOutputFile(intent.getStringExtra("audioFile"));

            try {
                mRecorder.prepare();
            } catch (IllegalStateException e) {
                Log.e(TAG,e.getMessage());
            } catch (IOException e) {
                Log.e(TAG,e.getMessage());
            }
            try {
                mRecorder.start();
            }
            catch (IllegalStateException e) {
                Log.e(TAG, e.getMessage());
            }
        }

        else if (!isStart) {
            mRecorder.stop();
            mRecorder.reset();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

回答1:


I found a solution with the RehearsalAudioRecorder

public void onStart(Intent intent, int startId) {
        boolean isStart = intent.getBooleanExtra("state", false);

        if (isStart) {

            acquireWakeLock();

            int i = 0;

            do {
                if (mRecorder != null)
                    mRecorder.release();

                 mRecorder = new RehearsalAudioRecorder(false, 0, 0, 0, 0);

            } while ((++i < 44100)
                    & !(mRecorder.getState() == RehearsalAudioRecorder.State.INITIALIZING));

            mRecorder.setOutputFile(intent.getStringExtra("audioFile"));
            mRecorder.prepare();
            mRecorder.start();

        } else if (!isStart) {

            if(mRecorder != null) {
                mRecorder.stop();
                mRecorder.release();
                mRecorder = null;
            }

            releaseWakeLock();

        }
    }


来源:https://stackoverflow.com/questions/7643338/mediarecorder-starts-with-a-1-sec-delay-how-do-i-get-rid-of-the-silence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!