【问题标题】:How to play different .wav when click button on wpf单击wpf上的按钮时如何播放不同的.wav
【发布时间】:2016-06-11 18:09:01
【问题描述】:

每次单击按钮时,我都会尝试播放不同的列出的 .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();
    }
}

}

对不起,我还在学习,请给我一些建议。

谢谢

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

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

    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();
        }
    }
    

    【讨论】:

    • 谢谢丹尼斯。但是你能告诉我你的设计师吗?我很困惑我必须在哪里应用你的代码。之前谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    相关资源
    最近更新 更多