如何在wpf上单击按钮时播放不同的.wav

时间:2016-06-11 04:31:43

标签: c# wpf xaml

我尝试播放不同列出的.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();
    }
}

}

抱歉,我还在学习,请给我一些建议。

感谢

1 个答案:

答案 0 :(得分: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();
    }
}