简体   繁体   English

使用多个按钮播放声音时出现问题Windows Phone

[英]Trouble with multiple buttons to play sounds Windows Phone

Here is three standard buttons with the media element in xaml : 这是xaml中带media元素的三个标准buttons

<Button Content="Sound1"
            Click="Button_Click1"
            HorizontalAlignment="Center"  
            VerticalAlignment="Top"/>
    <MediaElement x:Name="PlaySound1"
                    Grid.Row="1"
                    />

    <Button Content="Sound2" 
            Click="Button_Click2"
            HorizontalAlignment="Center"  
            VerticalAlignment="Top" 
            Margin="177,72,177,0"
            Width="126"/>
    <MediaElement x:Name="PlaySound2"
                    Grid.Row="1"
                    />

    <Button Content="Sound3" 
            Click="Button_Click3"
            HorizontalAlignment="Center"
            VerticalAlignment="Top"
            Margin="177,149,177,0"                      
            Width="126" />
    <MediaElement x:Name="PlaySound3"
                    Grid.Row="1"
                    />

Here is The simple code I used, and when I try to play a third button, it doesn't work, I clicked the buttons in all combinations (first the button2 , button3, and button1 ) and it doesn't worked. 这是我使用的简单代码,当我尝试播放第三个按钮时,它不起作用,我单击了所有组合的buttons (首先是button2 ,button3和button1 ),但它不起作用。

private void Button_Click1(object sender, RoutedEventArgs e)
{
    PlaySound1.Source = new Uri("sound.wma", UriKind.Relative);
    PlaySound1.Stop();
    PlaySound1.Play();
}

private void Button_Click2(object sender, RoutedEventArgs e)
{
    PlaySound2.Source = new Uri("sound2.wma", UriKind.Relative);
    PlaySound2.Stop();
    PlaySound2.Play();

}

private void Button_Click3(object sender, RoutedEventArgs e)
{
    PlaySound3.Source = new Uri("sound3.wma", UriKind.Relative);
    PlaySound3.Stop();
    PlaySound3.Play();
}

What is the correct method to use multiple buttons with multiple sounds? 使用带有多个声音的多个按钮的正确方法是什么?

By default, when you add a .Source it will play the sound. 默认情况下,当您添加.Source时,它将播放声音。 And, by default, Stop won't work. 而且,默认情况下,停止将不起作用。 In your initialization, set the .LoadedBehavior to Manual and assign the sources. 在初始化时,将.LoadedBehavior设置为Manual并分配源。

    protected override void OnInitialized(EventArgs e)
    {
        PlaySound1.LoadedBehavior = MediaState.Manual;
        PlaySound2.LoadedBehavior = MediaState.Manual;
        PlaySound3.LoadedBehavior = MediaState.Manual;
        PlaySound1.Source = new Uri("aah-01.wav", UriKind.Relative);
        PlaySound2.Source = new Uri("crowd-groan.wav", UriKind.Relative);
        PlaySound3.Source = new Uri("laugh-01.wav", UriKind.Relative);

        base.OnInitialized(e);
    }

then you can have 那么你可以拥有

    private void Button_Click1(object sender, RoutedEventArgs e)
    {
        PlaySound1.Stop();
        PlaySound1.Play();
    }

You can try something like this 您可以尝试这样的事情

private void Click(object sender, EventArgs e)
{
Button button = sender as Button;
switch(button.Name)
{
case "sound1":
PlaySound1.Stop();
PlaySound1.Source= new Uri("sound.wma",UriKind.Relative);
PlaySound1.Play();
break;
case "sound2":
PlaySound2.Stop();
PlaySound2.Source= new Uri("sound2.wma",UriKind.Relative);
PlaySound2.Play();
break;
case "sound3":
PlaySound3.Stop();
PlaySound3.Source= new Uri("sound3.wma",UriKind.Relative);
PlaySound3.Play();
break;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM