2014-03-16 107 views

回答

0

Petzold向我們展示了這一點。按一個按鈕,從庫中播放一個隨機曲調。

XAML:

  <Button Content="Button" HorizontalAlignment="Left" Margin="177,160,0,0" VerticalAlignment="Top"/> 

C#代碼背後:使用Microsoft.Xna.Framework.Media

;

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     Random random = new Random(); // new random variable 
     MediaLibrary mediaLib = new MediaLibrary(); // this is where it grabs your whole music library and enumerates everything. If you break here, you can see it. 

     AlbumCollection albums = mediaLib.Albums; // pick which song you want. 
     Album album = albums[random.Next(albums.Count)]; 
     SongCollection songs = mediaLib.Songs; 
     Song song = songs[random.Next(songs.Count)]; 
     MediaPlayer.Play(song); // this plays the song. 
    } 

就這麼簡單。

+0

哦...好吧,你在visual studio 2010中獲得的模板怎麼樣? (音頻播放) –