Exemplo n.º 1
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.º 2
0
        protected void StartRecord()
        {
            string fileName = DateTime.Now.ToString("MM-dd-yyyy-HHmmss");

            recordFile = fileName + ".mp3";

            stop.Enabled  = !stop.Enabled;
            start.Enabled = !start.Enabled;

            aTimer          = new Timer();
            aTimer.Interval = 1000;
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Enabled  = true;
            aTimer.Start();

            recorder = new MediaRecorder();
            recorder.SetAudioSource(AudioSource.Mic);
            recorder.SetOutputFormat(OutputFormat.Mpeg4);
            recorder.SetAudioEncoder(AudioEncoder.Aac);
            recorder.SetOutputFile(recordFolder + "/" + recordFile);

            recorder.SetMaxFileSize(0);
            recorder.SetMaxDuration(0);

            recorder.Prepare();
            recorder.Start();
        }
        public void StartRecord(string filename)
        {
            if (recorder != null)
            {
                recorder.Reset();
            }
            else
            {
                recorder = new MediaRecorder();
            }
            mCamera.Lock();
            mCamera.Unlock();

            recorder.SetCamera(mCamera);
            recorder.SetVideoSource(VideoSource.Camera);
            recorder.SetAudioSource(AudioSource.Camcorder);

            try
            {
                //try for full hd
                recorder.SetProfile(CamcorderProfile.Get(CURRENTCAMERA, CamcorderQuality.Q1080p));
            }
            catch
            {
                try
                {
                    recorder.SetProfile(CamcorderProfile.Get(CURRENTCAMERA, CamcorderQuality.Q720p));
                }
                catch
                {
                    try
                    {
                        //if not hd, try for highest phone can give
                        recorder.SetProfile(CamcorderProfile.Get(CURRENTCAMERA, CamcorderQuality.High));
                    }
                    catch
                    {
                        //if not, then set to low
                        recorder.SetProfile(CamcorderProfile.Get(CURRENTCAMERA, CamcorderQuality.Q480p));
                    }
                }
            }

            //recorder.SetAudioEncoder(AudioEncoder.Aac);
            //recorder.SetVideoEncoder(VideoEncoder.H264);
            recorder.SetPreviewDisplay(mPreview.Holder.Surface);
            recorder.SetOutputFile(filename);
            recorder.SetMaxDuration(1000 * 60 * 5);

            try
            {
                recorder.Prepare();
                recorder.Start();
            }
            catch (Exception e)
            {
                OnError?.Invoke(e.ToString());
            }
        }
 private void prepare()
 {
     if (recorder == null)
     {
         recorder = new MediaRecorder();
     }
     recorder.Reset();
     if (camera != null)
     {
         camera.Unlock();
         recorder.SetCamera(camera);
     }
     recorder.SetVideoSource(VideoSource.Camera);
     recorder.SetAudioSource(AudioSource.Camcorder);
     recorder.SetOutputFormat(OutputFormat.Mpeg4);
     recorder.SetVideoEncoder(VideoEncoder.H264);
     recorder.SetAudioEncoder(AudioEncoder.Aac);
     recorder.SetOrientationHint(90);
     recorder.SetOutputFile(savePath);
     recorder.SetMaxDuration(10 * 1000);
     recorder.SetMaxFileSize(5 * 1000 * 1000);
     recorder.SetVideoEncodingBitRate(2 * 1024 * 1024);
     if (supportFrameRate > 0)
     {
         recorder.SetVideoFrameRate(supportFrameRate);
     }
     //recorder.SetPreviewDisplay(vv.Holder.Surface);
     if (videoSizeList != null && videoSizeList.Count > 0)
     {
         recorder.SetVideoSize(videoSizeList[bestIndex].Width, videoSizeList[bestIndex].Height);
     }
     try
     {
         recorder.Prepare();
     }
     catch (Exception e)
     {
         Console.Out.WriteLine("===prepare error:" + e);
     }
 }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        // Should not be called by the UI thread
        private MP4Config testMediaRecorderAPI()
        {
            System.String key = PREF_PREFIX + "h264-mr-" + mRequestedQuality.framerate + "," + mRequestedQuality.resX + "," + mRequestedQuality.resY;

            if (mSettings != null && mSettings.Contains(key))
            {
                System.String[] s = mSettings.GetString(key, "").Split(',');
                return(new MP4Config(s[0], s[1], s[2]));
            }

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

            System.String TESTFILE = Android.OS.Environment.ExternalStorageDirectory.ToString() + "/spydroid-test.mp4";

            Log.Info(TAG, "Testing H264 support... Test file saved at: " + TESTFILE);

            try {
                File file = new File(TESTFILE);
                file.CreateNewFile();
            } catch (IOException e) {
                throw new StorageUnavailableException(e.Message);
            }

            // Save flash state & set it to false so that led remains off while testing h264
            bool savedFlashState = mFlashEnabled;

            mFlashEnabled = false;

            bool previewStarted = mPreviewStarted;

            bool cameraOpen = mCamera != null;

            createCamera();

            // Stops the preview if needed
            if (mPreviewStarted)
            {
                lockCamera();
                try {
                    mCamera.stopPreview();
                } catch (Exception e) {}
                mPreviewStarted = false;
            }

            try {
                Thread.Sleep(100);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.PrintStackTrace();
            }

            unlockCamera();

            try {
                mMediaRecorder = new MediaRecorder();
                mMediaRecorder.SetCamera(mCamera);
                mMediaRecorder.SetVideoSource(VideoSource.Camera);
                mMediaRecorder.SetOutputFormat(OutputFormat.ThreeGpp);
                mMediaRecorder.SetVideoEncoder((VideoEncoder)mVideoEncoder);
                mMediaRecorder.SetPreviewDisplay(mSurfaceView.Holder.Surface);
                mMediaRecorder.SetVideoSize(mRequestedQuality.resX, mRequestedQuality.resY);
                mMediaRecorder.SetVideoFrameRate(mRequestedQuality.framerate);
                mMediaRecorder.SetVideoEncodingBitRate((int)(mRequestedQuality.bitrate * 0.8));
                mMediaRecorder.SetOutputFile(TESTFILE);
                mMediaRecorder.SetMaxDuration(3000);

                // We wait a little and stop recording
                mMediaRecorder.SetOnInfoListener(this);

                /*mMediaRecorder.SetOnInfoListener(new MediaRecorder.IOnInfoListener() {
                 *                  public void onInfo(MediaRecorder mr, int what, int extra) {
                 *                          Log.d(TAG,"MediaRecorder callback called !");
                 *                          if (what==MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
                 *                                  Log.d(TAG,"MediaRecorder: MAX_DURATION_REACHED");
                 *                          } else if (what==MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) {
                 *                                  Log.d(TAG,"MediaRecorder: MAX_FILESIZE_REACHED");
                 *                          } else if (what==MediaRecorder.MEDIA_RECORDER_INFO_UNKNOWN) {
                 *                                  Log.d(TAG,"MediaRecorder: INFO_UNKNOWN");
                 *                          } else {
                 *                                  Log.d(TAG,"WTF ?");
                 *                          }
                 *                          mLock.release();
                 *                  }
                 *          });*/

                // Start recording
                mMediaRecorder.Prepare();
                mMediaRecorder.Start();

                if (mLock.TryAcquire(6, TimeUnit.Seconds))
                {
                    Log.d(TAG, "MediaRecorder callback was called :)");
                    Thread.Sleep(400);
                }
                else
                {
                    Log.d(TAG, "MediaRecorder callback was not called after 6 seconds... :(");
                }
            } catch (IOException e) {
                throw new ConfNotSupportedException(e.Message);
            } catch (RuntimeException e) {
                throw new ConfNotSupportedException(e.Message);
            } catch (InterruptedException e) {
                e.PrintStackTrace();
            } finally {
                try {
                    mMediaRecorder.stop();
                } catch (Java.Lang.Exception e) {}
                mMediaRecorder.Release();
                mMediaRecorder = null;
                lockCamera();
                if (!cameraOpen)
                {
                    destroyCamera();
                }
                // Restore flash state
                mFlashEnabled = savedFlashState;
                if (previewStarted)
                {
                    // If the preview was started before the test, we try to restart it.
                    try {
                        startPreview();
                    } catch (Java.Lang.Exception e) {}
                }
            }

            // Retrieve SPS & PPS & ProfileId with MP4Config
            MP4Config config = new MP4Config(TESTFILE);

            // Delete dummy video
            File file = new File(TESTFILE);

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

            Log.i(TAG, "H264 Test succeded...");

            // Save test result
            if (mSettings != null)
            {
                ISharedPreferencesEditor editor = mSettings.Edit();
                editor.PutString(key, config.getProfileLevel() + "," + config.getB64SPS() + "," + config.getB64PPS());
                editor.Commit();
            }

            return(config);
        }
Exemplo n.º 7
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");
            }
        }
Exemplo n.º 8
0
        private void initRecorder()
        {
            recorder = new MediaRecorder();
            path     = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/PincidentReport.mp4";

            recorder.SetCamera(camera);
            string manufacturer = Build.Manufacturer;

            if (ActivityCompat.CheckSelfPermission(CurrentContext, Manifest.Permission.RecordAudio) != Permission.Granted)
            {
                ActivityCompat.RequestPermissions(CurrentContext, new System.String[] { Manifest.Permission.RecordAudio },
                                                  10);
            }
            else
            {
                if (manufacturer.ToLower().Contains("samsung"))
                {
                    recorder.SetAudioSource(AudioSource.VoiceCommunication);
                }
                else
                {
                    recorder.SetAudioSource(AudioSource.Default);
                }
            }

            recorder.SetVideoSource(VideoSource.Camera);

            recorder.SetOutputFile(path);

            recorder.SetOnInfoListener(this);
            recorder.SetPreviewDisplay(videoView.Holder.Surface);
            recorder.SetOutputFormat(OutputFormat.Default);
            var SupportedVideoFrameRateByCamera = camera.GetParameters().SupportedPreviewFrameRates;
            var SmallToBigFrameRates            = SupportedVideoFrameRateByCamera.Reverse();

            var maxFrameRate = SmallToBigFrameRates.First().IntValue();

            recorder.SetVideoFrameRate(maxFrameRate);



            var SupportedSizesByCamera = camera.GetParameters().SupportedVideoSizes;

            var SmallToBigSizesList = SupportedSizesByCamera.Reverse();

            Android.Hardware.Camera.Size size = new Android.Hardware.Camera.Size(camera, 0, 0);


            foreach (var videoSize in SmallToBigSizesList) // The closest Height to 480
            {
                if (videoSize.Height == 480)
                {
                    size = videoSize;
                    break;
                }
                else if (videoSize.Height > 480)
                {
                    size = videoSize;
                    break;
                }
            }

            if (size.Height == 0)
            {
                size = SmallToBigSizesList.ElementAt(0);
            }

            recorder.SetVideoSize(size.Width, size.Height);

            recorder.SetVideoEncoder(VideoEncoder.Mpeg4Sp);// MPEG_4_SP
            recorder.SetAudioEncoder(AudioEncoder.Aac);

            recorder.SetMaxDuration(15000);

            recorder.SetOrientationHint(90);
        }