WPF播放声音
WPF播放声音
用MediaElement控件,一个放完到下一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | < 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 > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | 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(); } } } |
花一上午弄上面的代码,结果在群里一问,有个更简单的。直接调用系统内置的。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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号患者李娟娟到华兴诊室就诊" ); } } |
真是。。。。%……&¥……*&&*
撸码:复制、粘贴,拿起键盘就是“干”!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2019-06-03 thinkphp项目部署在phpstudy里的nginx上