Skip to content
This repository has been archived by the owner on Apr 18, 2019. It is now read-only.

Commit

Permalink
implemented video capture, added video test to capture.html
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrebnov committed Sep 19, 2011
1 parent 71a3565 commit 08a5a35
Show file tree
Hide file tree
Showing 13 changed files with 722 additions and 24 deletions.
Binary file added framework/Images/appbar.feature.video.rest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added framework/Images/appbar.save.rest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added framework/Images/appbar.stop.rest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
147 changes: 131 additions & 16 deletions framework/PhoneGap/Commands/Capture.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Tasks;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;
using Microsoft.Xna.Framework.Media;
using Microsoft.Phone;
using System.Windows.Media.Imaging;
using Microsoft.Phone;
using Microsoft.Phone.Tasks;
using Microsoft.Xna.Framework.Media;
using WP7GapClassLib.PhoneGap.UI;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using AudioResult = WP7GapClassLib.PhoneGap.UI.AudioCaptureTask.AudioResult;
using VideoResult = WP7GapClassLib.PhoneGap.UI.VideoCaptureTask.VideoResult;

namespace WP7GapClassLib.PhoneGap.Commands
{
Expand Down Expand Up @@ -66,6 +56,24 @@ public static CaptureAudioOptions Default
}
}

/// <summary>
/// Represents captureVideo action options.
/// </summary>
[DataContract]
public class CaptureVideoOptions
{
/// <summary>
/// The maximum number of video files the device user can capture in a single capture operation. The value must be greater than or equal to 1 (defaults to 1).
/// </summary>
[DataMember(IsRequired = false, Name = "limit")]
public int Limit { get; set; }

public static CaptureVideoOptions Default
{
get { return new CaptureVideoOptions() { Limit = 1 }; }
}
}

/// <summary>
/// Represents getFormatData action options.
/// </summary>
Expand Down Expand Up @@ -130,7 +138,6 @@ public MediaFile(string filePath, Stream stream)
{
this.LastModifiedDate = storage.GetLastWriteTime(filePath).DateTime.ToString();
}

}
}

Expand Down Expand Up @@ -182,6 +189,11 @@ public MediaFileData(WriteableBitmap image)
/// </summary>
protected CaptureAudioOptions captureAudioOptions;

/// <summary>
/// Capture Video options
/// </summary>
protected CaptureVideoOptions captureVideoOptions;

/// <summary>
/// Used to open camera application
/// </summary>
Expand All @@ -192,6 +204,11 @@ public MediaFileData(WriteableBitmap image)
/// </summary>
private AudioCaptureTask audioCaptureTask;

/// <summary>
/// Used for video recording
/// </summary>
private VideoCaptureTask videoCaptureTask;

/// <summary>
/// Stores information about captured files
/// </summary>
Expand Down Expand Up @@ -259,6 +276,37 @@ public void captureAudio(string options)
}
}

/// <summary>
/// Launches our own video recording control to capture video
/// </summary>
/// <param name="options">may contains additional parameters</param>
public void captureVideo(string options)
{
try
{
try
{
this.captureVideoOptions = String.IsNullOrEmpty(options) ?
CaptureVideoOptions.Default : JSON.JsonHelper.Deserialize<CaptureVideoOptions>(options);

}
catch (Exception ex)
{
this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, ex.Message));
return;
}

videoCaptureTask = new VideoCaptureTask();
videoCaptureTask.Completed += videoRecordingTask_Completed;
videoCaptureTask.Show();

}
catch (Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message));
}
}

/// <summary>
/// Retrieves the format information of the media file.
/// </summary>
Expand Down Expand Up @@ -468,6 +516,73 @@ private void audioRecordingTask_Completed(object sender, AudioResult e)
}
}

/// <summary>
/// Handles result of video recording tasks
/// </summary>
/// <param name="sender"></param>
/// <param name="e">stores information about current captured video</param>
private void videoRecordingTask_Completed(object sender, VideoResult e)
{

if (e.Error != null)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR));
return;
}

switch (e.TaskResult)
{
case TaskResult.OK:
try
{
// Get image data
MediaFile data = new MediaFile(e.VideoFileName, e.VideoFile);

this.files.Add(data);

if (files.Count < this.captureVideoOptions.Limit)
{
videoCaptureTask.Show();
}
else
{
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, files));
files.Clear();
}
}
catch (Exception ex)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error capturing video."));
}
break;

case TaskResult.Cancel:
if (files.Count > 0)
{
// User canceled operation, but some video clips were made
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, files));
files.Clear();
}
else
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Canceled."));
}
break;

default:
if (files.Count > 0)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, files));
files.Clear();
}
else
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Did not complete!"));
}
break;
}
}

/// <summary>
/// Extract file from Isolated Storage as WriteableBitmap object
/// </summary>
Expand Down
95 changes: 95 additions & 0 deletions framework/PhoneGap/UI/VideoCaptureTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2011, Sergey Grebnov.
*/

using System;
using System.IO;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace WP7GapClassLib.PhoneGap.UI
{
/// <summary>
/// Allows an application to launch the Video Recording application.
/// Use this to allow users to record video from your application.
/// </summary>
public class VideoCaptureTask
{
/// <summary>
/// Represents recorded video returned from a call to the Show method of
/// a WP7GapClassLib.PhoneGap.Controls.VideoCaptureTask object
/// </summary>
public class VideoResult : TaskEventArgs
{
/// <summary>
/// Initializes a new instance of the VideoResult class.
/// </summary>
public VideoResult()
{ }

/// <summary>
/// Initializes a new instance of the VideoResult class
/// with the specified Microsoft.Phone.Tasks.TaskResult.
/// </summary>
/// <param name="taskResult">Associated Microsoft.Phone.Tasks.TaskResult</param>
public VideoResult(TaskResult taskResult)
: base(taskResult)
{ }

/// <summary>
/// Gets the file name of the recorded Video.
/// </summary>
public Stream VideoFile { get; internal set; }

/// <summary>
/// Gets the stream containing the data for the recorded Video.
/// </summary>
public string VideoFileName { get; internal set; }
}

/// <summary>
/// Occurs when a Video recording task is completed.
/// </summary>
public event EventHandler<VideoResult> Completed;

/// <summary>
/// Shows Video Recording application
/// </summary>
public void Show()
{

var root = Application.Current.RootVisual as PhoneApplicationFrame;

root.Navigated += new System.Windows.Navigation.NavigatedEventHandler(NavigationService_Navigated);

// dummy parameter is used to always open a fresh version
root.Navigate(new System.Uri("/WP7GapClassLib;component/PhoneGap/UI/VideoRecorder.xaml?dummy=" + Guid.NewGuid().ToString(), UriKind.Relative));
}

/// <summary>
/// Performs additional configuration of the recording application.
/// </summary>
private void NavigationService_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
if (!(e.Content is VideoRecorder)) return;

(Application.Current.RootVisual as PhoneApplicationFrame).Navigated -= NavigationService_Navigated;

VideoRecorder VideoRecorder = (VideoRecorder)e.Content;

if (VideoRecorder != null)
{
VideoRecorder.Completed += this.Completed;
}
else if (this.Completed != null)
{
this.Completed(this, new VideoResult(TaskResult.Cancel));
}
}

}
}
34 changes: 34 additions & 0 deletions framework/PhoneGap/UI/VideoRecorder.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<phone:PhoneApplicationPage
x:Class="WP7GapClassLib.PhoneGap.UI.VideoRecorder"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="480"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Landscape" Orientation="LandscapeLeft"
shell:SystemTray.IsVisible="False">

<Canvas x:Name="LayoutRoot" Background="Transparent" Grid.ColumnSpan="1" Grid.Column="0">

<Rectangle
x:Name="viewfinderRectangle"
Width="640"
Height="480"
HorizontalAlignment="Left"
Canvas.Left="80"/>

</Canvas>

<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" x:Name="PhoneAppBar" Opacity="0.0">
<shell:ApplicationBarIconButton IconUri="/Images/appbar.feature.video.rest.png" Text="Record" x:Name="btnStartRecording" Click="StartRecording_Click" />
<shell:ApplicationBarIconButton IconUri="/Images/appbar.save.rest.png" Text="Take" x:Name="btnTakeVideo" Click="TakeVideo_Click"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

</phone:PhoneApplicationPage>
Loading

0 comments on commit 08a5a35

Please sign in to comment.