带有Silverlight的Windows Phone 7 - MediaElement不能使用此代码

时间:2010-11-12 18:10:41

标签: windows-phone-7

我做错了什么?

它没有显示任何错误,也没有播放。

MediaElement song = new MediaElement();
        song.Source = new Uri(@"\WP7_aaa\WP7_aaa\GameSounds\MenuScreen.mp3", UriKind.Relative);
        LayoutRoot.Children.Add(song);
        song.AutoPlay = false;
        song.Play();

3 个答案:

答案 0 :(得分:1)

在你的项目中,对于MP3文件,请你 -

  1. Build Action属性设置为Content
  2. Copy To Output Directory设置为Copy Always
  3. 如果您没有在项目中完成上述操作,请尝试使用它们。

    HTH,indyfromoz

答案 1 :(得分:1)

您需要等待加载歌曲,然后才能调用Play方法。

你想要的是:

 MediaElement song = new MediaElement();
 song.Source = new Uri("Audio/background.mp3", UriKind.Relative);
 song.MediaOpened += MediaElement_MediaOpened; 

然后在事件处理程序中:

    private void MediaElement_MediaOpened(object sender, RoutedEventArgs e)
    {
        (sender as MediaElement).Play();
    }

See this thread for more details。 (编辑:现在不知道在哪里找到这个线程,他们将它分成WP和Xbox论坛......)

答案 2 :(得分:0)

您必须将Uri的种类指定为RelativeOrAbsolute。

MediaElement song = new MediaElement();
song.Source = new Uri(@"\WP7_aaa\WP7_aaa\GameSounds\MenuScreen.mp3", UriKind.RelativeOrAbsolute);
LayoutRoot.Children.Add(song);
song.AutoPlay = false;
song.Play();