Git Product home page Git Product logo

androidaudiocamerapermission's People

Contributors

zhaozepeng avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

androidaudiocamerapermission's Issues

封装了一下这个类

` public static boolean checkCameraPermission(Context context) {

    int hasCameraPermission = PackageManager.PERMISSION_GRANTED;
    if (Build.VERSION.SDK_INT >= 23) {
        hasCameraPermission = context.checkSelfPermission(Manifest.permission.CAMERA);
    }else{
        Camera mCamera = null;

        try {
            mCamera = Camera.open(0);
            mCamera.setDisplayOrientation(90);
        } catch (Exception e) {
            Log.e(TAG, Log.getStackTraceString(e));
            hasCameraPermission = PackageManager.PERMISSION_DENIED;
        }

        if (hasCameraPermission == PackageManager.PERMISSION_GRANTED) {
            mCamera.release();
            mCamera = null;
        }
    }

    return hasCameraPermission == PackageManager.PERMISSION_GRANTED;
}

public static boolean checkAudioPermission(final Context context) {

    int hasAudioPermission = PackageManager.PERMISSION_GRANTED;

    if (Build.VERSION.SDK_INT >= 23) {
        hasAudioPermission = context.checkSelfPermission(Manifest.permission.RECORD_AUDIO);
    }else {
        bufferSizeInBytes = 0;
        bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz,
                channelConfig, audioFormat);
        AudioRecord audioRecord = null;

        try {
            audioRecord = new AudioRecord(audioSource, sampleRateInHz,
                    channelConfig, audioFormat, bufferSizeInBytes);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        //开始录制音频
        try {
            //防止某些手机崩溃,例如联想
            audioRecord.startRecording();
        } catch (IllegalStateException e) {
            e.printStackTrace();
            Log.e(TAG, Log.getStackTraceString(e));
            return false;
        }

        do {
            /**
             * 根据开始录音判断是否有录音权限
             */
            if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING
                    && audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED) {
                Log.e(TAG, "audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING : " + audioRecord.getRecordingState());
                //return false;
                hasAudioPermission = PackageManager.PERMISSION_DENIED;
                break;
            }

            if (audioRecord.getRecordingState() == AudioRecord.RECORDSTATE_STOPPED) {
                //如果短时间内频繁检测,会造成audioRecord还未销毁完成,此时检测会返回RECORDSTATE_STOPPED状态,再去read,会读到0的size,所以此时默认权限通过
                //return true;
                hasAudioPermission = PackageManager.PERMISSION_GRANTED;
                break;
            }

            byte[] bytes = new byte[1024];
            int readSize = audioRecord.read(bytes, 0, 1024);
            if (readSize == AudioRecord.ERROR_INVALID_OPERATION || readSize <= 0) {
                Log.e(TAG, "readSize illegal : " + readSize);
                //return false;
                hasAudioPermission = PackageManager.PERMISSION_DENIED;
            }
        }while(false);

        audioRecord.stop();
        audioRecord.release();
        audioRecord = null;
        //return true;
    }

    return hasAudioPermission == PackageManager.PERMISSION_GRANTED;
}

`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.