2010-11-29 70 views
0

我需要在播放鈴聲時振動手機。在netcf C中播放鈴聲時振動#

這是我的代碼:

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

我的問題是,手機FIRST震動,然後播放聲音..是不是不可能性振動的聲音的持續時間?

謝謝。

@ x86shadow:我試圖與線程,但不工作:(

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

    } 
+0

你可能想使用兩個線程來做到這一點,一個用於振動,一個用於播放聲音。 – fardjad 2010-11-29 14:41:24

+0

@ x86shadow:你能給我一個例子嗎? – Leen15 2010-11-29 14:48:50

回答

1

添加一個事件處理程序:

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

嘗試創建一個事件:

private void player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e) 
    { 
     if (Player.playState == WMPLib.WMPPlayState.wmppsPlaying) 
     { 
      SetVibrate(true); 
     } 
     else 
     { 
      SetVibrate(false); 
     } 
    }