这可能是一个非常简单的解决办法,但对于我的生活,我无法弄清楚。
我尝试做的是在我的C#项目中连续播放两个音频片段。但是,我不希望播放下一个剪辑,直到第一个剪辑完成播放。
总之,这就是我正在做的(旧代码):
public static void PlayAudioClip(string path)
{
Console.WriteLine(path) // Just so we know which file is playing.
wplayer.controls.stop();
wplayer.URL = path;
wplayer.controls.play();
}
public static void CharacterTalking()
{
PlayAudioClip(@"C:\Game\Audio\Hello world.mp3");
PlayAudioClip(@"C:\Game\Audio\How are you today.mp3");
}
现在,不是一个接一个地播放两个片段,实际上两个片段都没有播放。我一直无法弄明白为什么。
我喜欢使用Windows Media Player库,但如果您有其他建议,请随时分享。我不需要使用Windows Media Player。
发布的类似问题here解释了如何循环文件,但这不是我想做的事情。它有助于解释我可以使用事件处理程序来检查文件的状态,但这对我的解决方案没有帮助。或许它有,我只是过度思考它。
编辑:我发现我之前没有解释过这个问题。道歉。假设我给出的这两个音频文件是而不是程序最终只有两个。我想让我的PlayAudioClip()函数始终等待上一个剪辑完成播放。///////////////////////////////////////
编辑2:这是一个非常复杂的圆形方式,但这是我的新尝试,使用了我学到的一些信息here。
新守则:
public static List<String> Playlist = new List<string>();
public static void AddToPlaylist(string path)
{
Playlist.Add(path);
Console.WriteLine("Queued " + path);
}
public static void RunPlaylist()
{
PlayAudioClip(Playlist.First());
}
private void Wplayer_PlayStateChange(int NewState)
{
switch (NewState)
{
case 0:
WriteFancyConsoleLine("PlayState = wmppsUndefined", ConsoleColor.DarkGray);
break;
case 1:
WriteFancyConsoleLine("PlayState = wmppsStopped", ConsoleColor.Red);
try
{
Playlist.Remove(Playlist.First());
try
{
PlayAudioClip(Playlist.First());
}
catch
{
WriteFancyConsoleLine("Playlist is now empty!", ConsoleColor.DarkRed);
}
}
catch
{
WriteFancyConsoleLine("Playlist already empty!", ConsoleColor.DarkRed);
}
break;
case 2:
WriteFancyConsoleLine("PlayState = wmppsPaused", ConsoleColor.Yellow);
break;
case 3:
WriteFancyConsoleLine("PlayState = wmppsPlaying", ConsoleColor.Green);
break;
case 4:
WriteFancyConsoleLine("PlayState = wmppsScanForward", ConsoleColor.DarkMagenta);
break;
case 5:
WriteFancyConsoleLine("PlayState = wmppsScanReverse", ConsoleColor.Magenta);
break;
case 6:
WriteFancyConsoleLine("PlayState = wmppsBuffering", ConsoleColor.Gray);
break;
case 7:
WriteFancyConsoleLine("PlayState = wmppsWaiting", ConsoleColor.DarkYellow);
break;
case 8:
WriteFancyConsoleLine("PlayState = wmppsMediaEnded", ConsoleColor.DarkRed);
break;
case 9:
WriteFancyConsoleLine("PlayState = wmppsTransitioning", ConsoleColor.DarkGray);
break;
case 10:
WriteFancyConsoleLine("PlayState = wmppsReady", ConsoleColor.Magenta);
break;
case 11:
WriteFancyConsoleLine("PlayState = wmppsReconnecting", ConsoleColor.Magenta);
break;
case 12:
WriteFancyConsoleLine("PlayState = wmppsLast", ConsoleColor.DarkBlue);
break;
}
}
public static void PlayAudioClip(string path)
{
WriteFancyConsoleLine("Playing " + path, ConsoleColor.Gray);
wplayer.controls.stop();
wplayer.URL = path;
wplayer.controls.play();
}
private void CharacterTalking() // This is where the magic happens.
{
AddToPlaylist(@"C:\Game\Audio\Hello world.mp3");
AddToPlaylist(@"C:\Game\Audio\How are you today.mp3");
RunPlaylist();
}
然而,第二个音频片段仍然没有播放。在控制台中,出现wmppsStopped之后,它表示它尝试播放下一个剪辑,但之后只需在wmppsTransitioning和wmppsReady之间跳转几次。
答案 0 :(得分:1)
创建播放列表,添加歌曲,然后播放列表:
WMPLib.IWMPPlaylist Playlist = wplayer.newPlaylist("MyPlayList", "");
Playlist.appendItem(wplayer.newMedia(@"C:\Game\Audio\Hello world.mp3"));
Playlist.appendItem(wplayer.newMedia(@"C:\Game\Audio\How are you today.mp3"));
wplayer.currentPlaylist = Playlist;
wplayer.controls.play();
答案 1 :(得分:0)
我的问题的解决方案在这里找到:
Mediaplayer WinForm won't play
两个剪辑之后不会立即播放的原因是因为Windows Media Player需要一点时间为下一个剪辑做好准备,或者它已经崩溃。
通过在更改播放状态中设置布尔值,并从计时器调用下一个列表项,该程序现在可以正常工作。
public static List<String> Playlist = new List<string>();
public static void AddToPlaylist(string path)
{
Playlist.Add(path);
Console.WriteLine("Queued " + path);
}
public static void RunPlaylist()
{
try
{
PlayStateStopped = false;
PlayAudioClip(Playlist.First());
Playlist.Remove(Playlist.First());
}
catch
{
WriteFancyConsoleLine("Playlist is empty.", ConsoleColor.Red);
}
}
private void Wplayer_PlayStateChange(int NewState)
{
switch (NewState)
{
case 0:
WriteFancyConsoleLine("PlayState = wmppsUndefined", ConsoleColor.DarkGray);
break;
case 1:
WriteFancyConsoleLine("PlayState = wmppsStopped", ConsoleColor.Red);
PlayStateStopped = true;
break;
case 2:
WriteFancyConsoleLine("PlayState = wmppsPaused", ConsoleColor.Yellow);
break;
case 3:
WriteFancyConsoleLine("PlayState = wmppsPlaying", ConsoleColor.Green);
break;
case 4:
WriteFancyConsoleLine("PlayState = wmppsScanForward", ConsoleColor.DarkMagenta);
break;
case 5:
WriteFancyConsoleLine("PlayState = wmppsScanReverse", ConsoleColor.Magenta);
break;
case 6:
WriteFancyConsoleLine("PlayState = wmppsBuffering", ConsoleColor.Gray);
break;
case 7:
WriteFancyConsoleLine("PlayState = wmppsWaiting", ConsoleColor.DarkYellow);
break;
case 8:
WriteFancyConsoleLine("PlayState = wmppsMediaEnded", ConsoleColor.DarkRed);
break;
case 9:
WriteFancyConsoleLine("PlayState = wmppsTransitioning", ConsoleColor.DarkGray);
break;
case 10:
WriteFancyConsoleLine("PlayState = wmppsReady", ConsoleColor.Magenta);
break;
case 11:
WriteFancyConsoleLine("PlayState = wmppsReconnecting", ConsoleColor.Magenta);
break;
case 12:
WriteFancyConsoleLine("PlayState = wmppsLast", ConsoleColor.DarkBlue);
break;
}
}
public static void PlayAudioClip(string path)
{
WriteFancyConsoleLine("Playing " + path, ConsoleColor.Gray);
wplayer.controls.stop();
wplayer.URL = path;
wplayer.controls.play();
PlayStateStopped = false;
}
// Remember that timer1.Enabled = true; has to be set somewhere.
private void timer1_Tick(object sender, EventArgs e)
{
if (PlayStateStopped)
{
RunPlaylist();
}
}