Update ReactVideoView.cs

This commit is contained in:
Eric Rozell 2016-11-10 15:04:01 -08:00
parent 5af9e26233
commit abefb3ae12

View File

@ -17,9 +17,6 @@ namespace ReactNativeVideo
private readonly DispatcherTimer _timer; private readonly DispatcherTimer _timer;
private bool _isSourceSet;
private string _uri;
private bool _isLoopingEnabled; private bool _isLoopingEnabled;
private bool _isPaused; private bool _isPaused;
private bool _isMuted; private bool _isMuted;
@ -39,9 +36,22 @@ namespace ReactNativeVideo
{ {
set set
{ {
_uri = value; var uri = value;
base.Source = MediaSource.CreateFromUri(new Uri(_uri));
_isSourceSet = true; base.Source = MediaSource.CreateFromUri(new Uri(uri));
this.GetReactContext()
.GetNativeModule<UIManagerModule>()
.EventDispatcher
.DispatchEvent(
new ReactVideoEvent(
ReactVideoEventType.LoadStart.GetEventName(),
this.GetTag(),
new JObject
{
{ "src", uri }
}));
ApplyModifiers(); ApplyModifiers();
SubscribeEvents(); SubscribeEvents();
} }
@ -52,9 +62,10 @@ namespace ReactNativeVideo
set set
{ {
_isLoopingEnabled = value; _isLoopingEnabled = value;
if (_isSourceSet) var mediaPlayer = MediaPlayer;
if (mediaPlayer != null)
{ {
MediaPlayer.IsLoopingEnabled = _isLoopingEnabled; mediaPlayer.IsLoopingEnabled = _isLoopingEnabled;
} }
} }
} }
@ -64,9 +75,10 @@ namespace ReactNativeVideo
set set
{ {
_isMuted = value; _isMuted = value;
if (_isSourceSet) var mediaPlayer = MediaPlayer;
if (mediaPlayer != null)
{ {
MediaPlayer.IsMuted = _isMuted; mediaPlayer.IsMuted = _isMuted;
} }
} }
} }
@ -76,15 +88,16 @@ namespace ReactNativeVideo
set set
{ {
_isPaused = value; _isPaused = value;
if (_isSourceSet) var mediaPlayer = MediaPlayer;
if (mediaPlayer != null)
{ {
if (_isPaused) if (_isPaused)
{ {
MediaPlayer.Pause(); mediaPlayer.Pause();
} }
else else
{ {
MediaPlayer.Play(); mediaPlayer.Play();
} }
} }
} }
@ -95,9 +108,10 @@ namespace ReactNativeVideo
set set
{ {
_isUserControlEnabled = value; _isUserControlEnabled = value;
if (_isSourceSet) var mediaPlayer = MediaPlayer;
if (mediaPlayer != null)
{ {
MediaPlayer.SystemMediaTransportControls.IsEnabled = _isUserControlEnabled; mediaPlayer.SystemMediaTransportControls.IsEnabled = _isUserControlEnabled;
} }
} }
} }
@ -107,9 +121,10 @@ namespace ReactNativeVideo
set set
{ {
_volume = value; _volume = value;
if (_isSourceSet) var mediaPlayer = MediaPlayer;
if (mediaPlayer != null)
{ {
MediaPlayer.Volume = _volume; mediaPlayer.Volume = _volume;
} }
} }
} }
@ -119,9 +134,10 @@ namespace ReactNativeVideo
set set
{ {
_rate = value; _rate = value;
if (_isSourceSet) var mediaPlayer = MediaPlayer;
if (mediaPlayer != null)
{ {
MediaPlayer.PlaybackSession.PlaybackRate = _rate; mediaPlayer.PlaybackSession.PlaybackRate = _rate;
} }
} }
} }
@ -136,23 +152,24 @@ namespace ReactNativeVideo
public void Seek(double seek) public void Seek(double seek)
{ {
if (_isSourceSet) var mediaPlayer = MediaPlayer;
if (mediaPlayer != null)
{ {
MediaPlayer.PlaybackSession.Position = TimeSpan.FromSeconds(seek); mediaPlayer.PlaybackSession.Position = TimeSpan.FromSeconds(seek);
} }
} }
public void Dispose() public void Dispose()
{ {
if (_isSourceSet) var mediaPlayer = MediaPlayer;
if (mediaPlayer != null)
{ {
_timer.Tick -= OnTick; _timer.Tick -= OnTick;
MediaPlayer.SourceChanged -= OnSourceChanged; mediaPlayer.MediaOpened -= OnMediaOpened;
MediaPlayer.MediaOpened -= OnMediaOpened; mediaPlayer.MediaFailed -= OnMediaFailed;
MediaPlayer.MediaFailed -= OnMediaFailed; mediaPlayer.MediaEnded -= OnMediaEnded;
MediaPlayer.MediaEnded -= OnMediaEnded; mediaPlayer.PlaybackSession.BufferingStarted -= OnBufferingStarted;
MediaPlayer.PlaybackSession.BufferingStarted -= OnBufferingStarted; mediaPlayer.PlaybackSession.BufferingEnded -= OnBufferingEnded;
MediaPlayer.PlaybackSession.BufferingEnded -= OnBufferingEnded;
MediaPlayer.PlaybackSession.SeekCompleted -= OnSeekCompleted; MediaPlayer.PlaybackSession.SeekCompleted -= OnSeekCompleted;
} }
@ -172,18 +189,19 @@ namespace ReactNativeVideo
private void SubscribeEvents() private void SubscribeEvents()
{ {
_timer.Tick += OnTick; _timer.Tick += OnTick;
MediaPlayer.SourceChanged += OnSourceChanged; var mediaPlayer = MediaPlayer;
MediaPlayer.MediaOpened += OnMediaOpened; mediaPlayer.MediaOpened += OnMediaOpened;
MediaPlayer.MediaFailed += OnMediaFailed; mediaPlayer.MediaFailed += OnMediaFailed;
MediaPlayer.MediaEnded += OnMediaEnded; mediaPlayer.MediaEnded += OnMediaEnded;
MediaPlayer.PlaybackSession.BufferingStarted += OnBufferingStarted; mediaPlayer.PlaybackSession.BufferingStarted += OnBufferingStarted;
MediaPlayer.PlaybackSession.BufferingEnded += OnBufferingEnded; mediaPlayer.PlaybackSession.BufferingEnded += OnBufferingEnded;
MediaPlayer.PlaybackSession.SeekCompleted += OnSeekCompleted; mediaPlayer.PlaybackSession.SeekCompleted += OnSeekCompleted;
} }
private void OnTick(object sender, object e) private void OnTick(object sender, object e)
{ {
if (_isSourceSet && !_isCompleted && !_isPaused) var mediaPlayer = MediaPlayer;
if (mediaPlayer != null && !_isCompleted && !_isPaused)
{ {
this.GetReactContext() this.GetReactContext()
.GetNativeModule<UIManagerModule>() .GetNativeModule<UIManagerModule>()
@ -194,33 +212,18 @@ namespace ReactNativeVideo
this.GetTag(), this.GetTag(),
new JObject new JObject
{ {
{ "currentTime", MediaPlayer.PlaybackSession.Position.TotalSeconds }, { "currentTime", mediaPlayer.PlaybackSession.Position.TotalSeconds },
{ "playableDuration", 0.0 /* TODO */ } { "playableDuration", 0.0 /* TODO */ }
})); }));
} }
} }
private void OnSourceChanged(MediaPlayer sender, object args)
{
this.GetReactContext()
.GetNativeModule<UIManagerModule>()
.EventDispatcher
.DispatchEvent(
new ReactVideoEvent(
ReactVideoEventType.LoadStart.GetEventName(),
this.GetTag(),
new JObject
{
{ "src", this._uri }
}));
}
private void OnMediaOpened(MediaPlayer sender, object args) private void OnMediaOpened(MediaPlayer sender, object args)
{ {
RunOnDispatcher(delegate RunOnDispatcher(delegate
{ {
var width = MediaPlayer.PlaybackSession.NaturalVideoWidth; var width = sender.PlaybackSession.NaturalVideoWidth;
var height = MediaPlayer.PlaybackSession.NaturalVideoHeight; var height = sender.PlaybackSession.NaturalVideoHeight;
var orientation = (width > height) ? "landscape" : "portrait"; var orientation = (width > height) ? "landscape" : "portrait";
var size = new JObject var size = new JObject
{ {
@ -229,18 +232,25 @@ namespace ReactNativeVideo
{ "orientation", orientation } { "orientation", orientation }
}; };
this.GetReactContext().GetNativeModule<UIManagerModule>().EventDispatcher.DispatchEvent(new ReactVideoView.ReactVideoEvent(ReactVideoEventType.Load.GetEventName(), this.GetTag(), new JObject this.GetReactContext()
{ .GetNativeModule<UIManagerModule>()
{ "duration", MediaPlayer.PlaybackSession.NaturalDuration.TotalSeconds }, .EventDispatcher
{ "currentTime", MediaPlayer.PlaybackSession.Position.TotalSeconds }, .DispatchEvent(
{ "naturalSize", size }, new ReactVideoEvent(
{ "canPlayFastForward", false }, ReactVideoEventType.Load.GetEventName(),
{ "canPlaySlowForward", false }, this.GetTag(),
{ "canPlaySlow", false }, new JObject
{ "canPlayReverse", false }, {
{ "canStepBackward", false }, { "duration", sender.PlaybackSession.NaturalDuration.TotalSeconds },
{ "canStepForward", false } { "currentTime", sender.PlaybackSession.Position.TotalSeconds },
})); { "naturalSize", size },
{ "canPlayFastForward", false },
{ "canPlaySlowForward", false },
{ "canPlaySlow", false },
{ "canPlayReverse", false },
{ "canStepBackward", false },
{ "canStepForward", false }
}));
}); });
} }