Here is a workaround. The following code is a Phonegap command that implements video play back functionality.
using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using WP7GapClassLib.PhoneGap;
using WP7GapClassLib.PhoneGap.Commands;
using WP7GapClassLib.PhoneGap.JSON;
namespace PhoneGap.Extension.Commands
{
/// <summary>
/// Implements video play back functionality.
/// </summary>
public class Video : BaseCommand
{
/// <summary>
/// Video player object
/// </summary>
private MediaElement _player;
[DataContract]
public class VideoOptions
{
/// <summary>
/// Path to video file
/// </summary>
[DataMember(Name = "src")]
public string Src { get; set; }
}
public void Play(string args)
{
VideoOptions options = JsonHelper.Deserialize<VideoOptions>(args);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
try
{
_Play(options.Src);
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
catch (Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message));
}
});
}
private void _Play(string filePath)
{
// this.player is a MediaElement, it must be added to the visual tree in order to play
PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
if (frame != null)
{
PhoneApplicationPage page = frame.Content as PhoneApplicationPage;
if (page != null)
{
Grid grid = page.FindName("LayoutRoot") as Grid;
if (grid != null && _player == null)
{
_player = new MediaElement();
grid.Children.Add(this._player);
_player.Visibility = Visibility.Visible;
}
}
}
Uri uri = new Uri(filePath, UriKind.RelativeOrAbsolute);
if (uri.IsAbsoluteUri)
{
_player.Source = uri;
}
else
{
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoFile.FileExists(filePath))
{
using (
IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filePath, FileMode.Open,
isoFile))
{
_player.SetSource(stream);
}
}
else
{
throw new ArgumentException("Source doesn't exist");
}
}
}
_player.Play();
}
}
}
There is only the Play function here, but it can be extended to support Stop/Pause/Close ect.
To register this command on client side:
<script type="text/javascript">
function playVideo(src) {
PhoneGap.exec( //PhoneGap.exec = function(success, fail, service, action, args)
null, //success
null, //fail
"Video", //service
"Play", //action
{src: src} //args
);
};
</script>
To play back the file:
<a href="#" class="btn" onClick="playVideo('/app/www/dizzy.mp4');">Play</a>
Pay attention to the path '/app/www/dizzy.mp4'.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…