author | PAMPHILE Jonathan <pamphile@efrei.fr> |
Wed, 23 Sep 2009 22:22:52 +0200 | |
changeset 95 | 07972aa3f2e0 |
parent 84 | 067f4a6c0cca |
child 117 | c1222ecbf1b1 |
permissions | -rw-r--r-- |
72
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
1 |
using System; |
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
2 |
using System.IO; |
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
3 |
using System.Net; |
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
4 |
using System.Windows; |
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
5 |
using System.Windows.Controls; |
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
6 |
using System.Windows.Data; |
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
7 |
using System.Windows.Media; |
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
8 |
using System.Windows.Media.Animation; |
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
9 |
using System.Windows.Navigation; |
84 | 10 |
using Microsoft.Surface; |
11 |
using Microsoft.Surface.Presentation; |
|
12 |
using Microsoft.Surface.Presentation.Controls; |
|
13 |
using System.Collections.Generic; |
|
14 |
using System.Windows.Media.Imaging; |
|
95 | 15 |
using System.Xml.Serialization; |
16 |
||
17 |
using System.Reflection; |
|
72
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
18 |
|
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
19 |
namespace FingersDance.Control.ListVideo |
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
20 |
{ |
84 | 21 |
public partial class UserControlListVideo |
22 |
{ |
|
77 | 23 |
//Creation d'un Event pour Chaque Item Video |
72
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
24 |
public event EventHandler EH_ItemVideo1_ContactDown; |
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
25 |
public event EventHandler EH_ItemVideo2_ContactDown; |
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
26 |
|
95 | 27 |
public string path = ""; |
28 |
static Dictionary<string, ListVideoItem> _Videos = new Dictionary<string, ListVideoItem>(); |
|
72
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
29 |
|
84 | 30 |
public UserControlListVideo() |
31 |
{ |
|
32 |
this.InitializeComponent(); |
|
95 | 33 |
LoadList(); |
34 |
UpdateList(); |
|
84 | 35 |
|
36 |
// Insert code required on object creation below this point. |
|
37 |
} |
|
72
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
38 |
|
95 | 39 |
//Event appelé lors de la selection d'un Item dans la Video List |
40 |
private void ItemVideo_ContactTapGesture(object sender, Microsoft.Surface.Presentation.ContactEventArgs e) |
|
72
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
41 |
{ |
95 | 42 |
path = ((CustomListBoxItem)sender).Path; |
72
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
43 |
if (EH_ItemVideo1_ContactDown != null) |
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
44 |
EH_ItemVideo1_ContactDown(this, new EventArgs()); |
95 | 45 |
|
72
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
46 |
} |
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
47 |
|
95 | 48 |
private void ItemVideo_Click(object sender, System.Windows.RoutedEventArgs e) |
72
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
49 |
{ |
95 | 50 |
path = ((CustomListBoxItem)sender).Path; |
72
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
51 |
if (EH_ItemVideo1_ContactDown != null) |
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
52 |
EH_ItemVideo1_ContactDown(this, new EventArgs()); |
95 | 53 |
|
72
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
54 |
} |
84 | 55 |
|
56 |
private void ButtonImporter_ContactDown(object sender, Microsoft.Surface.Presentation.ContactEventArgs e) |
|
57 |
{ |
|
58 |
AddVideoToList(); |
|
59 |
} |
|
60 |
||
61 |
private void ButtonImporter_Click(object sender, System.Windows.RoutedEventArgs e) |
|
62 |
{ |
|
63 |
AddVideoToList(); |
|
64 |
} |
|
65 |
||
66 |
private void AddVideoToList() |
|
67 |
{ |
|
68 |
try |
|
69 |
{ |
|
70 |
string[] Files = new string[1]; |
|
71 |
if (System.IO.Directory.Exists(PathImporter.Text)) |
|
72 |
Files = System.IO.Directory.GetFiles(PathImporter.Text); |
|
73 |
else |
|
74 |
if (System.IO.File.Exists(PathImporter.Text)) |
|
75 |
Files[0] = PathImporter.Text; |
|
76 |
if (Files != null) |
|
95 | 77 |
{ |
84 | 78 |
foreach (string elt in Files) |
79 |
if (elt.ToLower().EndsWith(".wmv")) // ..... |
|
80 |
try |
|
81 |
{ |
|
95 | 82 |
ListVideoItem newItem = new ListVideoItem(); |
84 | 83 |
Image item = getFirstPreview(elt); |
95 | 84 |
if (item != null && !_Videos.ContainsKey(elt)) |
84 | 85 |
{ |
95 | 86 |
newItem.Name = elt.Split('\\')[elt.Split('\\').Length - 1].Split('.')[0]; |
87 |
newItem.Path = elt; |
|
88 |
newItem.Preview = elt.Replace(".wmv", ".jpg"); |
|
89 |
_Videos.Add(newItem.Path, newItem); |
|
84 | 90 |
} |
91 |
} |
|
92 |
catch (Exception ex) { } |
|
95 | 93 |
UpdateList(); |
94 |
SaveList(); |
|
95 |
} |
|
84 | 96 |
} |
97 |
catch (Exception) { } |
|
98 |
} |
|
99 |
||
100 |
Image getFirstPreview(string path) |
|
101 |
{ |
|
102 |
MediaPlayer _player = new MediaPlayer(); |
|
103 |
_player.Open(new Uri(path)); |
|
104 |
_player.Play(); |
|
105 |
_player.Position = new TimeSpan(0, 0, 5); |
|
106 |
System.Threading.Thread.Sleep(800); |
|
107 |
RenderTargetBitmap target = new RenderTargetBitmap(100, 100, 1 / 100, 1 / 100, PixelFormats.Pbgra32); |
|
108 |
DrawingVisual visual = new DrawingVisual(); |
|
109 |
DrawingContext context = visual.RenderOpen(); |
|
110 |
Rect frameRect = new Rect(); |
|
111 |
frameRect.Height = target.Height; |
|
112 |
frameRect.Width = target.Width; |
|
113 |
context.DrawVideo(_player, frameRect); |
|
114 |
context.Close(); |
|
115 |
target.Render(visual); |
|
116 |
Image _prev = new Image(); |
|
117 |
_prev.Source = BitmapFrame.Create(target).GetAsFrozen() as BitmapFrame; |
|
118 |
_player.Stop(); |
|
119 |
||
120 |
return _prev; |
|
121 |
} |
|
95 | 122 |
|
123 |
private void RefreshButton_ContactDown(object sender, Microsoft.Surface.Presentation.ContactEventArgs e) |
|
124 |
{ |
|
125 |
LoadList(); |
|
126 |
UpdateList(); |
|
127 |
} |
|
128 |
||
129 |
private void RefreshButton_Click(object sender, System.Windows.RoutedEventArgs e) |
|
130 |
{ |
|
131 |
LoadList(); |
|
132 |
UpdateList(); |
|
133 |
} |
|
134 |
||
135 |
void LoadList() |
|
136 |
{ |
|
137 |
_Videos.Clear(); |
|
138 |
StreamReader reader = null; |
|
139 |
FileInfo assemblyPath = new FileInfo(Assembly.GetExecutingAssembly().Location); |
|
140 |
DirectoryInfo info = assemblyPath.Directory; |
|
141 |
try |
|
142 |
{ |
|
143 |
reader = new StreamReader(info.FullName.ToString() + "\\Resources\\videos.xml"); |
|
144 |
XmlSerializer serializer = new XmlSerializer(typeof(List<ListVideoItem>)); |
|
145 |
List<ListVideoItem> temp = (List<ListVideoItem>)serializer.Deserialize(reader); |
|
146 |
foreach (ListVideoItem elt in temp) |
|
147 |
if (!elt.Preview.Equals("") && !_Videos.ContainsKey(elt.Path)) |
|
148 |
_Videos.Add(elt.Path, elt); |
|
149 |
reader.Close(); |
|
150 |
} |
|
151 |
catch (Exception e) |
|
152 |
{ |
|
153 |
if (reader != null) |
|
154 |
reader.Close(); |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
void SaveList() |
|
159 |
{ |
|
160 |
StreamWriter writter = null; |
|
161 |
FileInfo assemblyPath = new FileInfo(Assembly.GetExecutingAssembly().Location); |
|
162 |
DirectoryInfo info = assemblyPath.Directory; |
|
163 |
try |
|
164 |
{ |
|
165 |
writter = new StreamWriter(info.FullName.ToString() + "\\Resources\\videos.xml"); |
|
166 |
XmlSerializer serializer = new XmlSerializer(typeof(List<ListVideoItem>)); |
|
167 |
List<ListVideoItem> temp = new List<ListVideoItem>(); |
|
168 |
foreach (KeyValuePair<string, ListVideoItem> elt in _Videos) |
|
169 |
temp.Add(elt.Value); |
|
170 |
serializer.Serialize(writter, temp); |
|
171 |
writter.Close(); |
|
172 |
} |
|
173 |
catch (Exception e) |
|
174 |
{ |
|
175 |
if (writter != null) |
|
176 |
writter.Close(); |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
void UpdateList() |
|
181 |
{ |
|
182 |
stackPanel.Children.Clear(); |
|
183 |
foreach (KeyValuePair<string, ListVideoItem> elt in _Videos) |
|
184 |
{ |
|
185 |
try |
|
186 |
{ |
|
187 |
if (!elt.Value.Preview.Equals("")) |
|
188 |
{ |
|
189 |
MediaElement item = new MediaElement(); |
|
190 |
item.Source = new Uri(elt.Value.Preview); |
|
191 |
item.Width = 100; |
|
192 |
item.Height = 100; |
|
193 |
CustomListBoxItem Contener = new CustomListBoxItem(); |
|
194 |
Contener.Content = item; |
|
195 |
Contener.Path = elt.Value.Path; |
|
196 |
Contener.Name = elt.Value.Name; |
|
197 |
Contener.ContactTapGesture += ItemVideo_ContactTapGesture; |
|
198 |
stackPanel.Children.Add(Contener); |
|
199 |
} |
|
200 |
} |
|
201 |
catch (Exception e) |
|
202 |
{ } |
|
203 |
} |
|
204 |
} |
|
84 | 205 |
} |
72
f1ca0958c992
Travail sur le control List Video et comunication entre ListVideo et FingersDance
sarias
parents:
diff
changeset
|
206 |
} |