WPF播放声音
用MediaElement控件,一个放完到下一个
<Window x:Class="WpfApp1_candel.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1_candel" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <StackPanel> <Label Content="序号"></Label> <TextBox Name="txtNo" Text="22"></TextBox> <Label Content="诊室"></Label> <RadioButton Name="rad1" GroupName="zhenzhi" Content="茴香诊室" IsChecked="True"></RadioButton> <RadioButton Name="rad2" GroupName="zhenzhi" Content="合香诊室"></RadioButton> <RadioButton Name="rad3" GroupName="zhenzhi" Content="沉香诊室"></RadioButton> <Button Content="播放声音" Name="btnBoFang" Click="btnBoFang_Click"></Button> <Label Name="lblRes"></Label> <MediaElement Name="me1" Source="music/请.mp3" Visibility="Collapsed" LoadedBehavior="Manual" MediaEnded="me1_MediaEnded" /> <MediaElement Name="me2" Source="" Visibility="Collapsed" LoadedBehavior="Manual" MediaEnded="me2_MediaEnded" /> <MediaElement Name="me3" Source="music/号到.mp3" Visibility="Collapsed" LoadedBehavior="Manual" MediaEnded="me3_MediaEnded" /> <MediaElement Name="me4" Source="" Visibility="Collapsed" LoadedBehavior="Manual" MediaEnded="me4_MediaEnded" /> <MediaElement Source="music/就诊.mp3" Margin="0,0,0,64" Name="me5" UnloadedBehavior="Manual" Stretch="Fill" Visibility="Collapsed" LoadedBehavior="Manual" MediaEnded="me5_MediaEnded" /> </StackPanel> </Window>
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp1_candel { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { private bool _isNoOver = false; //播放序号的声音是否完了 private List<string> listno = new List<string>() { "1", "百", "2", "十", "4" }; private int start = 0; public MainWindow() { InitializeComponent(); } private void btnBoFang_Click(object sender, RoutedEventArgs e) { string no = txtNo.Text; if (no.Length > 4) { lblRes.Content = "最多是4位数"; return; } string zhenshi = ""; if (rad1.IsChecked.Value) { zhenshi = "茴香诊室"; } else if (rad2.IsChecked.Value) { zhenshi = "合香诊室"; } else { zhenshi = "沉香诊室"; } string str = $"请{no}号到{zhenshi}就诊"; SplitNo(no); lblRes.Content = str; me4.Source = new Uri(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"music/{zhenshi}.mp3")); me1.Play(); } //把序号拆成一个一个数存到listno集合中,如 124 拆开来存到 listno中为 new List<string>() { "1", "百", "2", "十", "4" }; private void SplitNo(string no) { listno.Clear(); var arr = no.ToCharArray(); switch (arr.Length) { case 1: //一位数 listno.Add(arr[0].ToString()); break; case 2: //二位数 new List<string>() { "2", "十", "4" }; listno.Add(arr[0].ToString()); listno.Add("十"); if (arr[1] != '0') { listno.Add(arr[1].ToString()); } break; case 3: listno.Add(arr[0].ToString()); listno.Add("百"); listno.Add(arr[1].ToString()); listno.Add("十"); listno.Add(arr[2].ToString()); if (arr[2] == '0') { //个位是0,把最后的个位去掉 listno.RemoveAt(4); if (arr[1] == '0') { //个位是0,十位是0 ,只保留前面二位:X百 listno.RemoveAt(2); listno.RemoveAt(3); } } else if (arr[1] == '0') { //个位不是0,十位是0,把”十“去掉 listno.Remove("十"); } break; case 4: listno.Add(arr[0].ToString()); listno.Add("千"); listno.Add(arr[1].ToString()); listno.Add("百"); listno.Add(arr[2].ToString()); listno.Add("十"); listno.Add(arr[3].ToString()); if (arr[3] == '0') { //个位是0 把最后一位去掉 listno.RemoveAt(6); if (arr[2] == '0') { //十位是0,把”十“去掉 listno.Remove("十"); } if (arr[2] == '0' && arr[3] == '0') { //十位是0,个位也是0, 去掉十位的数字,不用读出来 listno.RemoveAt(4); } if (arr[1] == '0') { //百位是0,把”百“去掉 listno.Remove("百"); } if (arr[1] == '0' && arr[2] == '0' && arr[3] == '0') { //百位,十位,个位 都是0的,把百位的数字去掉,不用读出来 listno.RemoveAt(2); } } else if (arr[2] == '0') { //十位是0,把”十“去掉,不用读出来 listno.Remove("十"); if (arr[1] == '0') { //百位是0,把”0“,”百“去掉,不读出来 listno.RemoveAt(2); listno.Remove("百"); } } else if (arr[1] == '0') { //只有百位是0,把”百“去掉,读成”1“,”千“,”0“,”2“,“十”,“4” listno.Remove("百"); } break; default: break; } } private void me1_MediaEnded(object sender, RoutedEventArgs e) { me1.Stop(); me2.Source = new Uri(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"music/{listno[start]}.mp3")); me2.Play(); } private void me2_MediaEnded(object sender, RoutedEventArgs e) { start++; if (start >= listno.Count) { _isNoOver = true; start = 0; me2.Stop(); me3.Play();//播放”号到“ } else { //序号没播放完就继续放下一位 me2.Source = new Uri(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"music/{listno[start]}.mp3")); me2.Play(); } } private void me3_MediaEnded(object sender, RoutedEventArgs e) { me3.Stop(); me4.Play(); } private void me4_MediaEnded(object sender, RoutedEventArgs e) { me4.Stop(); me5.Play(); } private void me5_MediaEnded(object sender, RoutedEventArgs e) { me5.Stop(); } } }
花了一上午来弄这个。。结果到群里问了下。。给了个更简单的。。直接调用系统内置的。。
private void Button_Click(object sender, RoutedEventArgs e) { // Initialize a new instance of the SpeechSynthesizer. using (SpeechSynthesizer synth = new SpeechSynthesizer()) { // Configure the audio output. synth.SetOutputToDefaultAudioDevice(); // Speak a string synchronously. synth.Speak("请第38号患者李娟娟到华兴诊室就诊"); } }真是。。。%&*%……&…………&*(