How to play native camera sound on Android

后端 未结 6 1877
星月不相逢
星月不相逢 2020-12-29 21:29

I would like to play native camera shutter sound clip on camera preview capture. I\'m referring to the sound clip played when takePicture() is called.
How

相关标签:
6条回答
  • 2020-12-29 21:36

    This resource explains how to play audio files: https://developer.android.com/guide/topics/media/index.html

    You'll probably have to provide your own shutter sound effect.

    0 讨论(0)
  • 2020-12-29 21:40
            AudioManager meng = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
            int volume = meng.getStreamVolume( AudioManager.STREAM_NOTIFICATION);
            if(volume != 0)
                sound.play(MediaActionSound.SHUTTER_CLICK);
    
    0 讨论(0)
  • 2020-12-29 21:41

    You may want to use SoundPool

    SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0);
    int shutterSound = soundPool.load(this, R.raw.camera_click, 0);
    

    and then to play the sound

    soundPool.play(shutterSound, 1f, 1f, 0, 0, 1);
    

    Check out http://developer.android.com/reference/android/media/SoundPool.html to understand the parameters.

    You will need a media file called camera_click.ogg in your project at res/raw. You should be able to use the Android default sound which can be obtained from the Android open source project in the following location ( frameworks/base/data/sounds/effects/camera_click.ogg ) if your project is licensed under the Apache license. If your project isn't licensed under the Apache license I have no idea if you can use it or not. I am not a lawyer.

    0 讨论(0)
  • 2020-12-29 21:48

    This snippet will help you to play sound only when the ringer mode is in normal mode and not in silent or vibrate.

    private var audioManager: AudioManager? = null
    private var mediaPlayer: MediaPlayer? = null
    
    private fun initAudio() {
        Log.v(LOG_TAG, "initAudio")
    
        audioManager ?: let {
            audioManager = context!!.getSystemService(Context.AUDIO_SERVICE) as AudioManager?
        }
    
        mediaPlayer = try {
            MediaPlayer().apply {
                if (Build.VERSION.SDK_INT >= 21) {
                    val audioAttributes = AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_MEDIA)
                        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                        .build()
                    setAudioAttributes(audioAttributes)
                } else {
                    setAudioStreamType(AudioManager.STREAM_MUSIC)
                }
                if (Build.VERSION.SDK_INT <= 28) {
                    setDataSource(context!!, Uri.parse("file:///system/media/audio/ui/camera_click.ogg"))
                } else {
                    setDataSource(context!!, Uri.parse("file:///system/product/media/audio/ui/camera_click.ogg"))
                }
                prepare()
            }
        } catch (e: Exception) {
            Log.e(LOG_TAG, "initAudio", e)
            null
        }
    }
    
    private fun playClickSound() {
    
        if (audioManager?.ringerMode == AudioManager.RINGER_MODE_NORMAL) {
            mediaPlayer?.start()
        }
    }
    
    0 讨论(0)
  • 2020-12-29 21:55

    You can use the MediaActionSound class (available from API 16). For example:

    MediaActionSound sound = new MediaActionSound();
    sound.play(MediaActionSound.SHUTTER_CLICK);
    
    0 讨论(0)
  • 2020-12-29 21:56

    If the system file is there, you can use it like this:

    public void shootSound()
    {
        AudioManager meng = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
        int volume = meng.getStreamVolume( AudioManager.STREAM_NOTIFICATION);
    
        if (volume != 0)
        {
            if (_shootMP == null)
                _shootMP = MediaPlayer.create(getContext(), Uri.parse("file:///system/media/audio/ui/camera_click.ogg"));
            if (_shootMP != null)
                _shootMP.start();
        }
    }
    
    0 讨论(0)
提交回复
热议问题