File size: 12,233 Bytes
e26fba6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using DZ.MediaPlayer.Vlc.WindowsForms;
using DZ.MediaPlayer.Vlc.Io;
using System.Threading;
using ComponentFactory.Krypton.Toolkit;
using BlueTube.Viewer.Properties;
using Client.Scraper;
using BlueTube.Viewer.Data;

namespace BlueTube.Viewer
{
    public partial class VideoPlayerWidget : UserControl, IFavouriteEnabledView
    {

        #region Fields

        private bool init;
        private List<KryptonCheckButton> playerButtons = new List<KryptonCheckButton>();
        private ScrapedVideo currentPlayingVideo;
        private DataService dataService = DataService.Create();
        private bool mute;
        private int lastVolume;
        public event EventHandler<ToggleFavouriteEventArgs> ToggelFavourite;
        public event EventHandler<PlayEventArgs> PlayNext;
        public event EventHandler<PlayEventArgs> PlayPrevious;

        #endregion

        #region Contructor

        public VideoPlayerWidget()
        {
            InitializeComponent();
            
            playerButtons.Add(this.buttonNext);
            playerButtons.Add(this.buttonPrevious);
            playerButtons.Add(this.buttonPlayPause);
            playerButtons.Add(this.buttonStop);
        }

        #endregion

        #region Public

        public bool PlayStart(ScrapedVideo video)
        {
            this.headerGroupVideoPlayer.ValuesPrimary.Heading = video.Title;
            this.currentPlayingVideo = video;
            UpdateFavouriteButton(DataService.Create().IsFavourite(this.currentPlayingVideo));
            return PlayStart(video.PlayUrl);
        }
         
        private void UpdateFavouriteButton(bool fav)
        {
            this.buttonSpecHeaderFavourite.Checked = fav  ?
                ButtonCheckState.Checked : ButtonCheckState.Unchecked;
        }


     
        public bool PlayStart(string url)
        {
            try
            {
                Init();

                this.vlcPlayerControl.Play(new MediaInput(MediaInputType.NetworkStream, url));
                return true;
            }
            catch (Exception ex)
            {
                Program.SetIdle();
                KryptonMessageBox.Show("Unable to play the video. Please try again", Constants.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return false;
        }
        public void ActivateView()
        {
            if (this.vlcPlayerControl.State == VlcPlayerControlState.Paused)
                this.vlcPlayerControl.PauseOrResume();
        }
        public void DeactivateView()
        {
            if(this.vlcPlayerControl.State == VlcPlayerControlState.Playing)
                this.vlcPlayerControl.PauseOrResume();
        }


        #endregion

        #region Private

        private void Init()
        {
            if (!init)
            {
                this.vlcPlayerControl.Initialize(this);
                InitializeProgress();
                InitializeVolume();
                this.vlcPlayerControl.StateChanged += vlc_onStateChanged;
                this.vlcPlayerControl.PositionChanged += vlc_onPositionChanged;
                this.vlcPlayerControl.EndReached += vlc_onEndReached;
                this.vlcPlayerControl.Player.IsFullScreen = true;
                init = true;
            }

        }
        private void InitializeProgress()
        {
            //statusStrip.Items["playerStatus"].Text = Convert.ToString(videoWindow.VlcPlayerControl.State);
            this.labelTimeProgress.Text = String.Format("{0:D2}:{1:D2}:{2:D2}",
                this.vlcPlayerControl.Time.Hours, this.vlcPlayerControl.Time.Minutes,
                this.vlcPlayerControl.Time.Seconds);
        }
        private void InitializeVolume()
        {
            this.kryptonTrackBarVolume.Value = (int)((1.0 * this.vlcPlayerControl.Volume / 100) *
                this.kryptonTrackBarVolume.Maximum);
        }
        private void PlayerButtonsUncheck()
        {
            foreach (var b in this.playerButtons)
                b.Checked = false;
        }
        private void UpdatePlayProgress()
        {

            try
            {
                int newTrackbarPositionValue = (int)(this.vlcPlayerControl.Position * 100);

                if (newTrackbarPositionValue != this.kryptonTrackBar1.Value)
                {
                    this.kryptonTrackBar1.Value = newTrackbarPositionValue > this.kryptonTrackBar1.Maximum
                                                ? this.kryptonTrackBar1.Maximum
                                                : newTrackbarPositionValue;
                }

                var time = this.vlcPlayerControl.Time;
                this.labelTimeProgress.Text = String.Format("{0:D2}:{1:D2}:{2:D2}", time.Hours, time.Minutes, time.Seconds);
                if (this.vlcPlayerControl.State != VlcPlayerControlState.Idle)
                {
                    var total = TimeSpan.FromMilliseconds((1 / this.vlcPlayerControl.Position) * this.vlcPlayerControl.Time.TotalMilliseconds);
                    this.labelTotalTime.Text = String.Format("{0:D2}:{1:D2}:{2:D2}", total.Hours, total.Minutes, total.Seconds);
                }
            }
            catch { }
            finally
            {

            }

        }
        public void PlayStop()
        {
            PlayerButtonsUncheck();
            try
            {
                if (this.vlcPlayerControl.State != VlcPlayerControlState.Idle)
                {
                    this.vlcPlayerControl.Stop();
                    UpdatePlayProgress();
                }
            }
            catch (Exception exc)
            {

                MessageBox.Show(String.Format("Cannot stop player : {0}", exc));
            }
        }
        

        #endregion

        #region Events

        private void vlc_onStateChanged(object sender, EventArgs args)
        {
            VlcPlayerControlState currentState = this.vlcPlayerControl.State;
            //
            switch (currentState)
            {
                case VlcPlayerControlState.Idle:
                    {
                        this.buttonPlayPause.Values.Image = Resources.play;
                        break;
                    }
                case VlcPlayerControlState.Paused:
                    {
                        this.buttonPlayPause.Values.Image = Resources.play;
                        break;
                    }
                case VlcPlayerControlState.Playing:
                    {
                        this.buttonPlayPause.Values.Image = Resources.pause;
                        break;
                    }
            }
        }
        private void vlc_onEndReached(object sender, EventArgs e)
        {

        }
        private void vlc_onPositionChanged(object sender, EventArgs e)
        {
            if (InvokeRequired)
                Invoke(new ThreadStart(UpdatePlayProgress));
            else
                UpdatePlayProgress();
        }

        private void buttonPlayPause_Click(object sender, EventArgs e)
        {
            PlayerButtonsUncheck();
            switch (this.vlcPlayerControl.State)
            {
                case VlcPlayerControlState.Idle:
                    {
                        if (currentPlayingVideo != null)
                            PlayStart(currentPlayingVideo);
                        break;
                    }
                case VlcPlayerControlState.Paused:
                    {
                        this.vlcPlayerControl.PauseOrResume();
                        break;
                    }
                case VlcPlayerControlState.Playing:
                    {
                        this.vlcPlayerControl.PauseOrResume();
                        break;
                    }
            }
        }

        private void kryptonCheckButton1_Click(object sender, EventArgs e)
        {
            //this.vlcControl1.AudioProperties.IsMute = !this.vlcControl1.AudioProperties.IsMute;
        }

        private void kryptonTrackBarVolume_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                this.vlcPlayerControl.Volume = (int)((1.0 *
                    this.kryptonTrackBarVolume.Value / this.kryptonTrackBarVolume.Maximum) * 100);
                if (this.vlcPlayerControl.Volume > 0)
                {
                    mute = false;
                    this.buttonVolume.Values.Image = Resources.volume;
                }

            }
            catch (Exception exc)
            {
                MessageBox.Show(String.Format("Cannot change volume level: {0}", exc));
            }
        }

        private void kryptonTrackBar1_Scroll(object sender, EventArgs e)
        {
            try
            {

                if (this.vlcPlayerControl.State == VlcPlayerControlState.Idle)
                {
                    this.kryptonTrackBar1.Value = 0;
                    return;
                }
                //
                float val = 1f * this.kryptonTrackBar1.Value / this.kryptonTrackBar1.Maximum;
                if (val != this.vlcPlayerControl.Position)
                    this.vlcPlayerControl.Position = val;

            }
            catch (Exception exc)
            {
                //
                MessageBox.Show(String.Format("Cannot change position : {0}", exc));
            }

        }

        private void VideoPlayerWidget_Load(object sender, EventArgs e)
        {
            //Init();
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            PlayStop();
        }

        private void kryptonCheckButtonVolume_Click(object sender, EventArgs e)
        {
            Init();
            if (!mute)
                this.lastVolume = this.vlcPlayerControl.Volume;

            this.vlcPlayerControl.Volume = mute ? this.lastVolume : 0;
            mute = !mute;
            this.buttonVolume.Values.Image = mute ? Resources.mute : Resources.volume;
            this.kryptonTrackBarVolume.Value = this.vlcPlayerControl.Volume;
        }

        public void TriggerSearch(SearchParameters param)
        {
            throw new NotImplementedException();
        }

        private void buttonSpecHeaderGroupFullscreen_Click(object sender, EventArgs e)
        {
            new FullscreenPlayer(this.vlcPlayerControl).ShowDialog();
        }

        private void buttonSpecHeaderGroup1_Click(object sender, EventArgs e)
        {
            if(this.currentPlayingVideo != null)
            {
                var fav = dataService.IsFavourite(this.currentPlayingVideo);
                if (!fav)
                    dataService.AddToFavourite(this.currentPlayingVideo);
                else
                    dataService.DeleteFromFavourite(this.currentPlayingVideo);
                    
                UpdateFavouriteButton(!fav);
                if (this.ToggelFavourite != null)
                    this.ToggelFavourite(this, new ToggleFavouriteEventArgs
                    {
                        IsFavourite = !fav,
                        Video = this.currentPlayingVideo
                    });

                
            }
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            if (PlayNext != null)
                PlayNext(this, new PlayEventArgs { Current = this.currentPlayingVideo });
        }

        private void buttonPrevious_Click(object sender, EventArgs e)
        {
            if (PlayPrevious != null)
                PlayPrevious(this, new PlayEventArgs { Current = this.currentPlayingVideo });
        }

        #endregion

       

    }

    
}