Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Play camera shutter sound programmatically

I want to play camera shutter sound programmatically. I'm not using ShutterCallback which automatically plays that sound, so I need to do it in some other way. Anyone knows the solution?

like image 663
mbz Avatar asked Oct 25 '12 13:10

mbz


2 Answers

MediaActionSound from API 16.

AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    switch( audio.getRingerMode() ){
        case AudioManager.RINGER_MODE_NORMAL:
            MediaActionSound sound = new MediaActionSound();
            sound.play(MediaActionSound.SHUTTER_CLICK);
        break;
        case AudioManager.RINGER_MODE_SILENT:
        break;
        case AudioManager.RINGER_MODE_VIBRATE:
        break;
    }

Respect vibrate/silent mode in Android.

like image 169
t0m Avatar answered Oct 22 '22 11:10

t0m


his resource explains how to play audio files

http://www.vogella.com/articles/AndroidMedia/article.html

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

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();
    }
}
like image 42
Jay Avatar answered Oct 22 '22 12:10

Jay