在 Windows Phone 7 中使用 MediaElement 播放 mp3



我正在尝试在Windows Phone 7中使用MediaElement播放mp3。这是代码。请帮助我,我什至没有收到错误...但是这首歌也不会播放...

private void button1_Click_1(object sender, RoutedEventArgs e)
    {
        mediaElement.Source = new Uri("Song.mp3", UriKind.Relative);
        if (mediaElement.CurrentState == MediaElementState.Playing)
        {
            button1.Content = "Pause";
            mediaElement.Pause();
        }
        else
        {
            button1.Content = "Play";
            mediaElement.Play();
        }
    }

XAML 页面

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <MediaElement Height="201" HorizontalAlignment="Left" Margin="9,6,0,0" Name="mediaElement" VerticalAlignment="Top" Width="441" />
        <Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="12,213,0,0" Name="button1" VerticalAlignment="Top" Width="438" />
    </Grid>

请确保文件的 URI 是相关的。并且歌曲.mp3需要设置为"内容"而不是"资源"文件。而且我认为您还需要在文件中设置"如果较新则复制到目标"。

媒体元素需要先加载其媒体,然后才能调用 Play(),否则不会发生任何事情。加载媒体时触发事件 MediaOpen。

myMediaElement.MediaOpened += (o, args) => myMediaElement.Play();

在 XAML 中:

{<Grid x:Name="ContentPanel" Margin="12,160,12,0" Grid.RowSpan="2">
            <Button Content="Xaml Play" Name="button1" Click="button1_Click" />
            <MediaElement x:Name="playSound" Source="sounds/Loose.wav" AutoPlay="False" Height="0" Width="0"  /></Grid>}

"

在 C# 中:

private void button1_Click(object sender, RoutedEventArgs e)
        {
            playSound.Play();
        }

希望这对:)有所帮助

最新更新