2 using System.Globalization;
3 using Xamarin.Forms.PlatformConfiguration.TizenSpecific;
5 using XLabel = Xamarin.Forms.Label;
6 using XTextAlignment = Xamarin.Forms.TextAlignment;
7 using PlayerState = Xamarin.Forms.PlatformConfiguration.TizenSpecific.PlaybackState;
9 namespace Xamarin.Forms.Platform.Tizen.Native
11 public class EmbeddingControls : ContentView
13 static readonly string PlayImagePath = "XSF.Resources.img_button_play.png";
14 static readonly string PauseImagePath = "XSF.Resources.img_button_pause.png";
16 public ImageButton PlayImage { get; private set; }
17 public ImageButton PauseImage { get; private set; }
19 public EmbeddingControls()
21 PlayImage = new ImageButton
23 Source = ImageSource.FromResource(PlayImagePath, typeof(EmbeddingControls).Assembly),
26 PlayImage.Clicked += OnImageButtonClicked;
27 AbsoluteLayout.SetLayoutFlags(PlayImage, AbsoluteLayoutFlags.All);
28 AbsoluteLayout.SetLayoutBounds(PlayImage, new Rectangle(0.5, 0.5, 0.25, 0.25));
30 PauseImage = new ImageButton
32 Source = ImageSource.FromResource(PauseImagePath, typeof(EmbeddingControls).Assembly),
35 PauseImage.Clicked += OnImageButtonClicked;
36 AbsoluteLayout.SetLayoutFlags(PauseImage, AbsoluteLayoutFlags.All);
37 AbsoluteLayout.SetLayoutBounds(PauseImage, new Rectangle(0.5, 0.5, 0.25, 0.25));
39 var bufferingLabel = new XLabel
41 FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label), false),
42 HorizontalTextAlignment = XTextAlignment.Center,
43 TextColor = Color.FromHex("#eeeeeeee")
45 bufferingLabel.SetBinding(XLabel.TextProperty, new Binding
47 Path = "BufferingProgress",
48 StringFormat = "{0:0%}"
50 bufferingLabel.SetBinding(IsVisibleProperty, new Binding
54 AbsoluteLayout.SetLayoutFlags(bufferingLabel, AbsoluteLayoutFlags.All);
55 AbsoluteLayout.SetLayoutBounds(bufferingLabel, new Rectangle(0.5, 0.5, 0.25, 0.25));
57 var progressBoxView = new BoxView
59 Color = Color.FromHex($"#4286f4")
61 progressBoxView.SetBinding(AbsoluteLayout.LayoutBoundsProperty, new Binding
64 Converter = new ProgressToBoundTextConverter()
66 AbsoluteLayout.SetLayoutFlags(progressBoxView, AbsoluteLayoutFlags.All);
68 var posLabel = new XLabel
70 Margin = new Thickness(10, 0, 0, 0),
71 FontSize = Device.GetNamedSize(NamedSize.Micro, typeof(XLabel)),
72 HorizontalTextAlignment = XTextAlignment.Start
74 posLabel.SetBinding(XLabel.TextProperty, new Binding
77 Converter = new MillisecondToTextConverter()
79 AbsoluteLayout.SetLayoutFlags(posLabel, AbsoluteLayoutFlags.All);
80 AbsoluteLayout.SetLayoutBounds(posLabel, new Rectangle(0,0,1,1));
82 var durationLabel = new XLabel
84 Margin = new Thickness(0, 0, 10, 0),
85 FontSize = Device.GetNamedSize(NamedSize.Micro, typeof(XLabel)),
86 HorizontalTextAlignment = XTextAlignment.End
88 durationLabel.SetBinding(XLabel.TextProperty, new Binding
91 Converter = new MillisecondToTextConverter()
93 AbsoluteLayout.SetLayoutFlags(durationLabel, AbsoluteLayoutFlags.All);
94 AbsoluteLayout.SetLayoutBounds(durationLabel, new Rectangle(0, 0, 1, 1));
96 var progressInnerLayout = new AbsoluteLayout
98 HorizontalOptions = LayoutOptions.FillAndExpand,
100 BackgroundColor = Color.FromHex("#80000000"),
109 var progressLayout = new StackLayout
113 new StackLayout { VerticalOptions = LayoutOptions.FillAndExpand },
116 Margin = Device.Idiom == TargetIdiom.Watch ? new Thickness(80, 0, 80, 0) : 20,
117 VerticalOptions = LayoutOptions.End,
118 HorizontalOptions = LayoutOptions.FillAndExpand,
119 BackgroundColor = Color.FromHex("#50000000"),
120 Children = { progressInnerLayout }
124 AbsoluteLayout.SetLayoutFlags(progressLayout, AbsoluteLayoutFlags.All);
125 AbsoluteLayout.SetLayoutBounds(progressLayout, new Rectangle(0, 0, 1, 1));
127 Content = new AbsoluteLayout
129 HorizontalOptions = LayoutOptions.FillAndExpand,
130 VerticalOptions = LayoutOptions.FillAndExpand,
140 protected override void OnBindingContextChanged()
142 base.OnBindingContextChanged();
143 if (BindingContext is IMediaPlayer player)
145 player.PlaybackPaused += OnPlaybackStateChanged;
146 player.PlaybackStarted += OnPlaybackStateChanged;
147 player.PlaybackStopped += OnPlaybackStateChanged;
151 async void OnPlaybackStateChanged(object sender, EventArgs e)
153 if (BindingContext is IMediaPlayer player)
155 if (player.State == PlayerState.Playing)
157 var unused = PlayImage.FadeTo(0, 100);
158 await PlayImage.ScaleTo(3.0, 300);
159 PlayImage.IsVisible = false;
160 PlayImage.Scale = 1.0;
162 PauseImage.IsVisible = true;
163 unused = PauseImage.FadeTo(1, 50);
167 var unused = PauseImage.FadeTo(0, 100);
168 await PauseImage.ScaleTo(3.0, 300);
169 PauseImage.IsVisible = false;
170 PauseImage.Scale = 1.0;
172 PlayImage.IsVisible = true;
173 unused = PlayImage.FadeTo(1, 50);
178 async void OnImageButtonClicked(object sender, EventArgs e)
180 if (BindingContext is MediaPlayer player)
182 if (player.State == PlayerState.Playing)
188 await player.Start();
194 public class ProgressToBoundTextConverter : IValueConverter
196 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
198 double progress = (double)value;
199 if (Double.IsNaN(progress))
203 return new Rectangle(0, 0, progress, 1);
206 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
208 Rectangle rect = (Rectangle)value;
213 public class MillisecondToTextConverter : IValueConverter
215 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
217 int millisecond = (int)value;
218 int second = (millisecond / 1000) % 60;
219 int min = (millisecond / 1000 / 60) % 60;
220 int hour = (millisecond / 1000 / 60 / 60);
223 return string.Format("{0:d2}:{1:d2}:{2:d2}", hour, min, second);
227 return string.Format("{0:d2}:{1:d2}", min, second);
231 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
233 throw new NotImplementedException();