錄音機(SoundRecorder)接收多種廣播的操作

    private void receiveBroadcast(Context context,Intent intent){
        String action=intent.getAction();
        String command=intent.getStringExtra(COMMAN);
        if(Intent.ACTION_MEDIA_EJECT.equals(action) || Intent.ACTION_MEDIA_UNMOUNTABLE.equals(action)){
            //M:when sd card ejected is the current operation path,call onError
            if(isCurrentAccessStorage(intent.getData())){
                if((STATE_RECORDING==mCurrentState) || (STATE_PAUSE_RECORDING==mCurrentState)){
                    mOnErrorListener.onError(ErrorHandle.ERROR_SD_UNMOUNTED_ON_RECORD);
                }else{
                    mOnErrorListener.onError(ErrorHandle.ERROR_SD_UNMOUNTED_WHEN_IDLE);
                }
                reset();
            }
        }else if(Intent.ACTION_MEDIA_MOUNTED.equals(action)){
            //tell activity to remove SD_UNMOUNTED dialog
            if(null!=mOnEventListener){
                mOnEventListener.onEvent(EVENT_STORAGE_MOUNTED);
            }
        }else if(Intent.ACTION_CONFIGURATION_CHANGED.equals(action)){
            if(STATE_IDLE!=mCurrentState){
                //when change language,update the language of notification
                showNotification(getApplicationContext());
                return;
            }
        }else if(SOUND_POWER_DOWN_MSG.equals(action) && CMDPAUSE.equals(command)){
            //M:camera begin to record when soundRecorder is background record,
            //it will send SOUND_POWER_DOWN_MSG,we receive and stop record
            //The process is same as audio focus loss
            if((STATE_RECORDING==mCurrentState) || (STATE_PAUSE_RECORDING==mCurrentState)){
                stopRecord();
            }else if(STATE_PLAYING==mCurrentState){
                stopPlay();
            }
            if(isCurrentFileWaitToSave()){
                saveRecordAsync();
            }
        }else if(Intent.ACTION_SHUTDOWN.equals(action) || ACTION_SHOTDOWN_IPO.equals(action)){
            //save the recording parameters
            storeRecordParamsSettings();
            //添加以下內容可以保證在重啓或者關機的時候,正在錄製的內容能夠自動保存
            if((STATE_RECORDING==mCurrentState) || (STATE_PAUSE_RECORDING==mCurrentState)){
                stopRecord();
            }else if(STATE_PLAYING==mCurrentState){
                stopPlay();
            }
            if(isCurrentFileWaitToSave()){
                saveRecordAsync();
            }
            //添加以下內容可以保證在重啓或者關機的時候,正在錄製的內容能夠自動保存
        }
    }

    private void rigisterBroadcastReceiver(){
        if(null==mStorageBroadcastReceiver){
            mStorageBroadcastReceiver=new BroadcastReceiver(){
                @Override
                public void onReceive(Context context,Intent intent){
                    receiveBroadcast(context,intent);
                }
            };
            IntentFilter iFilter=new IntentFilter();
            iFilter.addAction(Intent.ACTION_MEDIA_EJECT);
            iFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
            iFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
            iFilter.addDataScheme("file");
            registerReceiver(mStorageBroastReceiver,iFilter);

        }

        if(null==mOtherBroastReceiver){
            mOtherBroastReceiver=new BroadcastReceiver(){
                @Override
                public void onReceive(Context context, Intent intent) {
                    receiveBroadcast(context,intent);
                }
            };
            IntentFilter iFilter=new IntentFilter();
            iFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
            iFilter.addAction(SOUND_POWER_DOWN_MSG);
            iFilter.addAction(Intent.ACTION_SHUTDOWN);
            iFilter.addAction(ACTION_SHUTDOWN_IPO);

            registerReceiver(mOtherBroastReceiver,iFilter);
        }
    }

    private void unregisterBroadcastReceiver(){
        if(null!=mStorageBroastReceiver){
            unregisterReceiver(mStorageBroastReceiver);
        }
        if(null!=mOtherBroastReceiver){
            unregisterReceiver(mOtherBroastReceiver);
        }
    }
    
    /**
     * M; check if the storage Uri is current operation path.
     * @param storageUri
     *              same as file:///storage/sdcard0
     * @return current operation path return true
     */
    private boolean isCurrentAccessStorage(Uri storageUri){
        if((null==mCurrentFilePath)|| (null==storageUri)
                || !storageUri.getScheme().endsWith("file")){
            return false;
        }
        String storagePath=storageUri.getPath();
        String currentOperationPath=mCurrentFilePath.substring(0,storagePath.length());
        return currentOperationPath.equals(storagePath);
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章