我需要在播放铃声时振动手机。
这是我的代码:
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;
}
}
我的问题是手机首先振动,然后播放声音......在声音持续时间内不可能振动?
感谢。
@ 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 :(得分:1)
添加EventHandler:
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);
}
}