首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >只在第一次加载页面时播放SoundEffect

只在第一次加载页面时播放SoundEffect
EN

Stack Overflow用户
提问于 2012-06-13 02:12:54
回答 2查看 322关注 0票数 0

WP7.5/Silverlight应用程序。

在我的页面加载,我播放一个声音剪辑(例如,你好!今天是美好的一天。)

代码语言:javascript
复制
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    seLoadInstance = seLoad.CreateInstance(); //I initialize this seLoad in Initialize method
    seLoadInstance.Play();
}

现在,页面上还有3-4个其他元素。当用户单击其中任何一个时,就会播放该元素的声音剪辑。

代码语言:javascript
复制
private void ElementClick_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    seElementInstance = seElement.CreateInstance();
    seElementInstance .Play();
}

我想要的是:当页面第一次加载时,当用户单击元素时,当seLoadInstance被播放时,我不希望播放seElementInstance。

我可以像下面这样检查seLoadInstance的状态,以避免播放seElementInstance

代码语言:javascript
复制
private void ElementClick_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
  if(seLoadTextInstance.State != SoundState.Playing)
  {     
        seElementInstance = seElement.CreateInstance();
        seElementInstance .Play(); 
   }
}

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

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

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

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

有什么主意吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-21 17:16:24

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

如下所示:

代码语言:javascript
复制
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();
}
票数 0
EN

Stack Overflow用户

发布于 2012-06-13 07:20:58

到目前为止,我还没有使用过声音,但我看到的是:

为什么当您想要播放声音时总是创建新的实例?难道不可能为"se"-elements和cust创建一个实例,在调用"play“之前检查是否有人在运行?

例如:

代码语言:javascript
复制
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(); 
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11007363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档