所有mediaelement都没有播放

本文关键字:播放 mediaelement 所有 | 更新日期: 2023-09-27 18:06:51

由于某些原因,只有几个附加到媒体元素的声音正在播放。代码运行并正确命中所有内容,但并非所有声音都将播放。所有的媒体元素都在XAML中,并且被设置为Content,请复制更新的。

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;
        }
    }

所有mediaelement都没有播放

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