tags:

views:

447

answers:

3

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 could I that? Can someone walk me through the steps?

Thanks!

A: 

This resource explains how to play audio files

http://developer.android.com/intl/de/guide/topics/media/index.html

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

Jim Blackler
+1  A: 

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.

bwr
From the docs: "priority field: the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility."
greg7gkb
Apache license is very permissive; you can use its resources in any project.On the other hand, it is possible to look for the sound already available on the device.
Alex Cohn
A: 

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();
    }
}
Alex Cohn