Ejemplo n.º 1
0
        public void record()
        {
            try
            {
                recorder?.Stop();
                recorder?.Release();
                recorder?.Dispose();
                recorder = null;


                int minBufSize = AudioTrack.GetMinBufferSize(sampleRate, channelOut, encoding);
                //       DatagramSocket socket = new DatagramSocket();
                byte[] buffer = new byte[minBufSize];
                //     DatagramPacket packet;
                //     InetAddress destination = InetAddress.GetByName(serverAddress);
                recorder = new AudioRecord(AudioSource.VoiceCommunication, sampleRate, channelIn, encoding, minBufSize * 4);

                if (recorder.State != Android.Media.State.Initialized)
                {
                    return;
                }
                recorder.StartRecording();

                // string Path = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "temp.pcm");

                //System.IO.File.Delete(Path);

                //recorder.StartRecording();

                //DateTime currentTime = DateTime.Now.AddMilliseconds(500);

                //FileStream fs = new FileStream(Path, FileMode.Append, FileAccess.Write);

                //while (recorder.RecordingState == RecordState.Recording)
                //{
                //    minBufSize = recorder.Read(buffer, 0, buffer.Length);

                //    if (completefile == true)
                //        continue;

                //    fs.Write(buffer, 0, buffer.Length);

                //    if (currentTime < DateTime.Now)
                //    {
                //        fs.Close();

                //        play(File.ReadAllBytes(Path));
                //    //    NetProcess.SendAudioMessage(File.ReadAllBytes(Path));

                //        File.Delete(Path);
                //        var r = File.Exists(Path);

                //        fs = new FileStream(Path, FileMode.Append, FileAccess.Write);

                //        currentTime = DateTime.Now.AddMilliseconds(500);

                //    }
                //}

                ConcurrentQueue <System.IO.MemoryStream> Frames = new ConcurrentQueue <System.IO.MemoryStream>();

                while (recorder?.RecordingState == RecordState.Recording)
                {
                    if (recorder == null)
                    {
                        return;
                    }

                    minBufSize = recorder.Read(buffer, 0, buffer.Length);

                    Frames.Enqueue(new MemoryStream(buffer));

                    if (Frames.Count > 0)
                    {
                        if (NetProcess.TargetPlayerId.Count > 0)
                        {
                            NetProcess.SendAudioMessage(Frames);
                        }

                        Frames.Clear();
                    }
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                recorder.Stop();
                recorder.Release();
                recorder.Dispose();
                recorder = null;
            }
        }