locked
How to play a ringtone file on smartphone in c# RRS feed

  • Question

  • I want to create a smartphone app. in which when the phone is charging, the ringtone which is usually on vibrate changes to default or another ringtone when a phone call comes. The ringtone files are located in root dir. (ApplicationData\Sounds). I have written some code but I am not sure if this is the way it should be done. This is my first app. using object browser. I will appreciate your input.
     public Form1()
            {
                InitializeComponent();
                SystemState _Charging = new SystemState(SystemProperty.PowerBatteryState);
                _Charging.Changed += new ChangeEventHandler(_Charging_Changed);
    
                Microsoft.WindowsMobile.Status.SystemProperty systemProperty = Microsoft.WindowsMobile.Status.SystemProperty.PhoneIncomingCall;
                Microsoft.WindowsMobile.Status.SystemState systemState = new Microsoft.WindowsMobile.Status.SystemState(systemProperty);
                systemState.Changed += new Microsoft.WindowsMobile.Status.ChangeEventHandler(systemState_Changed);
    
    
            }
     void systemState_Changed(object sender, Microsoft.WindowsMobile.Status.ChangeEventArgs args)
            {
                Process audioPlayer = new Process();
                audioPlayer.StartInfo = new ProcessStartInfo("wmPlayer","\\ApplicationData\\Sounds\\Ring-WindowsMobile.wav");
                audioPlayer.Start();
    
            }
    
     void _Charging_Changed(object sender, ChangeEventArgs args)
            {
                if (SystemState.PowerBatteryState == BatteryState.Charging)
                
                {
                  
      // I think the code here should be for the incoming call but don't know how to put it.If I write here 
    //systemState_Changed(), it throws an exception saying it takes 0 arguements.Please help.
    
                }
            }
    
    
    
    
    
    
    
    
    Monday, April 20, 2009 1:00 AM

Answers

  • Hi Yo-ho,

    Its working perfectly. Please check the below code. I checked the code with button events.


     

    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;

    }

     

    //P/Invoke to change the volume settings

    [

    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);

     

     

     

    /// <summary>

     

    /// Set the Volume setting to Vibrate mode

     

    /// </summary>

     

    /// <returns></returns>

     

    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;

    }

     

    /// <summary>

     

    /// Set the Ringer into off mode

     

    /// </summary>

     

    /// <returns></returns>

     

    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;

    }

     

    /// <summary>

     

    /// Set the Ringer into on mode

     

    /// </summary>

     

    /// <returns></returns>

     

    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();

     

     

    /// <summary>

     

    /// Saves the current ringer tone file name

     

    /// </summary>

     

    /// <returns></returns>

     

    public int SaveSound()

    {

     

    //mOldSoundFileInfo.SstType = SoundType.None;

     

    uint ret = SndGetSound(SoundEvent.All, ref mOldSoundFileInfo);

     

    //= mOldSoundFileInfo.SstType;

     

    if (ret != 0)

    {

     

    return 1;

    }

     

    return 0;

    }

     

     

    /// <summary>

     

    /// restores to the original volume settings

     

    /// </summary>

     

    /// <returns></returns>

     

    public int RestoreSound()

    {

    State =

    "No";

     

    uint ret = SndSetSound(SoundEvent.All, ref mOldSoundFileInfo, true);

    SaveSound();

     

    if (ret != 0)

    {

     

    return 1;

    }

     

    return 0;

     

    }

     

    private void button1_Click(object sender, EventArgs e)

    {

    SetRingerVibrate();

    }

     

    private void button2_Click(object sender, EventArgs e)

    {

    SetRingerOn();

    }


    It is pefectly moving to vibration and ringoff. Please try to copy the code as it is with our leaving lines.

    Thought you have problem with changing volume settings thats why I tested that functionality.

    Please let me know if you have any queries regarding this.

    Regards,
    Malleswar

    Tuesday, April 21, 2009 4:09 PM

All replies

  • Hi Yo-ho,

    If I am not mistaken, you are looking for something to change the device volume profile when your phone is in charge. If it is, please refer the following link:

    http://social.msdn.microsoft.com/Forums/en-US/vssmartdevicesvbcs/thread/910018a7-c924-499b-a1a9-37d7c8d7dc57/



    If you want to play your own ring tone or music file, refer the following

    http://www.koushikdutta.com/2008/07/playing-mp3s-and-wmas-using-net-compact.html


    Please let me know if these both does not suit your requirements.


    Regards,
    Malleswar
    Monday, April 20, 2009 6:01 AM
  • Thank you for your reply. I will try it for my app.
    Monday, April 20, 2009 12:02 PM
  • Since I am new to windows mobile programming, I'll appreciate if you can look at the code below and let me know if this is the way it should be done. Also I want to know that once this app. is installed on smartphone, everytime the battery is charging will the event be raised and the ringer switches to on mode or is there any control setting or something which will make the app. run in the background. I hope that I am explaining crrectly, If not please let me know.
    I have taken the methods SetRingerOn and SetRingerVibrate from the above links which are provided.
    Thanks.
     public Form1()
            {
                InitializeComponent();
                SystemState _Charging = new SystemState(SystemProperty.PowerBatteryState);
                _Charging.Changed += new ChangeEventHandler(_Charging_Changed);
    
            }
    
     void _Charging_Changed(object sender, ChangeEventArgs args)
            {
                if (SystemState.PowerBatteryState == BatteryState.Charging)
                {
                    SetRingerOn();
                }
                if (SystemState.PowerBatteryState == BatteryState.Normal)
                {
                    SetRingerVibrate();
                }
            }
    Monday, April 20, 2009 7:10 PM
  • Hi Yo-ho,


    When you minimize the application, it automatically runs in back ground. and when ever your charging you device volume settings will be to on mode. When it is in normal it will go automatically vibrate mode.

    So you can test this functionality on the device and let me know if you face any problem.

    Regards,
    Malleswar

    Tuesday, April 21, 2009 4:55 AM
  • Hi Malleswar, I tested the app. on a real device and app. doesn't seems to be working. When the phone is charging, volume doesn't switches from vibrate mode.
    Is there any registry that needs to be updated when the values change or the event takes care of that?
    I will post the entire code, if you can let me know if there is something I am doing wrong.
    On the designer form , there are 2 soft keys, Exit and ok.
    Also which code should go in the ok button for the app. to run, if the user wishes to.
    I really appreciate your help.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.WindowsMobile.Status;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    
    
    namespace MobileLoudRingerApp
    {
        public partial class Form1 : Form
        {
    
    
            public Form1()
            {
                InitializeComponent();
                SystemState _Charging = new SystemState(SystemProperty.PowerBatteryState);
                _Charging.Changed += new ChangeEventHandler(_Charging_Changed);
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            void _Charging_Changed(object sender, ChangeEventArgs args)
            {
                if (SystemState.PowerBatteryState == BatteryState.Charging)
                {
                    SetRingerOn();
                }
                if (SystemState.PowerBatteryState == BatteryState.Normal)
                {
                    SetRingerVibrate();
                }
            }
            
                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, SNDFILEINFO pSoundFileInfo, bool fSupressUI);
    
                [DllImport("aygshell.dll", SetLastError = true)]
                private static extern uint SndGetSound(SoundEvent seSoundEvent, ref SNDFILEINFO pSoundFileInfo);
    
                public int SetRingerOn()
                {
                    SNDFILEINFO sfi = new SNDFILEINFO();
                    sfi.SstType = SoundType.On;
                    uint ret = SndSetSound(SoundEvent.All,sfi, true);
                    if (ret != 0)
                    {
                        return 1;
                    }
                    return 0;
                }
                public int SetRingerVibrate()
                {
                    SNDFILEINFO sfi = new SNDFILEINFO();
                    sfi.SstType = SoundType.Vibrate;
                    uint ret = SndSetSound(SoundEvent.All,sfi, true);
                    if (ret != 0)
                    {
                        return 1;
                    }
                    return 0;
                }
    
            private void menuItem1_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    Tuesday, April 21, 2009 12:25 PM
  • Hi Yo-ho,

    Its working perfectly. Please check the below code. I checked the code with button events.


     

    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;

    }

     

    //P/Invoke to change the volume settings

    [

    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);

     

     

     

    /// <summary>

     

    /// Set the Volume setting to Vibrate mode

     

    /// </summary>

     

    /// <returns></returns>

     

    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;

    }

     

    /// <summary>

     

    /// Set the Ringer into off mode

     

    /// </summary>

     

    /// <returns></returns>

     

    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;

    }

     

    /// <summary>

     

    /// Set the Ringer into on mode

     

    /// </summary>

     

    /// <returns></returns>

     

    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();

     

     

    /// <summary>

     

    /// Saves the current ringer tone file name

     

    /// </summary>

     

    /// <returns></returns>

     

    public int SaveSound()

    {

     

    //mOldSoundFileInfo.SstType = SoundType.None;

     

    uint ret = SndGetSound(SoundEvent.All, ref mOldSoundFileInfo);

     

    //= mOldSoundFileInfo.SstType;

     

    if (ret != 0)

    {

     

    return 1;

    }

     

    return 0;

    }

     

     

    /// <summary>

     

    /// restores to the original volume settings

     

    /// </summary>

     

    /// <returns></returns>

     

    public int RestoreSound()

    {

    State =

    "No";

     

    uint ret = SndSetSound(SoundEvent.All, ref mOldSoundFileInfo, true);

    SaveSound();

     

    if (ret != 0)

    {

     

    return 1;

    }

     

    return 0;

     

    }

     

    private void button1_Click(object sender, EventArgs e)

    {

    SetRingerVibrate();

    }

     

    private void button2_Click(object sender, EventArgs e)

    {

    SetRingerOn();

    }


    It is pefectly moving to vibration and ringoff. Please try to copy the code as it is with our leaving lines.

    Thought you have problem with changing volume settings thats why I tested that functionality.

    Please let me know if you have any queries regarding this.

    Regards,
    Malleswar

    Tuesday, April 21, 2009 4:09 PM