写个叫号的小demo
长相如下
代码如下
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 }