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?
Asked
Active
Viewed 1.1k times
14
-
1Duplicate of http://stackoverflow.com/q/2364892/1321873 – Rajesh Oct 25 '12 at 13:51
-
Possible duplicate of [How to play native camera sound on Android](https://stackoverflow.com/questions/2364892/how-to-play-native-camera-sound-on-android) – Hrishikesh Kadam May 14 '19 at 18:50
3 Answers
26
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.

t0m
- 3,004
- 31
- 53
-
2MediaActionSound is built to already respect the ringer mode if it is on vibrate or silenced so this switch case should be able to be removed. – kjanderson2 Feb 24 '20 at 21:29
9
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();
}
}

Jay
- 1,474
- 9
- 13
0
You can use this function:
private fun shuttleVolume() {
val sound = MediaActionSound()
sound.play(MediaActionSound.SHUTTER_CLICK)
}
Sample code:
imageCaptureButton.setOnClickListener {
shuttleVolume()
takePhoto()
}
Call it when you needed, for example before taking a photo!

Mohammad Reza
- 61
- 5