Windows phone 7 在Windows Phone 7中使用MediaElement播放mp3

Windows phone 7 在Windows Phone 7中使用MediaElement播放mp3,windows-phone-7,windows-phone,Windows Phone 7,Windows Phone,我正在尝试在WindowsPhone7中使用MediaElement播放mp3。这是密码。请帮帮我,我连一个错误都没有。。。但是这首歌也没有播放 private void button1_Click_1(object sender, RoutedEventArgs e) { mediaElement.Source = new Uri("Song.mp3", UriKind.Relative); if (mediaElement.CurrentState =

我正在尝试在WindowsPhone7中使用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正确无误。而且Song.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>}

希望这有帮助:)

嗨,谢谢你的回答。。。但那没用。你还能想到其他原因吗?我编辑了上面的代码。因此,当播放停止时,也不要在按钮上“暂停”。这意味着这首歌没有播放…:-(URL有效吗?请将URI与“mediaplayer任务”一起使用进行尝试)您好,我想这是我的仿真器中的一个问题。有人知道如何在vs2010中重置windows phone的设置吗?我在另一台机器上尝试了相同的代码,效果很好…当您关闭并再次打开仿真器时,仿真器设置会自动恢复。您是否尝试重新启动您的机器?还要检查仿真器的声音是否静音。OMG,我是o抱歉……这是我的错……我已关闭WP7模拟器的声音……:o不要使用此功能,请使用XNA SoundEffect是的,这是一种更好的方法
private void button1_Click(object sender, RoutedEventArgs e)
        {
            playSound.Play();
        }