2010-11-12 76 views
2

我在做什么错了,请打?的Windows Phone 7使用Silverlight - MediaElement的不使用此代码

它不显示任何错误,不玩了。

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(); 
+0

哦,我只需要改变LayoutRoot的网格内,例如:ContentPanel.children ... 。=) – Fulano 2010-11-12 19:45:01

回答

1

在项目中,为MP3文件,有你 -

  1. 设置Build Action属性Content
  2. 设置Copy To Output DirectoryCopy Always

如果你还没有完成上述项目,尝试出来。

HTH,indyfromoz

0

您必须指定一种开放的作为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(); 
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论坛...)