所有MediaElements都没有播放

时间:2014-06-05 19:18:33

标签: c# silverlight xaml windows-phone-8

由于某种原因,只有几个附加在媒体元素上的声音正在播放。代码运行并正确地击中所有内容但不会播放所有声音。所有媒体元素都在XAML中,并设置为内容,如果更新则复制。

XAML:

<Grid x:Name="LayoutRoot" Background="White" Margin="0,-1,0,0">
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="85"/>
            <RowDefinition Height="615*"/>
        </Grid.RowDefinitions>
        <TextBlock x:Name="Head" HorizontalAlignment="Left" Margin="10,0,0,0" TextWrapping="Wrap" Text="Electronic" VerticalAlignment="Bottom" FontFamily="Segoe WP Light" FontSize="60" RenderTransformOrigin="0.567,0.259" Height="85" Width="283" Foreground="#FF1BA1E2"/>
        <Grid Grid.Row="1" Background="#FFE9E9E9">
            <telerikPrimitives:RadUniformGrid NumberOfColumns="1" NumberOfRows="8">
                <StackPanel Orientation="Horizontal">
                    <Button x:Name="Aftershock" BorderBrush="{x:Null}" Tap="Button_Tap_1">
                        <Rectangle Fill="#FF1BA1E2" Stroke="#FF1BA1E2" Width="75" Height="75" VerticalAlignment="Center" HorizontalAlignment="Center">
                            <Rectangle.OpacityMask>
                                <ImageBrush ImageSource="Assets/AppBar/Transport.Play.png"/>
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </Button>
                    <TextBlock Text="Aftershock" VerticalAlignment="Center" Margin="20,0,0,0" FontSize="35" Foreground="#DE000000" FontFamily="Segoe WP"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Button x:Name="Conga" BorderBrush="{x:Null}" Tap="Button_Tap_1">
                        <Rectangle Fill="#FF1BA1E2" Stroke="#FF1BA1E2" Width="75" Height="75" VerticalAlignment="Center" HorizontalAlignment="Center">
                            <Rectangle.OpacityMask>
                                <ImageBrush ImageSource="Assets/AppBar/Transport.Play.png"/>
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </Button>
                    <TextBlock Text="Conga" VerticalAlignment="Center" Margin="20,0,0,0" FontSize="35" Foreground="#DE000000" FontFamily="Segoe WP"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Button x:Name="Fever" BorderBrush="{x:Null}" Tap="Button_Tap_1">
                        <Rectangle Fill="#FF1BA1E2" Stroke="#FF1BA1E2" Width="75" Height="75" VerticalAlignment="Center" HorizontalAlignment="Center">
                            <Rectangle.OpacityMask>
                                <ImageBrush ImageSource="Assets/AppBar/Transport.Play.png"/>
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </Button>
                    <TextBlock Text="Fever" VerticalAlignment="Center" Margin="20,0,0,0" FontSize="35" Foreground="#DE000000" FontFamily="Segoe WP"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Button x:Name="Lightspeed" BorderBrush="{x:Null}" Tap="Button_Tap_1">
                        <Rectangle Fill="#FF1BA1E2" Stroke="#FF1BA1E2" Width="75" Height="75" VerticalAlignment="Center" HorizontalAlignment="Center">
                            <Rectangle.OpacityMask>
                                <ImageBrush ImageSource="Assets/AppBar/Transport.Play.png"/>
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </Button>
                    <TextBlock Text="Lightspeed" VerticalAlignment="Center" Margin="20,0,0,0" FontSize="35" Foreground="#DE000000" FontFamily="Segoe WP"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Button x:Name="Remember" BorderBrush="{x:Null}" Tap="Button_Tap_1">
                        <Rectangle Fill="#FF1BA1E2" Stroke="#FF1BA1E2" Width="75" Height="75" VerticalAlignment="Center" HorizontalAlignment="Center">
                            <Rectangle.OpacityMask>
                                <ImageBrush ImageSource="Assets/AppBar/Transport.Play.png"/>
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </Button>
                    <TextBlock Text="Remember" VerticalAlignment="Center" Margin="20,0,0,0" FontSize="35" Foreground="#DE000000" FontFamily="Segoe WP"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Button x:Name="Synth" BorderBrush="{x:Null}" Tap="Button_Tap_1">
                        <Rectangle Fill="#FF1BA1E2" Stroke="#FF1BA1E2" Width="75" Height="75" VerticalAlignment="Center" HorizontalAlignment="Center">
                            <Rectangle.OpacityMask>
                                <ImageBrush ImageSource="Assets/AppBar/Transport.Play.png"/>
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </Button>
                    <TextBlock Text="Synth" VerticalAlignment="Center" Margin="20,0,0,0" FontSize="35" Foreground="#DE000000" FontFamily="Segoe WP"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Button x:Name="Viper" BorderBrush="{x:Null}" Tap="Button_Tap_1">
                        <Rectangle Fill="#FF1BA1E2" Stroke="#FF1BA1E2" Width="75" Height="75" VerticalAlignment="Center" HorizontalAlignment="Center">
                            <Rectangle.OpacityMask>
                                <ImageBrush ImageSource="Assets/AppBar/Transport.Play.png"/>
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </Button>
                    <TextBlock Text="Viper" VerticalAlignment="Center" Margin="20,0,0,0" FontSize="35" Foreground="#DE000000" FontFamily="Segoe WP"/>
                </StackPanel>
             </telerikPrimitives:RadUniformGrid>
        </Grid>
    </Grid>
    <MediaElement x:Name="AftershockSound" AutoPlay="False" Source="/Assets/Sounds/Electronic/Aftershock.mp3"/>
    <MediaElement x:Name="CongaSound" AutoPlay="False" Source="/Assets/Sounds/Electronic/Conga.mp3"/>
    <MediaElement x:Name="FeverSound" AutoPlay="False" Source="/Assets/Sounds/Electronic/Fever.mp3"/>
    <MediaElement x:Name="LightspeedSound" AutoPlay="False" Source="/Assets/Sounds/Electronic/Lightspeed.mp3"/>
    <MediaElement x:Name="RememberSound" AutoPlay="False" Source="/Assets/Sounds/Electronic/Remember.mp3"/>
    <MediaElement x:Name="SynthSound" AutoPlay="False" Source="/Assets/Sounds/Electronic/Synth.mp3"/>
    <MediaElement x:Name="ViperSound" AutoPlay="False" Source="/Assets/Sounds/Electronic/Viper.mp3"/>
</Grid>

C#

    private void Button_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
    {
        e.Handled = true;
        var button = sender as Button;
        string alarmName = button.Name;

        switch (alarmName)
        {
            case "Aftershock":
                AftershockSound.Stop();
                AftershockSound.Play();
                break;
            case "Conga":
                CongaSound.Stop();
                CongaSound.Play();
                break;
            case "Fever":
                FeverSound.Stop();
                FeverSound.Play();
                break;
            case "Lightspeed":
                LightspeedSound.Stop();
                LightspeedSound.Play();
                break;
            case "RememberSound":
                RememberSound.Stop();
                RememberSound.Play();
                break;
            case "SynthSound":
                SynthSound.Stop();
                SynthSound.Play();
                break;
            case "Viper":
                ViperSound.Stop();
                ViperSound.Play();
                break;
        }
    }

1 个答案:

答案 0 :(得分:1)

处理所有音频文件的MediaOpened事件,并确保在引发MediaOpened事件后调用停止/播放方法。