Пример #1
0
        private void UdpSocket_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
        {
            var datareader = args.GetDataReader();
            var dataLength = datareader.UnconsumedBufferLength;
            var buff       = new byte[dataLength];

            datareader.ReadBytes(buff);

            try
            {
                waveProvider.AddSamples(buff, 0, (int)dataLength);

                player.Init(CreateReader);
                player.Play();

                var ep = (EndPoint)localEndPoint;
                if (!udpConnectionActive)
                {
                    datareader.Dispose();
                    udpSocket.OutputStream.Dispose();
                }
            }
            catch (Exception)
            {
                udpConnectionActive = false;
            }
        }
Пример #2
0
        private void StartPlayingUsingNAudio()
        {
            //var device = await getDevice();
            //Debug.WriteLine(device);
            var waveOut = new WasapiOutRT(AudioClientShareMode.Shared, 100);

            waveOut.PlaybackStopped += OnPlaybackStopped;
            waveOut.Init(GetWaveProvider);
            waveOut.Play();
        }
        private void OnVoiceMessage(object sender, MessageWebSocketMessageReceivedEventArgs messageEventArgs)
        {
            using (var reader = messageEventArgs.GetDataStream())
                using (var stream = reader.AsStreamForRead())
                    using (var mStream = new MemoryStream())
                    {
                        var bufferSize = 32000;
                        var bytes      = new List <byte>();
                        var buf        = new byte[bufferSize];
                        var length     = stream.Read(buf, 0, buf.Length);
                        while (length - bufferSize == 0)
                        {
                            bytes.AddRange(buf);
                            length = stream.Read(buf, 0, buf.Length);
                        }
                        if (length > 0)
                        {
                            bytes.AddRange(buf.Take(length).ToArray());
                        }

                        var fullData = bytes.ToArray();
                        mStream.Write(fullData, 0, fullData.Length);
                        mStream.Position = 0;
                        var bitsPerSampleBytes = fullData.Skip(34).Take(2).ToArray();
                        var channelBytes       = fullData.Skip(22).Take(2).ToArray();
                        var samplingBytes      = fullData.Skip(24).Take(4).ToArray();
                        var bitsPerSample      = BitConverter.ToInt16(bitsPerSampleBytes, 0);
                        var channel            = BitConverter.ToInt16(channelBytes, 0);
                        var samplingRate       = BitConverter.ToInt32(samplingBytes, 0);

                        using (var player = new WasapiOutRT(AudioClientShareMode.Shared, 250))
                        {
                            player.Init(() =>
                            {
                                var waveChannel32 =
                                    new WaveChannel32(new RawSourceWaveStream(mStream,
                                                                              new WaveFormat(samplingRate, bitsPerSample, channel)));
                                var mixer = new MixingSampleProvider(new[] { waveChannel32.ToSampleProvider() });

                                return(mixer.ToWaveProvider16());
                            });

                            player.Play();
                            while (player.PlaybackState == PlaybackState.Playing)
                            {
                            }
                        }
                    }
        }
Пример #4
0
        private void Play()
        {
            if (player == null)
            {
                // Exclusive mode - fails with a weird buffer alignment error
                player = new WasapiOutRT(AudioClientShareMode.Shared, 200);
                player.Init(CreateReader);

                player.PlaybackStopped += PlayerOnPlaybackStopped;
            }

            if (player.PlaybackState != PlaybackState.Playing)
            {
                //reader.Seek(0, SeekOrigin.Begin);
                player.Play();
                StopCommand.IsEnabled  = true;
                PauseCommand.IsEnabled = true;
                LoadCommand.IsEnabled  = false;
            }
        }
 private void InitializeWasapi()
 {
     WasapiOut = new WasapiOutRT(AudioClientShareMode.Shared, 100);
     WasapiOut.Init(WaveProvider);
     WasapiOut.Play();
 }