我正在尝试创建我的母语文本到语音转换器。
我用mp3录制了每个字母。
这是我的代码:
private void PlayFile(string entertext)
{
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
char[] charArray = entertext.ToCharArray();
for (int i = 0; i < charArray.Length; i++)
{
if (charArray[i].ToString() == "a")
{
wplayer.URL = @"E:\alphabet_audio\01_a.mp3";
wplayer.controls.play();
}
else if (charArray[i].ToString() == "b")
{
wplayer.URL = @"E:\alphabet_audio\02_b.mp3";
wplayer.controls.play();
}
}
}
问题是它只播放最后一个。
答案 0 :(得分:0)
您需要确保文件已完成播放。要做到这一点,你需要一个wplayer
对象的处理程序,然后递归检查以确保前一个文件已经完成播放。现在这可能不是最好的做法,因为它假设WMP会在某个时候结束播放,如果不是你有失控的递归,但在这个用例中我不认为这很重要。
Private void PlayFile(string entertext)
{
//Add play state handler
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(wplayer_PlayStateChange);
char[] charArray = entertext.ToCharArray();
for (int i = 0; i < charArray.Length; i++)
{
//check play state
PlaySound(charArray[i].ToString(), wplayer)
}
}
Private void PlaySound(string letter, WMPLib.WindowsMediaPlayer wplayer)
{
if (WMPLib.WMPPlayState.wmppsMediaEnded == wplayer.playState)
{
if (letter == "a")
{
wplayer.URL = @"E:\alphabet_audio\01_a.mp3";
wplayer.controls.play();
}
else if (letter == "b")
{
wplayer.URL = @"E:\alphabet_audio\02_b.mp3";
wplayer.controls.play();
}
} else {
PlaySound(letter, wplayer)
}
}
以下是有关播放状态枚举的一些文档:https://msdn.microsoft.com/en-us/library/windows/desktop/dd564085(v=vs.85).aspx