前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c# 叫号小程序

c# 叫号小程序

作者头像
冰封一夏
发布2019-09-11 15:22:17
6360
发布2019-09-11 15:22:17
举报

写个叫号的小demo

长相如下

代码如下

代码语言:javascript
复制
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.IO;
 7 using System.Linq;
 8 using System.Media;
 9 using System.Text;
10 using System.Threading;
11 using System.Windows.Forms;
12 using NAudio.Wave;
13 
14 namespace VoiceDemo
15 {
16     public partial class Form1 : Form
17     {
18         public Form1()
19         {
20             InitializeComponent();
21         }
22 
23         private void button1_Click(object sender, EventArgs e)
24         {
25             string[] lstMp3s = Directory.GetFiles(Application.StartupPath + "\\mp3", "*.mp3");
26             Dictionary<string, string> lstDicMp3 = new Dictionary<string, string>();
27             for (int i = 0; i < lstMp3s.Length; i++)
28             {
29                 lstDicMp3.Add(Path.GetFileNameWithoutExtension(lstMp3s[i]), lstMp3s[i]);
30             }
31             lstDicMp3 = lstDicMp3.OrderByDescending(p => p.Key.Length).ToDictionary(p => p.Key, o => o.Value);
32 
33             string strText = textBox1.Text;
34             List<string> lst = new List<string>();
35             GetPlayList(lstDicMp3, strText, ref lst);
36             foreach (var item in lst)
37             {
38                 using (var ms = File.OpenRead(lstDicMp3[item]))
39                 using (var rdr = new Mp3FileReader(ms))
40                 using (var wavStream = WaveFormatConversionStream.CreatePcmStream(rdr))
41                 using (var baStream = new BlockAlignReductionStream(wavStream))
42                 using (var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
43                 {
44                     waveOut.Init(baStream);
45                     waveOut.Play();
46                     while (waveOut.PlaybackState == PlaybackState.Playing)
47                     {
48                         Thread.Sleep(10);
49                     }
50                 }
51             }
52         }
53 
54         private void GetPlayList(Dictionary<string, string> lstDicMp3, string strText, ref  List<string> lst)
55         {
56             foreach (var item in lstDicMp3)
57             {
58                 if (strText.StartsWith(item.Key))
59                 {
60                     lst.Add(item.Key);
61                     strText = strText.Remove(0, item.Key.Length);
62                     break;
63                 }
64             }
65             if (strText.Length > 0)
66                 GetPlayList(lstDicMp3, strText, ref lst);
67         }
68     }
69 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-12-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档