How to EnableShutterSound while taking picture in android xamarin

482 views Asked by At

I have a custom camera implementation which I would like to have have my own sound when the picture is taken how to enable shutter sound in hardware camera, I need to only play my camera sound and not the default one

 private async void TakePhotoButtonTapped(object sender, EventArgs e)
        {

            Android.Hardware.Camera.CameraInfo info = new Android.Hardware.Camera.CameraInfo();
            // mCameraFacing is CameraID.
            Android.Hardware.Camera.GetCameraInfo(mCameraFacing, info);
            if (info.CanDisableShutterSound)
                camera.EnableShutterSound(false);
            else
                camera.EnableShutterSound(true);
            Bitmap image = textureView.Bitmap;
            using (var imageStream = new MemoryStream())
            {
                await image.CompressAsync(Bitmap.CompressFormat.Jpeg, 100, imageStream);
                image.Recycle();
                imageBytes = imageStream.ToArray();
                AddImages(imageBytes);
                Toast.MakeText(Android.App.Application.Context, _languageCache.Translate("Photo Captured"), ToastLength.Short).Show();
            }
            await Task.Delay(300);

        }
1

There are 1 answers

0
Wendy Zang - MSFT On

You could use the code below of the sound settings and put it into the code which you used to take photo.

I use the system ShutterClick sound for reference. You could use your own as well.

  private void SoundSettings()
    {
        MediaActionSound sound = new MediaActionSound();
        AudioManager meng = (AudioManager)Context.GetSystemService(Context.AudioService);
        int volume = meng.GetStreamVolume(Android.Media.Stream.Notification);
        if (volume != 0)
            sound.Play(MediaActionSoundType.ShutterClick);
    }