仅在首次加载页面时播放SoundEffect

时间:2012-06-13 02:12:54

标签: windows-phone-7 xna windows-phone-7.1

WP7.5 / Silverlight App ...

在我的页面加载中,我播放一个声音片段(例如Hello!今天是美好的一天。)

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    seLoadInstance = seLoad.CreateInstance(); //I initialize this seLoad in Initialize method
    seLoadInstance.Play();
}

现在我在页面上有3-4个其他元素。当用户单击其中任何一个时,将播放该元素的声音片段。

private void ElementClick_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    seElementInstance = seElement.CreateInstance();
    seElementInstance .Play();
}

我想要的是: 当页面首次加载并且正在播放seLoadInstance并且用户单击该元素时,我不希望播放seElementInstance。

我可以像下面一样查看seLoadInstance的状态,不能播放seElementInstance

private void ElementClick_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
  if(seLoadTextInstance.State != SoundState.Playing)
  {     
        seElementInstance = seElement.CreateInstance();
        seElementInstance .Play(); 
   }
}

但上面的问题是我有另一个可以在点击时播放seLoadInstance的元素。

问题:我不知道如何区分正在播放的seLoadInstance是第一次还是元素点击。

可能的解决方案:我看到的一种方法是使用不同的实例来播放相同的声音。

我希望有一些更好的方法,比如我在加载时设置了一个标志,但是我找不到SoundInstance完成的任何显式事件或者我可以处理的Stopped。

任何想法??

2 个答案:

答案 0 :(得分:0)

直到现在还没有使用过声音,但我看到了:

为什么要在播放声音时始终创建新实例? 在调用“play”之前,是否有可能为“se”元素创建一个实例并检查是否有人正在运行?

例如:

private var seLoadInstance;
private var seElementInstance;

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    seLoadInstance = seLoad.CreateInstance();
    seElementInstance = seElement.CreateInstance();

    seLoadInstance.Play(); // no need to check if something is playing... nothing will be loaded
}

private void ElementClick_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if(seLoadInstance.State != SoundState.Playing && seElementInstance.State != SoundState.Playing)
    {     
        seElementInstance .Play(); 
    }
}

答案 1 :(得分:0)

我能够找到一种使用旗帜的方法。我没有在第一次加载完成时设置标志,而是从我的一个播放seLoadTextInstance的元素中设置标志。

如下所示:

private bool isElementLoadSoundPlaying = false; //I set this to true below in another handler

private void ElementClick_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
  //This if means LoadTextInstance is playing and it is the first time play
  if(seLoadTextInstance.State != SoundState.Playing && isElementLoadSoundPlaying == false )
  {     
     return;
  }
  seElementInstance = seElement.CreateInstance();
  seElementInstance .Play(); 
}

private void ElementLoadTextClick_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
  isElementLoadSoundPlaying = true;
  seLoadInstance = seLoad.CreateInstance();
  seLoadInstance.Play();
}
相关问题