audio录音在JAVA层的调用

本文详细介绍了在Android 7.0版本中如何在JAVA层调用录音功能。从SoundRecorder.java开始,逐步讲解了录音的启动、停止、播放等操作,涉及MediaRecorder的设置及关键参数的配置,以及MediaRecorder在JNI层的实现过程,包括MediaRecorderClient.cpp和StagefrightRecorder.cpp中的关键函数。整个流程展示了从用户界面操作到底层录音功能的实际执行过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用android 7.0版本的录音应用为例进行跟进下。

 

STEP 1:

\packages\apps\SoundRecorder\src\com\android\soundrecorder\SoundRecorder.java

 

   public void onCreate(Bundle icycle) {

       super.onCreate(icycle);

 

       Intent i = getIntent();

       if (i != null) {

           String s = i.getType();

           if (AUDIO_AMR.equals(s) || AUDIO_3GPP.equals(s) || AUDIO_ANY.equals(s)

                    || ANY_ANY.equals(s)) {

                mRequestedType = s;

           } else if (s != null) {

                // we only support amr and 3gppformats right now

                setResult(RESULT_CANCELED);

                finish();

                return;

           }

           

           final String EXTRA_MAX_BYTES

                =android.provider.MediaStore.Audio.Media.EXTRA_MAX_BYTES;

           mMaxFileSize = i.getLongExtra(EXTRA_MAX_BYTES, -1);

       }

       

       if (AUDIO_ANY.equals(mRequestedType) || ANY_ANY.equals(mRequestedType)){

           mRequestedType = AUDIO_3GPP;

       }

       

       setContentView(R.layout.main);

 

       mRecorder= new Recorder();

       mRecorder.setOnStateChangedListener(this);

       mRemainingTimeCalculator = new RemainingTimeCalculator();

 

        PowerManager pm

           = (PowerManager) getSystemService(Context.POWER_SERVICE);

       mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,

                                   "SoundRecorder");

 

       initResourceRefs();

       

       setResult(RESULT_CANCELED);

       registerExternalStorageListener();

       if (icycle != null) {

           Bundle recorderState = icycle.getBundle(RECORDER_STATE_KEY);

           if (recorderState != null) {

                mRecorder.restoreState(recorderState);

                mSampleInterrupted =recorderState.getBoolean(SAMPLE_INTERRUPTED_KEY, false);

                mMaxFileSize =recorderState.getLong(MAX_FILE_SIZE_KEY, -1);

           }

       }

       

       updateUi();

}

 

 

    /*

     * Handle the buttons.

     */

    public void onClick(View button) {

        if (!button.isEnabled())

            return;

 

        switch (button.getId()) {

            case R.id.recordButton:

               mRemainingTimeCalculator.reset();

                if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

                    mSampleInterrupted = true;

                    mErrorUiMessage =getResources().getString(R.string.insert_sd_card);

                    updateUi();

                } else if(!mRemainingTimeCalculator.diskSpaceAvailable()) {

                    mSampleInterrupted = true;

                    mErrorUiMessage =getResources().getString(R.string.storage_is_full);

                    updateUi();

                } else {

                    stopAudioPlayback();

 

                    if(AUDIO_AMR.equals(mRequestedType)) {

                       mRemainingTimeCalculator.setBitRate(BITRATE_AMR);

                        mRecorder.startRecording(MediaRecorder.OutputFormat.AMR_NB,".amr", this);

                    } else if(AUDIO_3GPP.equals(mRequestedType)) {

                       mRemainingTimeCalculator.setBitRate(BITRATE_3GPP);

                        mRecorder.startRecording(MediaRecorder.OutputFormat.THREE_GPP,".3gpp",

                                this);

                    } else {

                        throw newIllegalArgumentException("Invalid output file type requested");

                    }

                    

                    if (mMaxFileSize != -1) {

                       mRemainingTimeCalculator.setFileSizeLimit(

                               mRecorder.sampleFile(), mMaxFileSize);

                    }

                }

                break;

            case R.id.playButton:

                mRecorder.startPlayback();

                break;

            case R.id.stopButton:

                mRecorder.stop();

                break;

            case R.id.acceptButton:

                mRecorder.stop();

                saveSample();

                finish();

                break;

            case R.id.discardButton:

                mRecorder.delete();

           &

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值