2012-06-13 182 views
0

WP7.5/Silverlight應用程序......只在第一次頁面加載

在我的頁面加載,我玩的是聲音片段(如您好!今天是美好的一天。)

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

現在我在頁面上有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已完成或停止的任何顯式事件,我可以處理。

任何想法??

回答

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(); 
} 
0

沒有使用過的聲音到現在爲止,但我所看到的:

爲什麼當你想播放聲音,你總是創建新實例? 如果有人在調用「play」之前正在運行,是不是可以爲「se」-elements和cust檢查創建一個實例?

例如:

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

感謝。我想播放要點擊的元素的聲音,即停止正在播放的任何聲音。我不希望發生這種情況的唯一時間是首次加載頁面時。正如我之前提到的,我無法按照你提到的方式使用國家。對於創建新實例,我實際上會在實例停止播放後處理它。我不確定這是一個問題還是錯誤的編碼方式。 – oms

相關問題