protected override IEnumerable<Datum> Poll(CancellationToken cancellationToken) { MediaRecorder recorder = null; try { recorder = new MediaRecorder(); recorder.SetAudioSource(AudioSource.Mic); recorder.SetOutputFormat(OutputFormat.ThreeGpp); recorder.SetAudioEncoder(AudioEncoder.AmrNb); recorder.SetOutputFile("/dev/null"); recorder.Prepare(); recorder.Start(); // mark start time of amplitude measurement -- MaxAmplitude is always computed from previous call to MaxAmplitude int dummy = recorder.MaxAmplitude; Thread.Sleep(SampleLengthMS); return new Datum[] { new SoundDatum(DateTimeOffset.UtcNow, 20 * Math.Log10(recorder.MaxAmplitude)) }; // http://www.mathworks.com/help/signal/ref/mag2db.html } finally { if (recorder != null) { try { recorder.Stop(); } catch (Exception) { } try { recorder.Release(); } catch (Exception) { } } } }
public void SurfaceCreated(ISurfaceHolder holder) { var outputFile = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), "myvideooutputfile.mp4"); if (!recording) { mediaRecorder = new MediaRecorder(); //correctCamera(); //if (camera != null) // mediaRecorder.SetCamera(camera); mediaRecorder.SetAudioSource(AudioSource.Mic); mediaRecorder.SetVideoSource(VideoSource.Camera); mediaRecorder.SetOutputFormat(OutputFormat.ThreeGpp); mediaRecorder.SetAudioEncoder(AudioEncoder.Aac); mediaRecorder.SetVideoEncoder(VideoEncoder.Mpeg4Sp); if (System.IO.File.Exists(outputFile)) System.IO.File.Delete(outputFile); System.IO.File.Create(outputFile); CamcorderProfile camProf = CamcorderProfile.Get(CamcorderQuality.Low); fps = camProf.VideoFrameRate; mediaRecorder.SetOutputFile(outputFile); mediaRecorder.SetPreviewDisplay(holder.Surface); mediaRecorder.Prepare(); } else { mediaPlayer.SetDisplay(holder); mediaPlayer.SetDataSource(outputFile); mediaPlayer.Prepared += new EventHandler(mediaPlayer_Prepared); mediaPlayer.PrepareAsync(); } }
public bool RecordAudio() { if (string.IsNullOrEmpty(folder) || string.IsNullOrEmpty(directoryname)) { return false; } //--test // get all under root: /* var directories = Directory.EnumerateDirectories("./"); foreach (var directory in directories) { Console.WriteLine(directory); } */ // read text file: /* var text = File.ReadAllText("TestData/ReadMe.txt"); Console.WriteLine(text); */ // xml stream reader: /* using (TextReader reader = new StreamReader("./TestData/test.xml")) { System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(MyObject)); var xml = (MyObject)serializer.Deserialize(reader); } */ try { Directory.CreateDirectory(directoryname); fileName = string.Format("Audio{0}.aac", DateTime.Now.ToString("yyyyMMddHHmmss")); path = Path.Combine(directoryname, fileName); _recorder = new MediaRecorder(); _recorder.Reset(); _recorder.SetAudioSource(AudioSource.Mic); _recorder.SetOutputFormat(OutputFormat.Default); _recorder.SetAudioEncoder(AudioEncoder.Aac); _recorder.SetOutputFile(path); _recorder.Prepare(); _recorder.Start(); return true; } catch (System.Exception ex) { _recorder = null; fileName = string.Empty; var test = ex.Message; return false; } }
public void RecordSound() { _url = StorageService.GenerateFilename(StorageType.Sound, "3gpp"); _recorder = new MediaRecorder(); _recorder.SetAudioSource(AudioSource.Mic); _recorder.SetOutputFormat(OutputFormat.ThreeGpp); _recorder.SetAudioEncoder(AudioEncoder.AmrNb); _recorder.SetOutputFile(_url); _recorder.Prepare(); _recorder.Start(); }
public void SetUpRecoder() { try { if (null == activity) { return; } if (userResponse == UserResponse.Video) { mediaRecorder = new MediaRecorder(); mediaRecorder.Reset(); mediaRecorder.SetAudioSource(AudioSource.Mic); mediaRecorder.SetVideoSource(VideoSource.Surface); mediaRecorder.SetOutputFormat(OutputFormat.Mpeg4); mediaRecorder.SetOutputFile(path + "/test.mp4"); mediaRecorder.SetVideoEncodingBitRate(10000000); mediaRecorder.SetVideoFrameRate(30); mediaRecorder.SetVideoSize(videoSize.Width, videoSize.Height); mediaRecorder.SetVideoEncoder(VideoEncoder.H264); mediaRecorder.SetAudioEncoder(AudioEncoder.Aac); //int rotation = (int)activity.WindowManager.DefaultDisplay.Rotation; //int orientation = ORIENTATIONS.Get(rotation); if (LensFacing == CameraFacing.BACK) { mediaRecorder.SetOrientationHint(90); } else { mediaRecorder.SetOrientationHint(270); } mediaRecorder.SetMaxDuration(30000); mediaRecorder.Prepare(); } else { mediaRecorder = new MediaRecorder(); mediaRecorder.Reset(); mediaRecorder.SetAudioSource(AudioSource.Mic); mediaRecorder.SetOutputFormat(OutputFormat.ThreeGpp); mediaRecorder.SetAudioEncoder(AudioEncoder.AmrNb); mediaRecorder.SetOutputFile(path + "/test.mp3"); mediaRecorder.SetMaxDuration(30000); mediaRecorder.Prepare(); } } catch (System.Exception ex) { } }
protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.video); string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/test.mp4"; var record = FindViewById<Button> (Resource.Id.Record); var stop = FindViewById<Button> (Resource.Id.Stop); var play = FindViewById<Button> (Resource.Id.Play); var video = FindViewById<VideoView> (Resource.Id.SampleVideoView); record.Click += delegate { video.StopPlayback (); recorder = new MediaRecorder (); recorder.SetVideoSource (VideoSource.Camera); recorder.SetAudioSource (AudioSource.Mic); recorder.SetOutputFormat (OutputFormat.Default); recorder.SetVideoEncoder (VideoEncoder.Default); recorder.SetAudioEncoder (AudioEncoder.Default); recorder.SetOutputFile (path); recorder.SetPreviewDisplay (video.Holder.Surface); recorder.Prepare (); recorder.Start (); }; stop.Click += delegate { if (recorder != null) { recorder.Stop (); recorder.Release (); } }; play.Click += delegate { var uri = Android.Net.Uri.Parse (path); video.SetVideoURI (uri); video.Start (); }; }
public AudioRecorder(string filename) { if (filename == "") { #if DEBUG System.Diagnostics.Debug.WriteLine("filename passed in is NULL"); #endif return; } if (System.IO.File.Exists(filename)) System.IO.File.Delete(filename); recorder = new MediaRecorder(); recorder.SetAudioSource(AudioSource.Mic); recorder.SetOutputFormat(OutputFormat.ThreeGpp); recorder.SetAudioEncoder(AudioEncoder.Aac); tempFilename = System.IO.Path.Combine(wowZapp.LaffOutOut.Singleton.ContentDirectory, "tempfile.3gp"); outputFilename = filename; #if (DEBUG) System.Diagnostics.Debug.WriteLine("filename = {0}", filename); #endif f = System.IO.File.Create(tempFilename); recorder.SetOutputFile(tempFilename); recorder.Prepare(); }
private void startRecordingVideo() { if (null == Activity) return; media_recorder = new MediaRecorder (); File file = getVideoFile (Activity); try { //UI button_video.SetText (Resource.String.stop); is_recording_video = true; //Configure the MediaRecorder media_recorder.SetAudioSource (AudioSource.Mic); media_recorder.SetVideoSource (VideoSource.Surface); media_recorder.SetOutputFormat (OutputFormat.Mpeg4); media_recorder.SetOutputFile (System.IO.Path.GetFullPath (file.ToString())); media_recorder.SetVideoEncodingBitRate (10000000); media_recorder.SetVideoFrameRate (30); media_recorder.SetVideoSize (1440, 1080); media_recorder.SetVideoEncoder (VideoEncoder.H264); media_recorder.SetAudioEncoder (AudioEncoder.Aac); int rotation = (int)Activity.WindowManager.DefaultDisplay.Rotation; int orientation = ORIENTATIONS.Get (rotation); media_recorder.SetOrientationHint (orientation); media_recorder.Prepare (); Surface surface = media_recorder.Surface; //Set up CaptureRequest builder = camera_device.CreateCaptureRequest (CameraTemplate.Record); builder.AddTarget (surface); var preview_surface = new Surface (texture_view.SurfaceTexture); builder.AddTarget (preview_surface); var surface_list = new List<Surface>(); surface_list.Add(surface); surface_list.Add(preview_surface); camera_device.CreateCaptureSession(surface_list,new RecordingCaptureStateListener(this),null); } catch (IOException e) { e.PrintStackTrace (); } catch (CameraAccessException e) { e.PrintStackTrace (); } catch (IllegalStateException e) { e.PrintStackTrace (); } }
protected override void OnCreate(Bundle bundle) { base.OnCreate (bundle); // Set our view from the "main" layout resource SetContentView (Resource.Layout.Memory); ImageButton record = FindViewById<ImageButton> (Resource.Id.vid); ImageButton btnNormalDialog = FindViewById<ImageButton> (Resource.Id.img); _imageView = FindViewById<ImageView> (Resource.Id.imgv); btnNormalDialog.Click += ButtonOnClick; mToolbar = FindViewById<SupportToolbar>(Resource.Id.toolbar); mDrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout); mLeftDrawer = FindViewById<ListView>(Resource.Id.left_drawer); mLeftDrawer.Tag = 0; SetSupportActionBar(mToolbar); mLeftDataSet = new List<string>(); mLeftDataSet.Add ("Left Item 1"); mLeftDataSet.Add ("Left Item 2"); mLeftAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mLeftDataSet); mLeftDrawer.Adapter = mLeftAdapter; mDrawerToggle = new MyActionBarDrawerToggle( this, //Host Activity mDrawerLayout, //DrawerLayout Resource.String.openDrawer, //Opened Message Resource.String.closeDrawer //Closed Message ); mDrawerLayout.SetDrawerListener(mDrawerToggle); SupportActionBar.SetHomeButtonEnabled(true); SupportActionBar.SetDisplayShowTitleEnabled(true); mDrawerToggle.SyncState(); if (bundle != null) { if (bundle.GetString("DrawerState") == "Opened") { SupportActionBar.SetTitle(Resource.String.openDrawer); } else { SupportActionBar.SetTitle(Resource.String.closeDrawer); } } else { //This is the first the time the activity is ran SupportActionBar.SetTitle(Resource.String.closeDrawer); } VideoView video = FindViewById<VideoView> (Resource.Id.vidv); _button = FindViewById<ImageButton> (Resource.Id.mica); string path1 = Environment.ExternalStorageDirectory.AbsolutePath + "/test.mp4"; _button.Click += delegate { if (count == 1) { _recorder.SetAudioSource (AudioSource.Mic); _recorder.SetOutputFormat (OutputFormat.ThreeGpp); _recorder.SetAudioEncoder (AudioEncoder.AmrNb); _recorder.SetOutputFile (path1); _recorder.Prepare (); _recorder.Start (); Toast toast = Toast.MakeText (this , "Audio Recording", ToastLength.Short); toast.Show (); count ++; } else if (count == 2) { _recorder.Stop(); _recorder.Release (); count = 0; Toast toast = Toast.MakeText (this , "Recording Stopped", ToastLength.Short); toast.Show (); } }; record.Click += delegate { video.StopPlayback (); if(c1 == 1) { _recorder = new MediaRecorder (); _recorder.SetVideoSource (VideoSource.Camera); _recorder.SetAudioSource (AudioSource.Mic); _recorder.SetOutputFormat (OutputFormat.Default); _recorder.SetVideoEncoder (VideoEncoder.Default); _recorder.SetAudioEncoder (AudioEncoder.Default); _recorder.SetOutputFile (path1); _recorder.SetPreviewDisplay (video.Holder.Surface); _recorder.Prepare (); _recorder.Start (); c1++; } else if(c1 == 2){ if (_recorder != null) { _recorder.Stop (); _recorder.Release (); c1 = 0; } } }; }
private bool prepareVideoRecorder() { // BEGIN_INCLUDE (configure_preview) mCamera = CameraHelper.getDefaultCameraInstance(); // We need to make sure that our preview and recording video size are supported by the // camera. Query camera to find all the sizes and choose the optimal size given the // dimensions of our preview surface. Camera.Parameters parameters = mCamera.GetParameters(); IList <Camera.Size> mSupportedPreviewSizes = parameters.SupportedPreviewSizes; IList <Camera.Size> mSupportedVideoSizes = parameters.SupportedVideoSizes; Camera.Size optimalSize = CameraHelper.getOptimalVideoSize(mSupportedVideoSizes, mSupportedPreviewSizes, mPreview.Width, mPreview.Height); // Use the same size for recording profile. CamcorderProfile profile = CamcorderProfile.Get(CamcorderQuality.High); profile.VideoFrameWidth = optimalSize.Width; profile.VideoFrameHeight = optimalSize.Height; // likewise for the camera object itself. parameters.SetPreviewSize(profile.VideoFrameWidth, profile.VideoFrameHeight); mCamera.SetParameters(parameters); try { // Requires API level 11+, For backward compatibility use {@link setPreviewDisplay} // with {@link SurfaceView} mCamera.SetPreviewTexture(mPreview.SurfaceTexture); } catch (IOException e) { Log.Error(TAG, "Surface texture is unavailable or unsuitable" + e.Message); return(false); } // END_INCLUDE (configure_preview) // BEGIN_INCLUDE (configure_media_recorder) mMediaRecorder = new Android.Media.MediaRecorder(); // Step 1: Unlock and set camera to MediaRecorder mCamera.Unlock(); mMediaRecorder.SetCamera(mCamera); // Step 2: Set sources mMediaRecorder.SetAudioSource(AudioSource.Default); mMediaRecorder.SetVideoSource(VideoSource.Camera); // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) mMediaRecorder.SetProfile(profile); // Step 4: Set output file mOutputFile = CameraHelper.getOutputMediaFile(CameraHelper.MEDIA_TYPE_VIDEO); if (mOutputFile == null) { return(false); } mMediaRecorder.SetOutputFile(mOutputFile.Path); // END_INCLUDE (configure_media_recorder) // Step 5: Prepare configured MediaRecorder try { mMediaRecorder.Prepare(); } catch (IllegalStateException e) { Log.Debug(TAG, "IllegalStateException preparing MediaRecorder: " + e.Message); releaseMediaRecorder(); return(false); } catch (IOException e) { Log.Debug(TAG, "IOException preparing MediaRecorder: " + e.Message); releaseMediaRecorder(); return(false); } return(true); }
public void SurfaceCreated(ISurfaceHolder holder) { mediaRecord = new MediaRecorder (); mediaRecord.SetAudioSource (AudioSource.Mic); mediaRecord.SetVideoSource (VideoSource.Camera); mediaRecord.SetOutputFormat (OutputFormat.Default); mediaRecord.SetAudioEncoder (AudioEncoder.Default); mediaRecord.SetVideoEncoder (VideoEncoder.Default); System.IO.File.Create (videoFilename); mediaRecord.SetOutputFile (videoFilename); mediaRecord.SetPreviewDisplay (holder.Surface); mediaRecord.Prepare (); }
public void StartRecording() { var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); _recordedFileName = documents + File.PathSeparator + Guid.NewGuid() + ".mp4"; ClearMediaRecorder(); _mediaRecorder = new MediaRecorder(); _mediaRecorder.SetAudioSource(AudioSource.Default); _mediaRecorder.SetOutputFormat(OutputFormat.Mpeg4); _mediaRecorder.SetAudioEncoder(AudioEncoder.Aac); _mediaRecorder.SetOutputFile(_recordedFileName); _mediaRecorder.Prepare(); _mediaRecorder.Start(); }
//PREPARAMOS NUESTRO MEDIA RECORD Y CAMARA private bool PrepararVideoRecorder() { //OBTENEMOS LA INSTANCIA DE NUESTRA CAMARA YA PREPARADA _camera = ObtenerInstanciaCamara(); //INICIALIZAMOS NUESTRA VARIABLE MEDIARECORDER _mediaRecorder = new MediaRecorder(); //LE INDICAMOS A NUESTRA CAMARA QUE CAMBIE DE ROTACION ESTO ES DEVIDO QUE POR DEFUALT NOS MUESTRA EN POSICION HORIZONTAL, //Y COMO DEFINIMOS QUE LA POSICION DE NUESTRA APP SEA Portrait, REALIZAMOS EL CAMBIO. _camera.SetDisplayOrientation (90); //ABRIMOS NUESTRA CAMARA PARA SER USADA _camera.Unlock(); //DE IGUAL FORMA QUE NUESTRA CAMARA CAMBIAMOS LA POSICION DE NUESTRA MEDIARECORDER _mediaRecorder.SetOrientationHint (90); //ASIGNAMOS LA CAMARA A NUESTRA MEDIARECORDER _mediaRecorder.SetCamera(_camera); //ASIGNAMOS LOS FORMATOS DE VIDEO Y AUDIO _mediaRecorder.SetAudioSource(AudioSource.Camcorder); _mediaRecorder.SetVideoSource(VideoSource.Camera); //RECUPERAMOS EL PERFIL QUE TIENE NUESTRA CAMARA PARA PODER ASIGNARSELA A NUESTRA MEDIARECORDER var camcorderProfile = ((int)Build.VERSION.SdkInt) >= 9 ? CamcorderProfile.Get(0, CamcorderQuality.High) : CamcorderProfile.Get(CamcorderQuality.High); //ASIGNAMOS EL PERFIL A NUESTRO MEDIARECORDER _mediaRecorder.SetProfile(camcorderProfile); //LE ASIGNAMOS EL PATH DONDE SE ENCUESTRA NUESTRO ARCHIVO DE VIDEO PARA PODER CREARLO _mediaRecorder.SetOutputFile(PathArchivoVideo ()); //ASIGNAMOS EL SURFACE A NUESTRO MEDIARECORDER QUE UTILIZARA PARA VISUALIZAR LO QUE ESTAMOS GRABANDO _mediaRecorder.SetPreviewDisplay(_videoView.Holder.Surface); try { //CONFIRMAMOS LOS CAMBIOS HECHOS EN NUESTRO MEDIA RECORDER PARA PODER INICIAR A GRABAR _mediaRecorder.Prepare(); return true; } catch { //SI OCURRE ALGUN PROBLEMA LIBRAMOS LOS RECURSOS ASIGNADOS A NUESTRO MEDIARECORDER LiberarMediaRecorder(); return false; } }
//Used for entering the Mandarin_ma speech processing screen void HandleMandarin_Ma_Male_Select() { SetContentView (Resource.Layout.Mandarin_Mother_Male); string RecordPath = "/sdcard/Recording.mp3"; //string RecordPath = Environment.getExternalStorageDirectory () // .getAbsolutePath () + "/Recording.mp3"; //string RecordPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/Recording.3gpp"; //string RecordPath = this.file.getAbsolutePath().substring(8) Button PlayStock = FindViewById<Button> (Resource.Id.PlayMa); PlayBack = FindViewById<Button> (Resource.Id.PlayBack); Button BackToMandarin = FindViewById<Button> (Resource.Id.BackToMandarin); RecordVoice = FindViewById<Button> (Resource.Id.Record); StopRecord = FindViewById<Button> (Resource.Id.Stop); StockPlayer = MediaPlayer.Create(this, Resource.Raw.mother); _recorder = new MediaRecorder (); _player = new MediaPlayer (); //Click Event Handlers PlayStock.Click += delegate { StockPlayer.Start(); }; RecordVoice.Click += delegate { RecordVoice.Enabled = !RecordVoice.Enabled; StopRecord.Enabled = !StopRecord.Enabled; if (PlayBack.Enabled==true){ PlayBack.Enabled = false; } _recorder.SetAudioSource (AudioSource.Mic); _recorder.SetOutputFormat (OutputFormat.Mpeg4); _recorder.SetOutputFile(RecordPath); _recorder.SetAudioEncoder (AudioEncoder.Aac); _recorder.Prepare(); _recorder.Start (); }; //Stops recording StopRecord.Click += delegate { StopRecord.Enabled =! StopRecord.Enabled; if (PlayBack.Enabled==false){ PlayBack.Enabled = true; } _recorder.Stop (); _recorder.Reset (); /* For instant playback _player.SetDataSource (RecordPath); _player.Prepare (); _player.Start (); */ RecordVoice.Enabled = !RecordVoice.Enabled; }; PlayBack.Click += delegate { RecordPlayer = new MediaPlayer (); RecordPlayer.SetDataSource (RecordPath); RecordPlayer.Prepare (); RecordPlayer.Start (); }; BackToMandarin.Click += delegate { HandleMandarinSelect (); }; }