0

i need to vibrate the phone while playing a ringtone.

This is my code:

   public static bool PlaySound(string soundName)
    {
        try
        {
            WMPLib.WindowsMediaPlayer player = new WMPLib.WindowsMediaPlayer();
            string MediaFile = Assembly.GetExecutingAssembly().GetName().CodeBase.Substring(0, Assembly.GetExecutingAssembly().GetName().CodeBase.LastIndexOf("\\")) + "\\Resources\\" + soundName;
            player.URL = MediaFile;
            WindowsMediaPlayerClass wmp = new WindowsMediaPlayerClass();

            player.settings.volume = 100;
            player.controls.play();
            SetVibrate(true);
            System.Threading.Thread.Sleep((int)wmp.newMedia(MediaFile).duration*1000 + 100);
            SetVibrate(false);
            return true;
        }
        catch
        {
            return false;
        }
    }

My problem is that the phone FIRST vibrate, then Play sound.. is not possibile to vibrate for the duration of the sound?

thanks.

@x86shadow: I tried with thread but not working :(

   public static bool PlaySound(string soundName)
    {
        try
        {
            // 29/11/2010 Luca - Aggiungo vibrazione durante il suono del messaggio.
            WMPLib.WindowsMediaPlayer player = new WMPLib.WindowsMediaPlayer();

            string MediaFile = Assembly.GetExecutingAssembly().GetName().CodeBase.Substring(0, Assembly.GetExecutingAssembly().GetName().CodeBase.LastIndexOf("\\")) + "\\Resources\\" + soundName;
            player.URL = MediaFile;
            WindowsMediaPlayerClass wmp = new WindowsMediaPlayerClass();

            player.settings.volume = 100;
            RingDuration = (int) wmp.newMedia(MediaFile).duration*1000 + 100;

            VibrateWhilePlayingThread = new Thread(VibrateWhilePlaying);


            VibrateWhilePlayingThread.Start();

            player.controls.play();

            VibrateWhilePlayingThread.Join();

            return true;
        }
        catch
        {
            return false;
        }
    }

    private static int RingDuration;

    public static Thread VibrateWhilePlayingThread;

    public static void VibrateWhilePlaying()
    {
        SetVibrate(true);
        System.Threading.Thread.Sleep(RingDuration);
        SetVibrate(false);

    }
Paul Turner
  • 38,949
  • 15
  • 102
  • 166
Leen15
  • 113
  • 1
  • 14
  • you may want to use two threads to do this, one for vibration and one for playing sound. – fardjad Nov 29 '10 at 14:41
  • @x86shadow: could you give me an example? – Leen15 Nov 29 '10 at 14:48
  • http://stackoverflow.com/questions/2533266/how-to-start-a-smartdevice-application-minimized-in-c/2544463#2544463 this is not what you want but, may helps. I used two threads, one listening for the job done in another thread and both are running. – fardjad Nov 29 '10 at 15:01

1 Answers1

1

Adding an EventHandler:

Player.PlayStateChanged += new AxWMPLib._WMPOCXEvents_PlayChangeEventHandler(player_PlayStateChange);

Try to create an Event:

    private void player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {
        if (Player.playState == WMPLib.WMPPlayState.wmppsPlaying)
        {
            SetVibrate(true);
        }
        else
        {
            SetVibrate(false);
        }
    }
tinu
  • 558
  • 5
  • 18
  • yes, i'm sorry. that link is for Windows mobile 6.5: http://msdn.microsoft.com/en-us/library/bb416323.aspx – tinu Nov 30 '10 at 07:20
  • Thanks, but i need to keep compatibility at least with WM5-WM6.1. Why if i put the vibrate in thread nothing change? do i have to put the play in thread? – Leen15 Nov 30 '10 at 09:32
  • the Play() function don't have a Return Value, so a While there i think cannot be exist.. :( – Leen15 Nov 30 '10 at 11:47
  • Okay i'm confused... does'nt exist any Event? For example a stop-event of the player? – tinu Nov 30 '10 at 12:53
  • Yes i tried with some event of WmpLib, but nothing that work :( – Leen15 Nov 30 '10 at 13:48
  • Hmm.. strange. i don't know an other way to try it. I will contact you when I have something figured out. – tinu Nov 30 '10 at 14:06