Krok 7: Přidat kód do vašeho formuláře k přehrání zvuků
Nyní jste připraveni přidat druhý SoundPlayer a potom přidat metodu k volání jednotlivých SoundPlayer.
Přehrát zvuky
Začněte přidáním druhého SoundPlayer k přehrání zvuku Windows Tada. Hra bude tento zvuk přehrávat, dokud přehrávač nedosáhne jmenovky Konec.
Public Class Form1 ' This SoundPlayer plays a sound whenever the player hits a wall. Dim startSoundPlayer = New System.Media.SoundPlayer("C:\Windows\Media\chord.wav") ' This SoundPlayer plays a sound when the player finishes the game. Dim finishSoundPlayer = New System.Media.SoundPlayer("C:\Windows\Media\tada.wav") Public Sub New() ' This call is required by Windows Forms Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. MoveToStart() End Sub
public partial class Form1 : Form { // This SoundPlayer plays a sound whenever the player hits a wall. System.Media.SoundPlayer startSoundPlayer = new System.Media.SoundPlayer(@"C:\Windows\Media\chord.wav"); // This SoundPlayer plays a sound when the player finishes the game. System.Media.SoundPlayer finishSoundPlayer = new System.Media.SoundPlayer(@"C:\Windows\Media\tada.wav"); public Form1() { InitializeComponent(); MoveToStart(); }
Oba SoundPlayers jsou nyní přidány do formuláře. Přidejte metodu Play() k zavolání SoundPlayer, chcete-li zvuk přehrát v přiměřené době. Chcete zvuk přehrát, pokud uživatel narazí do zdi. Přidejte tedy příkaz startSoundPlayer.Play(); do vaší MoveToStart() metody. Nezapomeňte aktualizovat komentář. Výsledná metoda vypadá následovně.
''' <summary> ''' Play a sound, then move the mouse pointer to a point 10 pixels down and to ''' the right of the starting point in the upper-left corner of the maze. ''' </summary> ''' <remarks></remarks> Private Sub MoveToStart() startSoundPlayer.Play() Dim startingPoint = Panel1.Location startingPoint.Offset(10, 10) Cursor.Position = PointToScreen(startingPoint) End Sub
/// <summary> /// Play a sound, then move the mouse pointer to a point 10 pixels down and to /// the right of the starting point in the upper-left corner of the maze. /// </summary> private void MoveToStart() { startSoundPlayer.Play(); Point startingPoint = panel1.Location; startingPoint.Offset(10, 10); Cursor.Position = PointToScreen(startingPoint); }
Přidejte příkaz finishSoundPlayer.Play(); k obslužné rutině události MouseEnter jmenovky Konec. Nezapomeňte aktualizovat komentář, protože takto měníte kód.
Private Sub finishLabel_MouseEnter() Handles finishLabel.MouseEnter ' Play a sound, show a congratulatory MessageBox, then close the form. finishSoundPlayer.Play() MessageBox.Show("Congratulations!") Close() End Sub
private void finishLabel_MouseEnter(object sender, EventArgs e) { // Play a sound, show a congratulatory MessageBox, then close the form. finishSoundPlayer.Play(); MessageBox.Show("Congratulations!"); Close(); }
Chcete-li pokračovat nebo znovu projít
Přechod na další výukový program naleznete v tématu Krok 8: Spustit program a vyzkoušet ostatní funkce.
K návratu do předchozího kroku výukového programu přejděte na Krok 6: Přidat komponentu SoundPlayer.