Simple and practical PCM audio players - a few years after the return of silence first notes

--- --- restore content begins

PCM audio streaming network, as useful, not much to explain.

A simple class based NAudio, a simple assembly of the class, instantiation three parameters, followed by the sampling rate, the system playback device Index, playback channel, incoming call PlayData method to the PCM audio stream, with a flow to pass in the data off, playback is automatically stopped.

Next, the code posted:

 1 public class PCMPlayer
 2     {
 3         private MonoToStereoProvider16 monoToStereoProvider16;
 4         private BufferedWaveProvider bufferedWaveProvider;
 5         private WaveOut waveOut;
 6         private bool isRunning = false;
 7         private PCMPlayerChannel currentChannel;
 8 
 9         /// <summary>
10         /// 
11         /// </summary>
12         /// <param name="sampleRate">采样率8K至64K</param>
13         /// <param name = "deviceNumber"> to obtain the KEY value dictionary by GetPlayDevices </ param> 
14          ///  <param name = "playerChannel"> channel playback initialization </ param> 
15          public PCMPlayer ( int the sampleRate, int = DeviceNumber 0 , PCMPlayerChannel playerChannel = PCMPlayerChannel.Both)
 16          {
 . 17              currentChannel = playerChannel;
 18 is              WAVEFORMAT = new new WAVEFORMAT (the sampleRate, 16 , . 1 );
 . 19              bufferedWaveProvider = new new BufferedWaveProvider(WaveFormat);
20             monoToStereoProvider16 = new MonoToStereoProvider16(bufferedWaveProvider);
21             waveOut = new WaveOut();
22             waveOut.DeviceNumber = deviceNumber;
23             waveOut.Init(monoToStereoProvider16);
24             waveOut.Play();
25             isRunning = true;
26         }
27 
28         public void PlayData(byte[] data)
29         {
30             if (!isRunning) return;
31             bufferedWaveProvider.AddSamples(data, 0, data.Length);
32         }
33 
34         public void ClosePlay()
35         {
36             isRunning = false;
37             waveOut.Stop();
38             waveOut.Dispose();
39         }
40 
41         public bool SetPlayChannel(PCMPlayerChannel playerChannel)
42         {
43             if (!isRunning || waveOut == null || monoToStereoProvider16 == null) return false;
44             switch (playerChannel)
45             {
46                 case PCMPlayerChannel.Left:
47                     {
48                         monoToStereoProvider16.LeftVolume = 1.0f;
49                         monoToStereoProvider16.RightVolume = 0.0f;
50                     }
51                     break;
52                 case PCMPlayerChannel.Right:
53                     {
54                         monoToStereoProvider16.LeftVolume = 0.0f;
55                         monoToStereoProvider16.RightVolume = 1.0f;
56                     }
57                     break;
58                 case PCMPlayerChannel.Both:
59                     {
60                         monoToStereoProvider16.LeftVolume = 1.0f;
61                         monoToStereoProvider16.RightVolume = 1.0f;
62                     }
63                     break;
64                 default: return false;
65             }
66             return true;
67         }
68 
69         public WaveFormat WaveFormat { get; private set; }
70 
71         public static Dictionary<int, WaveOutCapabilities> GetPlayDevices
72         {
73             get
74             {
75                 Dictionary<int, WaveOutCapabilities> result = new Dictionary<int, WaveOutCapabilities>();
76                 try
77                 {
78                     int count = WaveOut.DeviceCount;
79                     for (int i = 0; i < count; i++)
80                     {
81                         var item = WaveOut.GetCapabilities(i);
82                         result.Add(i, item);
83                     }
84                 }
85                 catch { }
86                 return result;
87             }
88         }
89     }
90 
91     public enum PCMPlayerChannel
92     {
93         Left,
94         Right,
95         Both
96     }
View Code

 

Preview: Next few words on the audio stream compression and decompression network transmission (based on 10 times NSpeex audio compression, no loss of meat ears)

Guess you like

Origin www.cnblogs.com/yunxizfj/p/11402538.html