Exemplo n.º 1
0
        public void StartRecord(string sFileName)
        {
            this.sFileName = sFileName;

            var path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), sFileName);

            path = Android.App.Application.Context.GetExternalFilesDir(null).AbsolutePath + "/" + sFileName;

            _recorder.SetAudioSource(AudioSource.Mic);
            _recorder.SetOutputFormat(OutputFormat.Mpeg4);
            _recorder.SetAudioEncoder(AudioEncoder.Aac);
            _recorder.SetOutputFile(path);

            if (Int32.Parse(Android.OS.Build.VERSION.Sdk) >= 10)
            {
                _recorder.SetAudioSamplingRate(44100);
                _recorder.SetAudioEncodingBitRate(96000);
            }
            else
            {
                _recorder.SetAudioSamplingRate(8000);
                _recorder.SetAudioEncodingBitRate(12200);
            }

            _recorder.Prepare();
            _recorder.Start();
        }
Exemplo n.º 2
0
        private void RecordBtn_Click(object sender, EventArgs e)
        {
            recording = !recording;

            if (recording)
            {
                // Start recording
                recorder.SetAudioSource(AudioSource.Mic);
                recorder.SetOutputFormat(OutputFormat.Mpeg4);
                recorder.SetAudioEncoder(AudioEncoder.Aac);
                recorder.SetAudioSamplingRate(44100);
                recorder.SetAudioEncodingBitRate(96000);
                recorder.SetOutputFile(filePath);
                recorder.Prepare();
                recorder.Start();

                recordBtn.Text = Resources.GetString(Resource.String.StopBtn);
                image.Alpha    = 1f;
                clockThread    = new Thread(UpdateClock);
                clockThread.Start();
            }
            else
            {
                // Stop recording
                recorder.Stop();
                recorder.Reset();

                recordBtn.Text = Resources.GetString(Resource.String.StartBtn);
                image.Alpha    = LowAlpha;
                clockThread.Join();

                PlaybackAcceptPopup();
            }
        }
Exemplo n.º 3
0
 public void Start(string filePath)
 {
     try
     {
         if (File.Exists(filePath))
         {
             File.Delete(filePath);
         }
         if (recorder == null)
         {
             recorder = new MediaRecorder(); // Initial state.
         }
         recorder.Reset();
         recorder.SetAudioSource(AudioSource.Mic);
         recorder.SetOutputFormat(OutputFormat.Default);
         recorder.SetAudioEncoder(AudioEncoder.AmrNb);
         recorder.SetAudioChannels(1);
         recorder.SetAudioEncodingBitRate(96000);
         //recorder.SetAudioSamplingRate(44100);
         //recorder.SetAudioEncodingBitRate(48000);
         //recorder.SetAudioSamplingRate(22050);
         recorder.SetOutputFile(filePath);
         recorder.Prepare(); // Prepared state
         //recorder.SetMaxDuration(30000);
         recorder.Start();   // Recording state.
     }
     catch (Exception e)
     {
         HandleError.Process("RecordAudioService", "Start", e, false);
     }
 }
Exemplo n.º 4
0
        public void PrepareRecording()
        {
            try
            {
                if (File.Exists(m_oSaveRawPath))
                {
                    File.Delete(m_oSaveRawPath);
                }


                if (m_oRecorder == null)
                {
                    m_oRecorder = new MediaRecorder();
                }
                else
                {
                    m_oRecorder.Reset();
                }
                m_oRecorder.SetAudioSource(AudioSource.Mic);
                m_oRecorder.SetOutputFormat(OutputFormat.RawAmr);
                m_oRecorder.SetAudioEncoder(AudioEncoder.Default);
                m_oRecorder.SetMaxDuration(10000); // 10sec
                //m_oRecorder.SetAudioSamplingRate(44100);
                m_oRecorder.SetAudioEncodingBitRate(16);
                m_oRecorder.SetOutputFile(m_oSaveRawPath);
                m_oRecorder.Prepare();
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine("Error: " + ex.Message);
            }
        }
Exemplo n.º 5
0
        public void PrepareAudio()
        {
            try
            {
                // 一开始应该是false的
                _isPrepared = false;

                File dir = new File(_mDirString);
                if (!dir.Exists())
                {
                    dir.Mkdirs();
                }
                string fileNameString = GeneralFileName();
                using (var file = new File(dir, fileNameString))
                {
                    _mCurrentFilePathString = file.AbsolutePath;

                    _mRecorder = new MediaRecorder();
                    // 设置输出文件
                    _mRecorder.SetOutputFile(file.AbsolutePath);
                }
                //设置meidaRecorder的音频源是麦克风
                _mRecorder.SetAudioSource(AudioSource.Mic);
                // 设置文件音频的输出格式为amr
                _mRecorder.SetOutputFormat(OutputFormat.RawAmr);
                // 设置音频的编码格式为amr
                _mRecorder.SetAudioEncoder(AudioEncoder.AmrNb);
                //设置录音声道
                _mRecorder.SetAudioChannels(1);
                //设置编码比特率
                _mRecorder.SetAudioEncodingBitRate(1);
                //设置编码采样率
                _mRecorder.SetAudioSamplingRate(8000);
                // 严格遵守google官方api给出的mediaRecorder的状态流程图
                try
                {
                    _mRecorder.Prepare();
                }
                catch (IOException e)
                {
                    Debug.WriteLine("录音未成功,请重试" + e.Message);
                }
                _mRecorder.Start();
                // 准备结束
                _isPrepared = true;

                // 已经准备好了,可以录制了
                Prepared?.Invoke(this, new EventArgs());
            }
            catch (IllegalStateException e)
            {
                e.PrintStackTrace();
            }
            catch (IOException e)
            {
                e.PrintStackTrace();
            }
        }
Exemplo n.º 6
0
        protected override void encodeWithMediaRecorder()
        {
            // We need a local socket to forward data output by the camera to the packetizer
            createSockets();

            Android.Util.Log.v(TAG, "Requested audio with " + mQuality.bitRate / 1000 + "kbps" + " at " + mQuality.samplingRate / 1000 + "kHz");

            mMediaRecorder = new MediaRecorder();
            mMediaRecorder.SetAudioSource((AudioSource)mAudioSource);
            mMediaRecorder.SetOutputFormat((OutputFormat)mOutputFormat);
            mMediaRecorder.SetAudioEncoder((AudioEncoder)mAudioEncoder);
            mMediaRecorder.SetAudioChannels(1);
            mMediaRecorder.SetAudioSamplingRate(mQuality.samplingRate);
            mMediaRecorder.SetAudioEncodingBitRate(mQuality.bitRate);

            // We write the output of the camera in a local socket instead of a file !
            // This one little trick makes streaming feasible quiet simply: data from the camera
            // can then be manipulated at the other end of the socket
            FileDescriptor fd = null;

            if (sPipeApi == PIPE_API_PFD)
            {
                fd = mParcelWrite.FileDescriptor;
            }
            else
            {
                fd = mSender.FileDescriptor;
            }
            mMediaRecorder.SetOutputFile(fd);
            mMediaRecorder.SetOutputFile(fd);

            mMediaRecorder.Prepare();
            mMediaRecorder.Start();

            InputStream iStream = null;

            if (sPipeApi == PIPE_API_PFD)
            {
                iStream = new ParcelFileDescriptor.AutoCloseInputStream(mParcelRead);
            }
            else
            {
                try {
                    // mReceiver.getInputStream contains the data from the camera
                    iStream = mReceiver.InputStream;
                } catch (IOException e) {
                    stop();
                    throw new IOException("Something happened with the local sockets :/ Start failed !");
                }
            }

            // the mPacketizer encapsulates this stream in an RTP stream and send it over the network
            mPacketizer.setInputStream(iStream);
            mPacketizer.start();
            mStreaming = true;
        }
Exemplo n.º 7
0
        void StartRecording()
        {
            _recorder    = new MediaRecorder();
            _isrecording = true;
            // Set how we want the audio formatting to be.
            _recorder.SetAudioSource(AudioSource.Mic);
            _recorder.SetOutputFormat(OutputFormat.Mpeg4);
            _recorder.SetAudioEncoder(AudioEncoder.Aac);
            _recorder.SetAudioSamplingRate(44100);
            _recorder.SetAudioEncodingBitRate(48000);

            _recorder.SetOutputFile(_path);
            _recorder.Prepare();
            _recorder.Start();
            LOG_EVENT("START_RECORDING");
        }
Exemplo n.º 8
0
 public async void StartRecording()
 {
     try
     {
         _recorder.SetAudioEncodingBitRate(16000);
         _recorder.SetAudioSource(AudioSource.VoiceRecognition);
         _recorder.SetOutputFormat(OutputFormat.ThreeGpp);
         _recorder.SetAudioEncoder(AudioEncoder.AmrNb);
         _recorder.SetOutputFile(path);
         _recorder.Prepare();
         _recorder.Start();
     }
     catch (Exception e)
     {
         var str = e.Message;
     }
 }
Exemplo n.º 9
0
        static void CreateRecorder()
        {
            if (Recorder == null)
            {
                Recorder = new MediaRecorder();
            }
            else
            {
                Recorder.Reset();
            }

            Recorder.SetAudioSource(AudioSource.Mic);
            Recorder.SetOutputFormat(OutputFormat.Mpeg4);
            Recorder.SetAudioEncoder(AudioEncoder.AmrNb);
            Recorder.SetAudioEncodingBitRate(ENCODING_BIT_RATE);
            Recorder.SetAudioSamplingRate(AUDIO_SAMPLING_RATE);
            Recorder.SetOutputFile(Recording.FullName);
            Recorder.Prepare();
        }
Exemplo n.º 10
0
        private bool PrepareMediaRecorder()
        {
            outputPath = new Java.IO.File(Activity.GetExternalFilesDir(null),
                                          DateTime.UtcNow.ToString("MM-dd-yyyy-HH-mm-ss-fff") + ".mp4").AbsolutePath;

            if (File.Exists(outputPath))
            {
                File.Delete(outputPath);
            }

            Camera.Parameters parameters = camera.GetParameters();
            camera.Unlock();
            mediaRecorder = new MediaRecorder();
            mediaRecorder.SetOutputFile(outputPath);
            mediaRecorder.SetCamera(camera);
            mediaRecorder.SetAudioSource(AudioSource.Camcorder);
            mediaRecorder.SetVideoSource(VideoSource.Camera);
            mediaRecorder.SetOutputFormat(OutputFormat.Mpeg4);
            mediaRecorder.SetVideoFrameRate(30);
            mediaRecorder.SetVideoEncodingBitRate(5000000);
            mediaRecorder.SetVideoEncoder(VideoEncoder.H264);
            mediaRecorder.SetAudioEncoder(AudioEncoder.Aac);
            mediaRecorder.SetAudioSamplingRate(44100);
            mediaRecorder.SetAudioEncodingBitRate(96000);
            mediaRecorder.SetVideoSize(parameters.PictureSize.Width, parameters.PictureSize.Height);
            mediaRecorder.SetMaxDuration(600000); // ten mins

            try
            {
                mediaRecorder.Prepare();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                ReleaseMediaRecorder();
                return(false);
            }

            return(true);
        }
        public Task RecordAsync()
        {
            if (isRecording)
            {
                return(Task.CompletedTask);
            }

            return(Task.Run(() =>
            {
                try
                {
                    isRecording = true;
                    filePath = Path.GetTempFileName();

                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }

                    recorder = new MediaRecorder();

                    recorder.Reset();
                    recorder.SetAudioSource(AudioSource.Mic);
                    recorder.SetOutputFormat(OutputFormat.AacAdts);
                    recorder.SetAudioEncoder(AudioEncoder.Aac);
                    recorder.SetAudioEncodingBitRate(32000);
                    recorder.SetAudioSamplingRate(44100);
                    recorder.SetOutputFile(filePath);
                    recorder.Prepare();
                    recorder.Start();
                }
                catch (Exception e)
                {
                    isRecording = false;
                    Console.Out.WriteLine(e.StackTrace);
                }
            }));
        }
        private void StartRecording(int count)
        {
            string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

            path = Path.Combine(path, "myfile" + count + ".amr");

            if (count > 0)
            {
                _recorder.Stop();
                _recorder.Reset();
            }

            _recorder.SetOutputFile(path);

            _recorder.SetAudioSource(AudioSource.Mic);
            _recorder.SetOutputFormat(OutputFormat.AmrWb);
            _recorder.SetAudioEncoder(AudioEncoder.AmrWb);
            _recorder.SetAudioEncodingBitRate(16000);
            _recorder.SetAudioChannels(1);

            _recorder.Prepare();
            _recorder.Start();
        }
Exemplo n.º 13
0
        /**
         * Records a short sample of AAC ADTS from the microphone to find out what the sampling rate really is
         * On some phone indeed, no error will be reported if the sampling rate used differs from the
         * one selected with setAudioSamplingRate
         * @throws IOException
         * @throws IllegalStateException
         */

        private void testADTS()
        {
            setAudioEncoder((int)Android.Media.AudioEncoder.Aac);
            try
            {
                Type mType = typeof(MediaRecorder.OutputFormat);

                if (mType.GetField("AAC_ADTS") == null)
                {
                    return;
                }

                setOutputFormat((int)Android.Media.AudioEncoder.Aac);
            }
            catch (System.Exception ignore)
            {
                setOutputFormat(6);
            }

            System.String key = PREF_PREFIX + "aac-" + mQuality.samplingRate;

            if (mSettings != null && mSettings.Contains(key))
            {
                System.String[] s = mSettings.GetString(key, "").Split(',');
                mQuality.samplingRate = (int)Integer.ValueOf(s[0]);
                mConfig  = (int)Integer.ValueOf(s[1]);
                mChannel = (int)Integer.ValueOf(s[2]);
                return;
            }

            System.String TESTFILE = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/spydroid-test.adts";

            if (!Android.OS.Environment.ExternalStorageState.Equals(Android.OS.Environment.MediaMounted))
            {
                throw new IllegalStateException("No external storage or external storage not ready !");
            }

            // The structure of an ADTS packet is described here: http://wiki.multimedia.cx/index.php?title=ADTS

            // ADTS header is 7 or 9 bytes long
            byte[] buffer = new byte[9];

            mMediaRecorder = new MediaRecorder();
            mMediaRecorder.SetAudioSource((AudioSource)mAudioSource);
            mMediaRecorder.SetOutputFormat((OutputFormat)mOutputFormat);
            mMediaRecorder.SetAudioEncoder((AudioEncoder)mAudioEncoder);
            mMediaRecorder.SetAudioChannels(1);
            mMediaRecorder.SetAudioSamplingRate(mQuality.samplingRate);
            mMediaRecorder.SetAudioEncodingBitRate(mQuality.bitRate);
            mMediaRecorder.SetOutputFile(TESTFILE);
            mMediaRecorder.SetMaxDuration(1000);
            mMediaRecorder.Prepare();
            mMediaRecorder.Start();

            // We record for 1 sec
            // TODO: use the MediaRecorder.OnInfoListener
            try
            {
                Thread.Sleep(2000);
            }
            catch (InterruptedException e) { }

            mMediaRecorder.Stop();
            mMediaRecorder.Release();
            mMediaRecorder = null;

            File             file = new File(TESTFILE);
            RandomAccessFile raf  = new RandomAccessFile(file, "r");

            // ADTS packets start with a sync word: 12bits set to 1
            while (true)
            {
                if ((raf.ReadByte() & 0xFF) == 0xFF)
                {
                    buffer[0] = (byte)raf.ReadByte();
                    if ((buffer[0] & 0xF0) == 0xF0)
                    {
                        break;
                    }
                }
            }

            raf.Read(buffer, 1, 5);

            mSamplingRateIndex    = (buffer[1] & 0x3C) >> 2;
            mProfile              = ((buffer[1] & 0xC0) >> 6) + 1;
            mChannel              = (buffer[1] & 0x01) << 2 | (buffer[2] & 0xC0) >> 6;
            mQuality.samplingRate = AUDIO_SAMPLING_RATES[mSamplingRateIndex];

            // 5 bits for the object type / 4 bits for the sampling rate / 4 bits for the channel / padding
            mConfig = (mProfile & 0x1F) << 11 | (mSamplingRateIndex & 0x0F) << 7 | (mChannel & 0x0F) << 3;

            Log.Info(TAG, "MPEG VERSION: " + ((buffer[0] & 0x08) >> 3));
            Log.Info(TAG, "PROTECTION: " + (buffer[0] & 0x01));
            Log.Info(TAG, "PROFILE: " + AUDIO_OBJECT_TYPES[mProfile]);
            Log.Info(TAG, "SAMPLING FREQUENCY: " + mQuality.samplingRate);
            Log.Info(TAG, "CHANNEL: " + mChannel);

            raf.Close();

            if (mSettings != null)
            {
                ISharedPreferencesEditor editor = mSettings.Edit();
                editor.PutString(key, mQuality.samplingRate + "," + mConfig + "," + mChannel);
                editor.Commit();
            }

            if (!file.Delete())
            {
                Log.Error(TAG, "Temp file could not be erased");
            }
        }