Android: takePicture() not making sound

I am using the takePicture() method from the Camera class. It takes the picture , but it makes no sound! My client wants the default click sound that Android makes.

I have seen other threads and it seems that the problem is in disabling the sound, not the opposite! Why is it that my app is not making the click sound??

Upvotes: 20

Views: 10633

Answers (2)

H0LL0
H0LL0

Reputation: 489

If you want a camera to play the default android sound, you have to give it an empty shutter callback.

ShutterCallback shutterCallback = new ShutterCallback() {
    @Override
    public void onShutter() { 
        /* Empty Callbacks play a sound! */
    }
};

mCamera.takePicture(shutterCallback , null, onPicTaken);

Tested on HTC One SV and Samsung Galaxy XCover 2.

You give the callback an empty shutter callback and then it plays the default sound.

If you call takePicture with a null ShutterCallback the camera won't play any sound.

The answer of Shrikant is working, but might behave unexpectedly as mgr.playSoundEffect(); is not intended to be called with AudioManager.FLAG_PLAY_SOUND as the doc states.

Upvotes: 20

Shrikant Ballal
Shrikant Ballal

Reputation: 7087

I also had this problem. I solved it by using following code: The following is used to call takePicture:

clickButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View view) {
                mCamera.takePicture(shutterCallback, null, onPicTaken);
            }
        });

Now shutteerCallBack:

private final ShutterCallback shutterCallback = new ShutterCallback() {
        public void onShutter() {
            AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            mgr.playSoundEffect(AudioManager.FLAG_PLAY_SOUND);
        }
    };

Now Do whatever after taking picture from camera:

     /**
     * This will be called after taking picture from camera.
     */
    final transient private PictureCallback onPicTaken = new PictureCallback() {
        /**
         * After taking picture, onPictureTaken() will be called where image
         * will be saved.
         * 
         */
        @Override
        public void onPictureTaken(final byte[] data, final Camera camera) {
         }

This will play sound on clicking capture button.

Thank you :)

Upvotes: 53

Related Questions