file360

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 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace File360
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class VideoPage : Page
    {

        private bool MENU_HIDDEN = true;
        private DispatcherTimer musicDispatcher;
        public VideoPage()
        {
            this.InitializeComponent();
            Loaded += VideoPage_Loaded;
            Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
            SizeChanged += VideoPage_SizeChanged;
        }

        private void VideoPage_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            VisualStateManager.GoToState(this, "Landscape", false);
        }

        private void VideoPage_Loaded(object sender, RoutedEventArgs e)
        {
            MenuHide.Begin();
        }

        private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
        {
            if (this.Frame.CanGoBack)
                this.Frame.GoBack();
            else Application.Current.Exit();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            IStorageFile isf = (IStorageFile)e.Parameter;
            mediaPlayer.Source = new Uri(isf.Path);
            mediaPlayer.MediaOpened += (es,st) =>
            {
                UpdateVideoInfo(isf.Name, mediaPlayer.NaturalDuration.TimeSpan.Minutes + ":" + mediaPlayer.NaturalDuration.TimeSpan.Seconds.ToString(),"8");
                musicSlider.Maximum = mediaPlayer.NaturalDuration.TimeSpan.TotalSeconds;
                musicDispatcher.Start();
            };

            musicDispatcher = new DispatcherTimer();
            musicDispatcher.Interval = TimeSpan.FromMilliseconds(1000);


            mediaPlayer.MediaEnded += async (es,st) =>
            {
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, delegate
                {
                    //UpdateVideoInfo("no media", "--:--", "5");
                    //musicDispatcher.Stop();
                    ExitOrGoBack();
                });
            };

            mediaPlayer.MediaFailed += async (s, t) =>
            {
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, delegate
                {
                    //UpdateVideoInfo("no media", "--:--", "5");
                    if (musicDispatcher.IsEnabled)
                        musicDispatcher.Stop();
                    MessageBox mb = new MessageBox();
                    mb.HeadingText = "Cannot Play Media";
                    mb.ContentText = "Cannot play this file, do you want to open with other video player?";
                    mb.LeftButtonHandler += (es, st) => 
                    {
                        ExitOrGoBack();
                    };
                    mb.RightButtonHandler += async (es, st) =>
                    {
                        await Windows.System.Launcher.LaunchFileAsync(isf);
                    };
                    MediaFragment.Children.Add(mb);
                });
            };


            musicDispatcher.Tick += (s, t) =>
            {
                musicSlider.Value = mediaPlayer.Position.TotalSeconds;
                currentDuration.Text = mediaPlayer.Position.Minutes + ":" + mediaPlayer.Position.Seconds;

            };
            videoName.Text = isf.Name;

        }

        private void ExitOrGoBack()
        {
            if (this.Frame.CanGoBack)
                this.Frame.GoBack();
            else Application.Current.Exit();
        }

        private void UpdateVideoInfo(string name, string dur, string playMode)
        {
            videoName.Text = name;
            mediaDuration.Text = dur;
            musicSlider.Value = 0;
            playPause.Content = playMode;
        }

        private void mediaPlayer_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
        {

        }

        private void mediaPlayer_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {

        }

        private void mediaPlayer_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {

        }

        private void PlayCurrent(object sender, RoutedEventArgs e)
        {
            if (mediaPlayer.CurrentState == MediaElementState.Playing)
            {
                mediaPlayer.Pause();
                playPause.Content = "5";
                //DurationBlink.Begin();
            }
            else if (mediaPlayer.CurrentState == MediaElementState.Paused)
            {
                mediaPlayer.Play();
                playPause.Content = "8";
                //DurationBlink.Stop();
            }

        }

        private void Grid_Tapped(object sender, TappedRoutedEventArgs e)
        {
            if (MENU_HIDDEN)
                MenuHideRev.Begin();
            else MenuHide.Begin();
            MENU_HIDDEN = !MENU_HIDDEN;
        }
    }
}