Skip to content

Instantly share code, notes, and snippets.

@Alphapage
Created October 24, 2012 12:25
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Alphapage/3945799 to your computer and use it in GitHub Desktop.
Basic Video Recorder control in monodroid MvvmCross (add to Tutorial.UI sample and navigate to the ViewModel)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/Tutorial.UI.Droid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<include
android:id="@+id/videoRecorder"
layout="@layout/videorecorder" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/Tutorial.UI.Droid"
android:id="@+id/videoRecorder"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
local:MvxBind="{&apos;Text&apos;:{&apos;Path&apos;:&apos;FileName&apos;}}" />
<Button
android:text="Start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonStart" />
<VideoView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/videoViewRecorder" />
<Button
android:text="Stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonStop" />
</LinearLayout>
using Android.App;
using Cirrious.MvvmCross.Binding.Droid.Views;
using Tutorial.Core.ViewModels.Lessons;
using Android.Widget;
using Android.Hardware;
using Android.Media;
namespace Tutorial.UI.Droid.Views.Lessons
{
[Activity]
public class VideoRecorderView
: MvxBindingActivityView<VideoRecorderViewModel>
{
Camera camera;
MediaRecorder mediaRecorder;
protected override void OnViewModelSet()
{
SetContentView(Resource.Layout.Page_VideoRecorderView);
Button buttonStart = ((Button)FindViewById<Button>(Resource.Id.buttonStart));
buttonStart.Click += new System.EventHandler(buttonStart_Click);
}
void buttonStart_Click(object sender, System.EventArgs e)
{
VideoView videoViewRecorder = ((VideoView)FindViewById<VideoView>(Resource.Id.videoViewRecorder));
if (mediaRecorder != null)
mediaRecorder.Reset();
else
mediaRecorder = new MediaRecorder();
if (camera == null)
{
camera = Camera.Open();
camera.Unlock();
}
mediaRecorder.SetCamera(camera);
mediaRecorder.SetAudioSource(AudioSource.Camcorder);
mediaRecorder.SetVideoSource(VideoSource.Camera);
CamcorderProfile cpHigh = CamcorderProfile.Get(CamcorderQuality.High);
mediaRecorder.SetProfile(cpHigh);
mediaRecorder.SetPreviewDisplay(videoViewRecorder.Holder.Surface);
System.Diagnostics.Debug.WriteLine("path:" + Android.OS.Environment.ExternalStorageDirectory + "/" + ViewModel.FileName + ".mp4");
mediaRecorder.SetOutputFile(Android.OS.Environment.ExternalStorageDirectory + "/" + ViewModel.FileName + ".mp4");
mediaRecorder.SetMaxDuration(50000); // 50 seconds
mediaRecorder.SetMaxFileSize(5000000); // Approximately 5 megabytes
mediaRecorder.Prepare();
mediaRecorder.Start();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cirrious.MvvmCross.ViewModels;
using System.Windows.Input;
using Cirrious.MvvmCross.Commands;
namespace Tutorial.Core.ViewModels.Lessons
{
public class VideoRecorderViewModel
: MvxViewModel
{
private string fileName="test";
public string FileName
{
get { return fileName; }
set { fileName = value; RaisePropertyChanged(() => FileName); }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment