public static async Task<string> PathToVideoFromCamera (MediaPicker mediaPicker)
		{
			try{
				if (!mediaPicker.IsCameraAvailable || !mediaPicker.VideosSupported) {
					return null;
				}

				var mediaFile = await mediaPicker.TakeVideoAsync (new StoreVideoOptions {});

				return mediaFile.Path;
			} catch {
				return null;
			}
		}
		/// <summary>
		/// Event handler when the user clicks the Take a Video button
		/// </summary>
		/// <param name='sender'>
		/// Sender
		/// </param>
		partial void takeVideoBtnClicked (MonoTouch.Foundation.NSObject sender)
		{
			Console.WriteLine("takeVideoBtnClicked");
			
			picker = new MediaPicker ();
			
			// Check if a camera is available and videos are supported on this device
			if (!picker.IsCameraAvailable || !picker.VideosSupported)
			{
				ShowUnsupported();
				return;
			}
			
			// Call TakeVideoAsync, which returns a Task<MediaFile>.
			picker.TakeVideoAsync (new StoreVideoOptions
			{
				Quality = VideoQuality.Medium,
				DesiredLength = new TimeSpan(0, 0, 30)
			})
			.ContinueWith (t => // Continue when the user has finished recording
			{
				if (t.IsCanceled) // The user canceled
					return;
					
				// Play the video the user recorded
				InvokeOnMainThread( delegate {
					moviePlayer = new MPMoviePlayerViewController (NSUrl.FromFilename(t.Result.Path));
					moviePlayer.MoviePlayer.UseApplicationAudioSession = true;
		    		this.PresentMoviePlayerViewController(moviePlayer);
				});
			});
		}
Beispiel #3
0
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);
			SetContentView (Resource.Layout.Main);
			
			Button videoButton = FindViewById<Button> (Resource.Id.takeVideoButton);
			videoButton.Click += delegate {
				// The MediaPicker is the class used to invoke the
				// camera and gallery picker for selecting and
				// taking photos and videos
				var picker = new MediaPicker (this);

				// We can check to make sure the device has a camera
				// and supports dealing with video.
				if (!picker.IsCameraAvailable || !picker.VideosSupported) {
					ShowUnsupported();
					return;
				}
				
				// TakeVideoAsync is an async API that takes a 
				// StoreVideoOptions object with various 
				// properties, such as the name and folder to
				// store the resulting video. You can
				// also limit the length of the video
				picker.TakeVideoAsync (new StoreVideoOptions {
					Name = "MyVideo",
					Directory = "MyVideos",
					DesiredLength = TimeSpan.FromSeconds (10)
				})
				.ContinueWith (t => {
					if (t.IsCanceled)
						return;

					RunOnUiThread (() => ShowVideo (t.Result.Path));
				});
			};
			
			Button photoButton = FindViewById<Button> (Resource.Id.takePhotoButton);
			photoButton.Click += delegate
			{
				var picker = new MediaPicker (this);

				if (!picker.IsCameraAvailable || !picker.PhotosSupported) {
					ShowUnsupported();
					return;
				}

				picker.TakePhotoAsync (new StoreCameraMediaOptions {
					Name = "test.jpg",
					Directory = "MediaPickerSample"
				})
				.ContinueWith (t => {
					if (t.IsCanceled)
						return;

					RunOnUiThread (() => ShowImage (t.Result.Path));
				});
			};
			
			Button pickVideoButton = FindViewById<Button> (Resource.Id.pickVideoButton);
			pickVideoButton.Click += delegate
			{
				// The MediaPicker is the class used to  invoke the camera
				// and gallery picker for selecting and taking photos
				// and videos
				var picker = new MediaPicker (this);
				
				if (!picker.VideosSupported) {
					ShowUnsupported();
					return;
				}

				// PickVideoAsync is an async API that invokes
				// the native gallery
				picker.PickVideoAsync ().ContinueWith (t => {
					if (t.IsCanceled)
						return;

					RunOnUiThread (() => ShowVideo (t.Result.Path));
				});
			};

			Button pickPhotoButton = FindViewById<Button> (Resource.Id.pickPhotoButton);
			pickPhotoButton.Click += delegate {
				var picker = new MediaPicker (this);

				if (!picker.PhotosSupported) {
					ShowUnsupported();
					return;
				}

				picker.PickPhotoAsync ().ContinueWith (t => {
					if (t.IsCanceled)
						return;

					RunOnUiThread (() => ShowImage (t.Result.Path));
				});
			};
		}
Beispiel #4
-1
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);
			SetContentView (Resource.Layout.Main);
			ImageView image = FindViewById<ImageView> (Resource.Id.image);
			VideoView videoView  = FindViewById<VideoView>(Resource.Id.surfacevideoview);
			
			
			//
			// Wire up the take a video button
			//
			Button videoButton = FindViewById<Button> (Resource.Id.takeVideoButton);
			videoButton.Click += delegate
			{
				//
				// The MediaPicker is the class used to 
				// invoke the camera and gallery picker
				// for selecting and taking photos
				// and videos
				//
				var picker = new MediaPicker (this);

				// We can check to make sure the device has a camera
				// and supports dealing with video.
				if (!picker.IsCameraAvailable || !picker.VideosSupported)
				{
					ShowUnsupported();
					return;
				}
				
				//
				// TakeVideoAsync is an async API that takes a 
				// StoreVideoOptions object with various 
				// properties, such as the name and folder to
				// store the resulting video. You can
				// also limit the length of the video
				//
				picker.TakeVideoAsync (new StoreVideoOptions
				{
					Name = "MyVideo",
					Directory = "MyVideos",
					DesiredLength = TimeSpan.FromSeconds (10)
				})
				.ContinueWith (t =>
				{
					if (t.IsCanceled)
						return;

					//
					// Because TakeVideoAsync returns a Task
					// we can use ContinueWith to run more code
					// after the user finishes recording the video
					//
					RunOnUiThread (() =>
					{
						//
						// Toggle the visibility of the image and videoviews
						//
						image.Visibility = Android.Views.ViewStates.Gone;	
						videoView.Visibility = Android.Views.ViewStates.Visible;
						
						//	
						// Load in the video file
						//
						videoView.SetVideoPath(t.Result.Path);
	        
						//
						// optional: Handle when the video finishes playing
						//
	        			//videoView.setOnCompletionListener(this);
	        
						//
						// Start playing the video
						//	
	        			videoView.Start();
					});
				});
			};
			
			//
			// Wire up the take a photo button
			//
			Button photoButton = FindViewById<Button> (Resource.Id.takePhotoButton);
			photoButton.Click += delegate
			{
				var picker = new MediaPicker (this);

				if (!picker.IsCameraAvailable || !picker.PhotosSupported)
				{
					ShowUnsupported();
					return;
				}

				picker.TakePhotoAsync (new StoreCameraMediaOptions
				{
					Name = "test.jpg",
					Directory = "MediaPickerSample"
				})
				.ContinueWith (t =>
				{
					if (t.IsCanceled)
						return;

					Bitmap b = BitmapFactory.DecodeFile (t.Result.Path);
					RunOnUiThread (() =>
					{
						//
						// Toggle the visibility of the image and video views
						//
						videoView.Visibility = Android.Views.ViewStates.Gone;
						image.Visibility = Android.Views.ViewStates.Visible;
							
						//
						// Display the bitmap
						//
						image.SetImageBitmap (b);

						// Cleanup any resources held by the MediaFile instance
						t.Result.Dispose();
					});
				});
			};
			
			//
			// Wire up the pick a video button
			//
			Button pickVideoButton = FindViewById<Button> (Resource.Id.pickVideoButton);
			pickVideoButton.Click += delegate
			{
				//
				// The MediaPicker is the class used to 
				// invoke the camera and gallery picker
				// for selecting and taking photos
				// and videos
				//
				var picker = new MediaPicker (this);
				
				if (!picker.VideosSupported)
				{
					ShowUnsupported();
					return;
				}

				//
				// PickVideoAsync is an async API that invokes
				// the native gallery
				//
				picker.PickVideoAsync ()
				.ContinueWith (t =>
				{
					if (t.IsCanceled)
						return;

					//
					// Because PickVideoAsync returns a Task
					// we can use ContinueWith to run more code
					// after the user finishes recording the video
					//
					RunOnUiThread (() =>
					{
						//
						// Toggle the visibility of the image and video views
						//
						image.Visibility = Android.Views.ViewStates.Gone;	
						videoView.Visibility = Android.Views.ViewStates.Visible;
						
						//	
						// Load in the video file
						//
						videoView.SetVideoPath(t.Result.Path);
	        
						//
						// Optional: Handle when the video finishes playing
						//
	        			//videoView.setOnCompletionListener(this);
	        
						//
						// Start playing the video
						//	
	        			videoView.Start();

						// Cleanup any resources held by the MediaFile instance
						t.Result.Dispose();
					});
				});
			};
			
			//
			// Wire up the pick a photo button
			//
			Button pickPhotoButton = FindViewById<Button> (Resource.Id.pickPhotoButton);
			pickPhotoButton.Click += delegate
			{
				var picker = new MediaPicker (this);

				if (!picker.PhotosSupported)
				{
					ShowUnsupported();
					return;
				}

				picker.PickPhotoAsync ()
				.ContinueWith (t =>
				{
					if (t.IsCanceled)
						return;

					Bitmap b = BitmapFactory.DecodeFile (t.Result.Path);
					RunOnUiThread (() =>
					{
						//
						// Toggle the visibility of the image and video views
						//
						videoView.Visibility = Android.Views.ViewStates.Gone;
						image.Visibility = Android.Views.ViewStates.Visible;
							
						//
						// Display the bitmap
						//
						image.SetImageBitmap (b);

						// Cleanup any resources held by the MediaFile instance
						t.Result.Dispose();
					});
				});
			};
		}