none
How to set device Volume to vibrate?

    Question

  • Hi,

    I need to set the device Volume as on or off or vibrate programatically. I am currently using C#, Compact framework 2.0.

    Can any body please guide me or provide me the code.


    Thanks in Advance


    Regards,
    Malleswar

    Tuesday, February 24, 2009 3:07 PM

Answers

  • Hi,

    I could finally solve my problem. Here is the C# code to change the volume mode to vibrate or off or on.


    using System;
    using System.Runtime.InteropServices;

    public class ChangeVolumeMode
    {
        private enum SoundEvent
        {
            All = 0,
            RingLine1,
            RingLine2,
            KnownCallerLine1,
            RoamingLine1,
            RingVoip
        }

        private enum SoundType
        {
            On = 0,
            File = 1,
            Vibrate = 2,
            None = 3
        }

        private struct SNDFILEINFO
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szPathNameNative;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szDisplayNameNative;

            public SoundType SstType;
        }

        [DllImport("aygshell.dll", SetLastError = true)]
        private static extern uint SndSetSound(SoundEvent seSoundEvent, ref SNDFILEINFO pSoundFileInfo, bool fSuppressUI);

        [DllImport("aygshell.dll", SetLastError = true)]
        private static extern uint SndGetSound(SoundEvent seSoundEvent, ref SNDFILEINFO pSoundFileInfo);

        public int SetRingerVibrate()
        {
            SNDFILEINFO sfi = new SNDFILEINFO();
            sfi.SstType = SoundType.Vibrate;
            uint ret = SndSetSound(SoundEvent.All, ref sfi, true);
            if (ret != 0)
            {
                return 1;
            }
            return 0;
        }

        public int SetRingerOff()
        {
            SNDFILEINFO sfi = new SNDFILEINFO();
            sfi.SstType = SoundType.None;
            uint ret = SndSetSound(SoundEvent.All, ref sfi, true);
            if (ret != 0)
            {
                return 1;
            }
            return 0;
        }

        public int SetRingerOn()
        {
            SNDFILEINFO sfi = new SNDFILEINFO();
            sfi.SstType = SoundType.On;
            uint ret = SndSetSound(SoundEvent.All, ref sfi, true);
            if (ret != 0)
            {
                return 1;
            }
            return 0;
        }

        private SNDFILEINFO mOldSoundFileInfo = new SNDFILEINFO();
        public int SaveSound()
        {
            uint ret = SndGetSound(SoundEvent.All, ref mOldSoundFileInfo);
            if (ret != 0)
            {
                return 1;
            }
            return 0;
        }

        public int RestoreSound()
        {
            uint ret = SndSetSound(SoundEvent.All, ref mOldSoundFileInfo, true);
            if (ret != 0)
            {
                return 1;
            }
            return 0;
        }
    }


    Its working perfectly. Please let me know, if you find any problem with this code.


    Regards,
    Malleswar


    Saturday, February 28, 2009 11:11 AM

All replies

  • Hi,

    The following forum posting might help you :-

    http://forum.xda-developers.com/showthread.php?t=264497

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    Tuesday, February 24, 2009 3:49 PM
  • Take a look at this thread

    http://social.msdn.microsoft.com/Forums/en-US/netfxcompact/thread/eaabbdac-29f7-44c7-bd52-4f8221bc2c17

    Regards,
    Navin Narayan

    Tuesday, February 24, 2009 4:41 PM
  • Thanks for your reply.

    Paul, I am trying it in C#, so please provide if you hve P/Invoke code or C# code directly.

    If any body else have references, please provide me.

    Thanks in Advance

    Regards,
    Malleswar
    Wednesday, February 25, 2009 4:29 AM
  • C# Code is available in this thread(here) as well .

    Regards,
    Navin Narayan
    Wednesday, February 25, 2009 4:47 AM
  • Hi Narayan,

    Thanks for the reply. This code looks to generate the vibration if I am not mistaken.

    But my requirement is to set the device volume on or off or to set to vibrate. So this code may not work for me.

    Please provide me if you have any other references.

    Thanks in Advance


    Regards,
    Malleswar

    Wednesday, February 25, 2009 4:57 AM
  • Have you looked at chaging registry values from the last post in the link shared by Paul.

    HKCU\ControlPanel\Sounds\RingTone0:Sound

    *none* = Off (Silent)
    *vibrate* = Vibrate
    Fantasy .wmd = On (with the specified ringtone)



    Regards,
    Navin Narayan
    Wednesday, February 25, 2009 5:33 AM
  • Its C++ code. I am in confusion to use C++ in C# code. I used P/Invoke for many features, but never used C++ with Compact framework.

    Please provide me if you have any sample related to this.

    Regards,
    Malleswar
    Wednesday, February 25, 2009 6:29 AM
  • Hi Malleswar,

    Try to edit the registry

    HKCU\ControlPanel\Sounds\RingTone0:Sound

    *none* = Off (Silent)
    *vibrate* = Vibrate
    Fantasy .wmd = On (with the specified ringtone)

    If you need to know how registry editing can be done look at PInvoke egs for registry in http://msdn.microsoft.com/en-us/library/aa446550.aspx 


    Regards,
    Navin Narayan
    Wednesday, February 25, 2009 7:08 AM
  • Hi,

     I am able to change the registry values by using the following code. Could not find any difference in System volume settings. According to the following code registry values are set to the vibration if I am not wrong . but device is still in volume mode.


    private
    void button1_Click(object sender, EventArgs e)

    {

    Object o,o1;

    Registry.SetValue(@"HKEY_CURRENT_USER\ControlPanel\Sounds\RingTone0", "Sound", (object)"*vibrate*");

    o = Registry.GetValue(@"HKEY_CURRENT_USER\ControlPanel\Sounds\RingTone0", "Sound", (object)"Eswar");

    MessageBox.Show(o.ToString());

    Registry.SetValue(@"HKEY_CURRENT_USER\ControlPanel\Notifications\ShellOverrides", "Mode", (object)"1");

    o1 = Registry.GetValue(@"HKEY_CURRENT_USER\ControlPanel\Notifications\ShellOverrides", "Mode", (object)"12");

    MessageBox.Show(o1.ToString());

    AudioUpdateFromRegistry();

    }

    [DllImport("coredll")]

    public static extern void AudioUpdateFromRegistry();


    Please guide me if anyone worked on the related stuff earlier.

    Thanks in advance

    Regards,
    Malleswar

    Wednesday, February 25, 2009 12:54 PM
  • Hi,

    I could finally solve my problem. Here is the C# code to change the volume mode to vibrate or off or on.


    using System;
    using System.Runtime.InteropServices;

    public class ChangeVolumeMode
    {
        private enum SoundEvent
        {
            All = 0,
            RingLine1,
            RingLine2,
            KnownCallerLine1,
            RoamingLine1,
            RingVoip
        }

        private enum SoundType
        {
            On = 0,
            File = 1,
            Vibrate = 2,
            None = 3
        }

        private struct SNDFILEINFO
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szPathNameNative;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szDisplayNameNative;

            public SoundType SstType;
        }

        [DllImport("aygshell.dll", SetLastError = true)]
        private static extern uint SndSetSound(SoundEvent seSoundEvent, ref SNDFILEINFO pSoundFileInfo, bool fSuppressUI);

        [DllImport("aygshell.dll", SetLastError = true)]
        private static extern uint SndGetSound(SoundEvent seSoundEvent, ref SNDFILEINFO pSoundFileInfo);

        public int SetRingerVibrate()
        {
            SNDFILEINFO sfi = new SNDFILEINFO();
            sfi.SstType = SoundType.Vibrate;
            uint ret = SndSetSound(SoundEvent.All, ref sfi, true);
            if (ret != 0)
            {
                return 1;
            }
            return 0;
        }

        public int SetRingerOff()
        {
            SNDFILEINFO sfi = new SNDFILEINFO();
            sfi.SstType = SoundType.None;
            uint ret = SndSetSound(SoundEvent.All, ref sfi, true);
            if (ret != 0)
            {
                return 1;
            }
            return 0;
        }

        public int SetRingerOn()
        {
            SNDFILEINFO sfi = new SNDFILEINFO();
            sfi.SstType = SoundType.On;
            uint ret = SndSetSound(SoundEvent.All, ref sfi, true);
            if (ret != 0)
            {
                return 1;
            }
            return 0;
        }

        private SNDFILEINFO mOldSoundFileInfo = new SNDFILEINFO();
        public int SaveSound()
        {
            uint ret = SndGetSound(SoundEvent.All, ref mOldSoundFileInfo);
            if (ret != 0)
            {
                return 1;
            }
            return 0;
        }

        public int RestoreSound()
        {
            uint ret = SndSetSound(SoundEvent.All, ref mOldSoundFileInfo, true);
            if (ret != 0)
            {
                return 1;
            }
            return 0;
        }
    }


    Its working perfectly. Please let me know, if you find any problem with this code.


    Regards,
    Malleswar


    Saturday, February 28, 2009 11:11 AM