因為工作上常需要在,當使用者按下一個按鈕時,發出聲音,但是若是在當下才去產生元件撥放音效檔,有時會有延遲的發生,因此做法就改為先將所需的元件先產生出來,等到使用者按下按鈕時再撥放音效
,不過這要注意一點,因為在windowsmediaplayer的autostart屬性預設為true,也就是當你產生元件時,就會撥放音效,這確實令人很頭痛,不過解決方法可以在建立時先將mute設為ture,要撥放時再改設為true 即可,範例如下:
using WMPLib;
...
/// <summary>
/// 預設儲存按鍵音樂物件
/// </summary>
List<WindowsMediaPlayer> listMediaPlayer = new List<WindowsMediaPlayer>();
...
private void CratePadMediaPlayer()
{
listMediaPlayer.Clear();
for (int i = 0; i < 10; i++)
{
WindowsMediaPlayer newMedia = new WindowsMediaPlayer();
string path = System.IO.Directory.GetCurrentDirectory();
newMedia.settings.mute = true;
newMedia.URL = path + @"\audio\" + "phone_0" + i + ".wav";
listMediaPlayer.Add(newMedia);
}
}
...
private void ProcessTelPadLeftButtonDown(string phonenumber)
{
if (phonenumber != "#" && phonenumber != "*")
{
try
{
listMediaPlayer[int.Parse(phonenumber)].settings.mute = false;
listMediaPlayer[int.Parse(phonenumber)].controls.play();
}
catch (Exception ex)
{
#if DEBUG
Console.WriteLine("ProcessTelPadLeftButtonDown ex:"+ex.Message );
#endif
}
}
else
{
listMediaPlayer[0].settings.mute = false;
listMediaPlayer[0].controls.play();
}
}
留言列表