本文介绍了MediaRecorder始于一个1秒的延迟。我该如何摆脱沉默的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用MediaRecorder的服务来录制声音。但是,它创建了一个1秒的延迟(沉默)在开始。如何我摆脱这种?我试图用<一个href="http://rehearsalassist.svn.sourceforge.net/viewvc/rehearsalassist/android/releases/RehearsalAssistant_0_8_2/src/urbanstew/RehearsalAssistant/RehearsalAudioRecorder.java?revision=90&view=markup"相对=nofollow> RehearsalAudioRecorder 仍然没有运气。如果任何人有固定前请咨询这个问题。

开始

 意图serviceIntent =新意图();
            serviceIntent.setAction(com.soundre coder.RecorderService);
            serviceIntent.putExtra(的AudioFile,路径);
            serviceIntent.putExtra(状态,真正的);
            startService(serviceIntent);
 

停止

 意图serviceIntent =新意图();
            serviceIntent.setAction(com.soundre coder.RecorderService);
            serviceIntent.putExtra(国家,假);
            startService(serviceIntent);
 

RecorderService.java文件

 公共类RecorderService扩展服务
{
    私有静态最后字符串变量= NULL;
    私有静态MediaRecorder mRecorder;

    公共无效的onCreate(){};

    公共无效ONSTART(意向意图,诠释startId)
    {
        布尔ISSTART = intent.getBooleanExtra(国家,假);

        如果(ISSTART){
            mRecorder =新MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setAudioEn codeR(MediaRecorder.AudioEn coder.AMR_NB);
            mRecorder.setOutputFile(intent.getStringExtra(AUDIOFILE));

            尝试 {
                mRecorder prepare()。
            }赶上(IllegalStateException异常E){
                Log.e(TAG,e.getMessage());
            }赶上(IOException异常E){
                Log.e(TAG,e.getMessage());
            }
            尝试 {
                mRecorder.start();
            }
            赶上(IllegalStateException异常E){
                Log.e(TAG,e.getMessage());
            }
        }

        否则,如果(!ISSTART){
            mRecorder.stop();
            mRecorder.reset();
        }
    }

    @覆盖
    公众的IBinder onBind(意向意图){
        返回null;
    }
}
 

解决方案

我发现与RehearsalAudioRecorder的解决方案

 公共无效ONSTART(意向意图,诠释startId){
        布尔ISSTART = intent.getBooleanExtra(国家,假);

        如果(ISSTART){

            acquireWakeLock();

            INT I = 0;

            做 {
                如果(mRecorder!= NULL)
                    mRecorder.release();

                 mRecorder =新RehearsalAudioRecorder(假的,0,0,0,0);

            }而((+ I&LT; 44100)
                    &放大器; !(mRecorder.getState()== RehearsalAudioRecorder.State.INITIALIZING));

            mRecorder.setOutputFile(intent.getStringExtra(AUDIOFILE));
            mRecorder prepare()。
            mRecorder.start();

        }否则,如果(!ISSTART){

            如果(mRecorder!= NULL){
                mRecorder.stop();
                mRecorder.release();
                mRecorder = NULL;
            }

            releaseWakeLock();

        }
    }
 

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;
    }
}
解决方案

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();

        }
    }

这篇关于MediaRecorder始于一个1秒的延迟。我该如何摆脱沉默的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 17:03