/// <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); }); }); }
public void CaptureVideo(string date, Action Ready) { var picker = new MediaPicker(); picker.TakeVideoAsync(new StoreVideoOptions { Name = date + ".mp4", Directory = "TemporaryFiles" }).ContinueWith(t => { ALAssetsLibrary library = new ALAssetsLibrary(); library.WriteVideoToSavedPhotosAlbum(new NSUrl(t.Result.Path), (assetUrl, error) => {}); SaveFileToMedialibrary(t.Result, ".mp4", () => Ready()); }, TaskScheduler.FromCurrentSynchronizationContext()); }
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(); }); }); }; }
public override bool FinishedLaunching(UIApplication app, NSDictionary options) { pickPhoto = new StringElement("Pick Photo"); pickPhoto.Tapped += () => { mediaPicker.PickPhotoAsync().ContinueWith(t => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) { return; } // We get back a MediaFile MediaFile media = t.Result; ShowPhoto(media); }, uiScheduler); // Make sure we use the UI thread to show our photo. }; takePhoto = new StringElement("Take Photo"); takePhoto.Tapped += () => { // Make sure we actually have a camera if (!mediaPicker.IsCameraAvailable) { ShowUnsupported(); return; } // When capturing new media, we can specify it's name and location mediaPicker.TakePhotoAsync(new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample" }) .ContinueWith(t => { if (t.IsCanceled || t.IsFaulted) { return; } ShowPhoto(t.Result); }, uiScheduler); }; takeVideo = new StringElement("Take Video"); takeVideo.Tapped += () => { // Make sure video is supported and a camera is available if (!mediaPicker.VideosSupported || !mediaPicker.IsCameraAvailable) { ShowUnsupported(); return; } // When capturing video, we can hint at the desired quality and length. // DesiredLength is only a hint, however, and the resulting video may // be longer than desired. mediaPicker.TakeVideoAsync(new StoreVideoOptions { Quality = VideoQuality.Medium, DesiredLength = TimeSpan.FromSeconds(10), Directory = "MediaPickerSample", Name = "test.mp4" }) .ContinueWith(t => { if (t.IsCanceled || t.IsFaulted) { return; } ShowVideo(t.Result); }, uiScheduler); }; pickVideo = new StringElement("Pick Video"); pickVideo.Tapped += () => { if (!mediaPicker.VideosSupported) { ShowUnsupported(); return; } mediaPicker.PickVideoAsync().ContinueWith(t => { if (t.IsCanceled || t.IsFaulted) { return; } ShowVideo(t.Result); }, uiScheduler); }; var root = new RootElement("Xamarin.Media Sample") { new Section("Picking media") { pickPhoto, pickVideo }, new Section("Capturing media") { takePhoto, takeVideo } }; dialogController = new DisposingMediaViewController(root); viewController = new UINavigationController(dialogController); window = new UIWindow(UIScreen.MainScreen.Bounds); window.RootViewController = viewController; window.MakeKeyAndVisible(); return(true); }
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)); }); }; }