2016-06-11 63 views
1

我尝试播放不同的列出的.wav声音,每次点击像医院或银行上的队列应用程序这样的按钮。如何在wpf上单击按钮时播放不同的.wav

我不知道在一个按钮上。

这是我的XAML代码:

<Window x:Class="WpfApplication8.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="296.477"> 
<Grid Margin="0,0,2,0"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="17*"/> 
     <ColumnDefinition Width="15*"/> 
     <ColumnDefinition Width="254*"/> 
     <ColumnDefinition Width="0*"/> 
    </Grid.ColumnDefinitions> 
    <Button Content="Next" Margin="10,251,175,0" VerticalAlignment="Top" Click="Button_Click" RenderTransformOrigin="1.169,0.851" Height="59" FontSize="20" FontWeight="Bold" Grid.ColumnSpan="3"/> 
    <Button Content="Reset" HorizontalAlignment="Left" Margin="150.474,251,0,0" VerticalAlignment="Top" Width="94" Click="Button_Click_1" Grid.Column="2" Height="59" FontSize="20" FontWeight="Bold"/> 
    <TextBox x:Name="number" HorizontalAlignment="Left" Height="132" Margin="10,85,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="216" FontSize="96" FontWeight="Bold" RenderTransformOrigin="0.689,0.462" Text="0" Grid.Column="1" IsReadOnly="True" TextAlignment="Center" Grid.ColumnSpan="2" TextChanged="number_TextChanged"/> 
    <Label Content="No antrian Ke:" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top" Width="259" FontSize="36" Grid.Column="1" Height="53" FontWeight="Bold" Grid.ColumnSpan="2"/> 
    <Button Content="Nomor urut" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Grid.Column="1" Margin="0,224,0,0" Click="Button_Click_2" Grid.ColumnSpan="2"/> 
    <Button Content="angka" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Grid.Column="2" Margin="65,224,0,0" Click="Button_Click_3"/> 

</Grid> 

,这是我xaml.cs代码:

int counter = 0; 

    private void Button_Click(object sender, RoutedEventArgs e) 
    {  
     counter++; 
     number.Text = counter.ToString(); 

    } 

    private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     counter = 0; 
     number.Text = counter.ToString();   
    } 

    private void number_TextChanged(object sender, TextChangedEventArgs e) 
    { 

    } 

    private void Button_Click_2(object sender, RoutedEventArgs e) 
    { 
     System.Media.SoundPlayer 
     player = new System.Media.SoundPlayer 
     (@"D:\TEKNIK DIII MI\SEMESTER 2\project ferdi\WpfApplication8\Sounds\Sounds\nomor-urut.wav"); 
     player.Play(); 
    } 

    private void Button_Click_3(object sender, RoutedEventArgs e) 
    { 
     System.Media.SoundPlayer 
     player = new System.Media.SoundPlayer 
     (@"D:\TEKNIK DIII MI\SEMESTER 2\project ferdi\WpfApplication8\Sounds\Sounds\satu.wav"); 
     player.Play(); 
    } 
} 

}

对不起我还在学习,请给我有人建议。

谢谢

回答

0

只是把你想要播放的所有文件放在列表或数组中。将该文件的当前索引保存在列表中。然后,您可以实现一种遍历列表并播放下一首曲目的方法:

public class PlaylistPlayer 
{ 
    private int _currentTitle = 0; 
    private readonly List<string> _playList; 
    private SoundPlayer _player; 

    public PlaylistPlayer() 
    { 
     _playList = new List<string> 
     { 
      "C:\a.wav", 
      "C:\b.wav" 
     }; 

     _player = new SoundPlayer(); 
    } 

    private void Next() 
    { 
     _currentTitle += 1; 

     //If we are at the end of the playlist start from 0 
     if (_playList.Count > _currentTitle) 
      _currentTitle = 0; 

     var nextTitle = _playList[_currentTitle]; 
     StartTrack(nextTitle); 
    } 

    private void StartTrack(string track) 
    { 
     _player.Stop(); 
     _player.SoundLocation = track; 
     _player.Play(); 
    } 
} 
+0

感谢dennis。但你会告诉我你的设计师?我很困惑,我必须应用你的代码。以前感谢。 –