2016-11-09 12:31:42 -07:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using ReactNative.UIManager;
|
|
|
|
|
using ReactNative.UIManager.Events;
|
|
|
|
|
using System;
|
|
|
|
|
using Windows.ApplicationModel.Core;
|
|
|
|
|
using Windows.Media.Core;
|
|
|
|
|
using Windows.Media.Playback;
|
|
|
|
|
using Windows.UI.Core;
|
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
using Windows.UI.Xaml.Controls;
|
|
|
|
|
|
|
|
|
|
namespace ReactNativeVideo
|
|
|
|
|
{
|
|
|
|
|
class ReactVideoView : MediaPlayerElement, IDisposable
|
|
|
|
|
{
|
|
|
|
|
public const string EVENT_PROP_SEEK_TIME = "seekTime";
|
|
|
|
|
|
|
|
|
|
private readonly DispatcherTimer _timer;
|
|
|
|
|
|
|
|
|
|
private bool _isLoopingEnabled;
|
|
|
|
|
private bool _isPaused;
|
|
|
|
|
private bool _isMuted;
|
|
|
|
|
private bool _isUserControlEnabled;
|
|
|
|
|
private bool _isCompleted;
|
|
|
|
|
private double _volume;
|
|
|
|
|
private double _rate;
|
|
|
|
|
|
|
|
|
|
public ReactVideoView()
|
|
|
|
|
{
|
|
|
|
|
_timer = new DispatcherTimer();
|
|
|
|
|
_timer.Interval = TimeSpan.FromMilliseconds(250.0);
|
|
|
|
|
_timer.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new string Source
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-11-10 16:04:01 -07:00
|
|
|
|
var uri = value;
|
|
|
|
|
|
|
|
|
|
base.Source = MediaSource.CreateFromUri(new Uri(uri));
|
|
|
|
|
|
|
|
|
|
this.GetReactContext()
|
|
|
|
|
.GetNativeModule<UIManagerModule>()
|
|
|
|
|
.EventDispatcher
|
|
|
|
|
.DispatchEvent(
|
|
|
|
|
new ReactVideoEvent(
|
|
|
|
|
ReactVideoEventType.LoadStart.GetEventName(),
|
|
|
|
|
this.GetTag(),
|
|
|
|
|
new JObject
|
|
|
|
|
{
|
|
|
|
|
{ "src", uri }
|
|
|
|
|
}));
|
|
|
|
|
|
2016-11-09 12:31:42 -07:00
|
|
|
|
ApplyModifiers();
|
|
|
|
|
SubscribeEvents();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsLoopingEnabled
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_isLoopingEnabled = value;
|
2016-11-10 16:04:01 -07:00
|
|
|
|
var mediaPlayer = MediaPlayer;
|
|
|
|
|
if (mediaPlayer != null)
|
2016-11-09 12:31:42 -07:00
|
|
|
|
{
|
2016-11-10 16:04:01 -07:00
|
|
|
|
mediaPlayer.IsLoopingEnabled = _isLoopingEnabled;
|
2016-11-09 12:31:42 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsMuted
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_isMuted = value;
|
2016-11-10 16:04:01 -07:00
|
|
|
|
var mediaPlayer = MediaPlayer;
|
|
|
|
|
if (mediaPlayer != null)
|
2016-11-09 12:31:42 -07:00
|
|
|
|
{
|
2016-11-10 16:04:01 -07:00
|
|
|
|
mediaPlayer.IsMuted = _isMuted;
|
2016-11-09 12:31:42 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsPaused
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_isPaused = value;
|
2016-11-10 16:04:01 -07:00
|
|
|
|
var mediaPlayer = MediaPlayer;
|
|
|
|
|
if (mediaPlayer != null)
|
2016-11-09 12:31:42 -07:00
|
|
|
|
{
|
|
|
|
|
if (_isPaused)
|
|
|
|
|
{
|
2016-11-10 16:04:01 -07:00
|
|
|
|
mediaPlayer.Pause();
|
2016-11-09 12:31:42 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-11-10 16:04:01 -07:00
|
|
|
|
mediaPlayer.Play();
|
2016-11-09 12:31:42 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsUserControlEnabled
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_isUserControlEnabled = value;
|
2016-11-10 16:04:01 -07:00
|
|
|
|
var mediaPlayer = MediaPlayer;
|
|
|
|
|
if (mediaPlayer != null)
|
2016-11-09 12:31:42 -07:00
|
|
|
|
{
|
2016-11-10 16:04:01 -07:00
|
|
|
|
mediaPlayer.SystemMediaTransportControls.IsEnabled = _isUserControlEnabled;
|
2016-11-09 12:31:42 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double Volume
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_volume = value;
|
2016-11-10 16:04:01 -07:00
|
|
|
|
var mediaPlayer = MediaPlayer;
|
|
|
|
|
if (mediaPlayer != null)
|
2016-11-09 12:31:42 -07:00
|
|
|
|
{
|
2016-11-10 16:04:01 -07:00
|
|
|
|
mediaPlayer.Volume = _volume;
|
2016-11-09 12:31:42 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double Rate
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_rate = value;
|
2016-11-10 16:04:01 -07:00
|
|
|
|
var mediaPlayer = MediaPlayer;
|
|
|
|
|
if (mediaPlayer != null)
|
2016-11-09 12:31:42 -07:00
|
|
|
|
{
|
2016-11-10 16:04:01 -07:00
|
|
|
|
mediaPlayer.PlaybackSession.PlaybackRate = _rate;
|
2016-11-09 12:31:42 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double ProgressUpdateInterval
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_timer.Interval = TimeSpan.FromSeconds(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Seek(double seek)
|
|
|
|
|
{
|
2016-11-10 16:04:01 -07:00
|
|
|
|
var mediaPlayer = MediaPlayer;
|
|
|
|
|
if (mediaPlayer != null)
|
2016-11-09 12:31:42 -07:00
|
|
|
|
{
|
2016-11-10 16:04:01 -07:00
|
|
|
|
mediaPlayer.PlaybackSession.Position = TimeSpan.FromSeconds(seek);
|
2016-11-09 12:31:42 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2016-11-10 16:04:01 -07:00
|
|
|
|
var mediaPlayer = MediaPlayer;
|
|
|
|
|
if (mediaPlayer != null)
|
2016-11-09 12:31:42 -07:00
|
|
|
|
{
|
|
|
|
|
_timer.Tick -= OnTick;
|
2016-11-10 16:04:01 -07:00
|
|
|
|
mediaPlayer.MediaOpened -= OnMediaOpened;
|
|
|
|
|
mediaPlayer.MediaFailed -= OnMediaFailed;
|
|
|
|
|
mediaPlayer.MediaEnded -= OnMediaEnded;
|
|
|
|
|
mediaPlayer.PlaybackSession.BufferingStarted -= OnBufferingStarted;
|
|
|
|
|
mediaPlayer.PlaybackSession.BufferingEnded -= OnBufferingEnded;
|
2016-11-09 12:31:42 -07:00
|
|
|
|
MediaPlayer.PlaybackSession.SeekCompleted -= OnSeekCompleted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_timer.Stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ApplyModifiers()
|
|
|
|
|
{
|
|
|
|
|
IsLoopingEnabled = _isLoopingEnabled;
|
|
|
|
|
IsMuted = _isMuted;
|
|
|
|
|
IsPaused = _isPaused;
|
|
|
|
|
IsUserControlEnabled = _isUserControlEnabled;
|
|
|
|
|
Volume = _volume;
|
|
|
|
|
Rate = _rate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SubscribeEvents()
|
|
|
|
|
{
|
|
|
|
|
_timer.Tick += OnTick;
|
2016-11-10 16:04:01 -07:00
|
|
|
|
var mediaPlayer = MediaPlayer;
|
|
|
|
|
mediaPlayer.MediaOpened += OnMediaOpened;
|
|
|
|
|
mediaPlayer.MediaFailed += OnMediaFailed;
|
|
|
|
|
mediaPlayer.MediaEnded += OnMediaEnded;
|
|
|
|
|
mediaPlayer.PlaybackSession.BufferingStarted += OnBufferingStarted;
|
|
|
|
|
mediaPlayer.PlaybackSession.BufferingEnded += OnBufferingEnded;
|
|
|
|
|
mediaPlayer.PlaybackSession.SeekCompleted += OnSeekCompleted;
|
2016-11-09 12:31:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnTick(object sender, object e)
|
|
|
|
|
{
|
2016-11-10 16:04:01 -07:00
|
|
|
|
var mediaPlayer = MediaPlayer;
|
|
|
|
|
if (mediaPlayer != null && !_isCompleted && !_isPaused)
|
2016-11-09 12:31:42 -07:00
|
|
|
|
{
|
|
|
|
|
this.GetReactContext()
|
|
|
|
|
.GetNativeModule<UIManagerModule>()
|
|
|
|
|
.EventDispatcher
|
|
|
|
|
.DispatchEvent(
|
|
|
|
|
new ReactVideoEvent(
|
2016-11-10 16:04:01 -07:00
|
|
|
|
ReactVideoEventType.Progress.GetEventName(),
|
2016-11-09 12:31:42 -07:00
|
|
|
|
this.GetTag(),
|
|
|
|
|
new JObject
|
|
|
|
|
{
|
2016-11-10 16:04:01 -07:00
|
|
|
|
{ "currentTime", mediaPlayer.PlaybackSession.Position.TotalSeconds },
|
2016-11-09 12:31:42 -07:00
|
|
|
|
{ "playableDuration", 0.0 /* TODO */ }
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnMediaOpened(MediaPlayer sender, object args)
|
|
|
|
|
{
|
|
|
|
|
RunOnDispatcher(delegate
|
|
|
|
|
{
|
2016-11-10 16:04:01 -07:00
|
|
|
|
var width = sender.PlaybackSession.NaturalVideoWidth;
|
|
|
|
|
var height = sender.PlaybackSession.NaturalVideoHeight;
|
2016-11-09 12:31:42 -07:00
|
|
|
|
var orientation = (width > height) ? "landscape" : "portrait";
|
|
|
|
|
var size = new JObject
|
|
|
|
|
{
|
|
|
|
|
{ "width", width },
|
|
|
|
|
{ "height", height },
|
|
|
|
|
{ "orientation", orientation }
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-10 16:04:01 -07:00
|
|
|
|
this.GetReactContext()
|
|
|
|
|
.GetNativeModule<UIManagerModule>()
|
|
|
|
|
.EventDispatcher
|
|
|
|
|
.DispatchEvent(
|
|
|
|
|
new ReactVideoEvent(
|
|
|
|
|
ReactVideoEventType.Load.GetEventName(),
|
|
|
|
|
this.GetTag(),
|
|
|
|
|
new JObject
|
|
|
|
|
{
|
|
|
|
|
{ "duration", sender.PlaybackSession.NaturalDuration.TotalSeconds },
|
|
|
|
|
{ "currentTime", sender.PlaybackSession.Position.TotalSeconds },
|
|
|
|
|
{ "naturalSize", size },
|
|
|
|
|
{ "canPlayFastForward", false },
|
|
|
|
|
{ "canPlaySlowForward", false },
|
|
|
|
|
{ "canPlaySlow", false },
|
|
|
|
|
{ "canPlayReverse", false },
|
|
|
|
|
{ "canStepBackward", false },
|
|
|
|
|
{ "canStepForward", false }
|
|
|
|
|
}));
|
2016-11-09 12:31:42 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnMediaFailed(MediaPlayer sender, MediaPlayerFailedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
var errorData = new JObject
|
|
|
|
|
{
|
|
|
|
|
{ "what", args.Error.ToString() },
|
|
|
|
|
{ "extra", args.ErrorMessage }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.GetReactContext()
|
|
|
|
|
.GetNativeModule<UIManagerModule>()
|
|
|
|
|
.EventDispatcher
|
|
|
|
|
.DispatchEvent(
|
|
|
|
|
new ReactVideoEvent(
|
|
|
|
|
ReactVideoEventType.Error.GetEventName(),
|
2016-11-10 16:04:01 -07:00
|
|
|
|
this.GetTag(),
|
2016-11-09 12:31:42 -07:00
|
|
|
|
new JObject
|
|
|
|
|
{
|
|
|
|
|
{ "error", errorData }
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnMediaEnded(MediaPlayer sender, object args)
|
|
|
|
|
{
|
|
|
|
|
_isCompleted = true;
|
|
|
|
|
this.GetReactContext()
|
|
|
|
|
.GetNativeModule<UIManagerModule>()
|
|
|
|
|
.EventDispatcher
|
|
|
|
|
.DispatchEvent(
|
|
|
|
|
new ReactVideoEvent(
|
2016-11-10 16:04:01 -07:00
|
|
|
|
ReactVideoEventType.End.GetEventName(),
|
2016-11-09 12:31:42 -07:00
|
|
|
|
this.GetTag(),
|
|
|
|
|
null));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnBufferingStarted(MediaPlaybackSession sender, object args)
|
|
|
|
|
{
|
|
|
|
|
this.GetReactContext()
|
|
|
|
|
.GetNativeModule<UIManagerModule>()
|
|
|
|
|
.EventDispatcher
|
|
|
|
|
.DispatchEvent(
|
|
|
|
|
new ReactVideoEvent(
|
|
|
|
|
ReactVideoEventType.Stalled.GetEventName(),
|
|
|
|
|
this.GetTag(),
|
|
|
|
|
new JObject()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnBufferingEnded(MediaPlaybackSession sender, object args)
|
|
|
|
|
{
|
|
|
|
|
this.GetReactContext()
|
|
|
|
|
.GetNativeModule<UIManagerModule>()
|
|
|
|
|
.EventDispatcher
|
|
|
|
|
.DispatchEvent(
|
|
|
|
|
new ReactVideoEvent(
|
|
|
|
|
ReactVideoEventType.Resume.GetEventName(),
|
|
|
|
|
this.GetTag(),
|
|
|
|
|
new JObject()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnSeekCompleted(MediaPlaybackSession sender, object args)
|
|
|
|
|
{
|
|
|
|
|
this.GetReactContext()
|
|
|
|
|
.GetNativeModule<UIManagerModule>()
|
|
|
|
|
.EventDispatcher.DispatchEvent(
|
|
|
|
|
new ReactVideoEvent(
|
2016-11-10 16:04:01 -07:00
|
|
|
|
ReactVideoEventType.Seek.GetEventName(),
|
2016-11-09 12:31:42 -07:00
|
|
|
|
this.GetTag(),
|
|
|
|
|
new JObject()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async void RunOnDispatcher(DispatchedHandler action)
|
|
|
|
|
{
|
2016-11-10 14:50:50 -07:00
|
|
|
|
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, action).AsTask().ConfigureAwait(false);
|
2016-11-09 12:31:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ReactVideoEvent : Event
|
|
|
|
|
{
|
|
|
|
|
private readonly string _eventName;
|
|
|
|
|
private readonly JObject _eventData;
|
|
|
|
|
|
2016-11-10 16:04:01 -07:00
|
|
|
|
public ReactVideoEvent(string eventName, int viewTag, JObject eventData)
|
2017-12-24 04:16:02 -07:00
|
|
|
|
: base(viewTag)
|
2016-11-09 12:31:42 -07:00
|
|
|
|
{
|
|
|
|
|
_eventName = eventName;
|
|
|
|
|
_eventData = eventData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string EventName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _eventName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool CanCoalesce
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Dispatch(RCTEventEmitter eventEmitter)
|
|
|
|
|
{
|
|
|
|
|
eventEmitter.receiveEvent(ViewTag, EventName, _eventData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|