C# 检查xna中是否播放特定歌曲

C# 检查xna中是否播放特定歌曲,c#,xna,C#,Xna,我想知道是否有可能检查XNA中是否播放特定的歌曲,我想做的是 if(stateS == "normal") { if(MediaPlayer.IsPlaying(song1) { //do nothing } if(!MediaPlayer.IsPlaying(song1) { //play son

我想知道是否有可能检查XNA中是否播放特定的歌曲,我想做的是

if(stateS == "normal")
        {
            if(MediaPlayer.IsPlaying(song1)
            {
               //do nothing  
            }
            if(!MediaPlayer.IsPlaying(song1)
            {
               //play song 1 
            }
            spriteBatch.Draw(norm, pos, Color.White);
        }
        if(stateS == "fast")
        {
            if(MediaPlayer.IsPlaying(song2)
            {
               //do nothing  
            }
            if(!MediaPlayer.IsPlaying(song2)
            {
               //play song 2
            }
            spriteBatch.Draw(fast, pos, Color.White);
        }
        if(stateS == "slow")
        {
            if(MediaPlayer.IsPlaying(song3)
            {
               //do nothing  
            }
            if(!MediaPlayer.IsPlaying(song31)
            {
               //play song 3
            }
            spriteBatch.Draw(slow, pos, Color.White);
        }

可悲的是,我没有找到任何方法来做到这一点,因为我没有找到任何方法来查看是否正在播放特定的歌曲。如有任何建议或帮助,将不胜感激

您可以通过使用时间跨度字典标记每个声音开始播放的时间来跟踪所有声音:

Dictionary<string, TimeSpan> SoundStartTimeDic;
然后您可以看到当前时间减去声音开始时间之间的差值是否大于声音的持续时间:

if (gameTime.TotalMilliseconds -
     SoundStartTimeDic[mySound.Name()].TotalMilliseconds >
     mySound.Duration.TotalMilliseconds)
{ /* yes, the sound has played already */ }

所以我想你想在它结束后再播放你的声音。您使用MediaPlayer是出于某种原因

您可以使用
System.Media.SoundPlayer

SoundPlayer sound = new SoundPlayer("path");
sound.PlayLooping();
或者用XNA的方式:

SoundEffect bgmusic;
bgmusic = Content.Load<SoundEffect>("path");
SoundEffectInstance instance = bgEffect.CreateInstance();
instance.IsLooped = true;
bgEffect.Play();
音效音乐;
bgmusic=Content.Load(“路径”);
SoundEffectInstance实例=bgEffect.CreateInstance();
instance.islopped=true;
bgpeffect.Play();

使用另一个名为Playing的Song变量,我将希望播放的歌曲设置为它,并播放它。例如:

        if (stateS == "normal")
        {
            if (!MediaPlayer.Equals(playing, normS))
            {
                playing = normS;
            }

            spriteBatch.Draw(norm, pos, Color.White);
        }
        else if (stateS == "fast")
        {
            if (!MediaPlayer.Equals(playing, fastS))
            {
                playing = fastS;
            }
            spriteBatch.Draw(fast, pos, Color.White);
        }
        else if (stateS == "slow")
        {
            if (!MediaPlayer.Equals(playing, slowS))
            {
                playing = slowS;
            }
        }

不完全是这样,我想检查一下当前是否正在播放一首歌曲(song1)。有一个MediaState属性返回歌曲当前是否正在播放,只是不具体。这个方法可以处理任何具有可测量持续时间的内容,所以。。。在陈述问题时,你真的应该增加更多的上下文。细节,环境,你的研究。你知道,有助于更好地理解你的问题并找到可能不是最明显的解决方案的东西。我只是做了一个编辑,希望它能澄清问题。我同意我提供的信息太少。我使用MediaPlayer的原因是我希望播放一首歌,而不是音效。如果我没有弄错的话,这首歌是声音的派生类别;)没关系,我解决了。谢谢你的帮助!
        if (stateS == "normal")
        {
            if (!MediaPlayer.Equals(playing, normS))
            {
                playing = normS;
            }

            spriteBatch.Draw(norm, pos, Color.White);
        }
        else if (stateS == "fast")
        {
            if (!MediaPlayer.Equals(playing, fastS))
            {
                playing = fastS;
            }
            spriteBatch.Draw(fast, pos, Color.White);
        }
        else if (stateS == "slow")
        {
            if (!MediaPlayer.Equals(playing, slowS))
            {
                playing = slowS;
            }
        }