private static void CreateRadioPanel(Panel panel) { Label title = new Label("Radio", 5, 5, 200, Fonts.ArialMediumBold.Height) { Font = Fonts.ArialMediumBold }; panel.AddChild(title); const int btSize = 50; const int margin = 5; int y = title.X + title.Height + margin; RadioStationsList radioList = new RadioStationsList(margin, y, panel.Width - margin * 3 - btSize, panel.Height - y - margin * 2 - btSize); foreach (RadioStationItem item in AppSettings.Instance.RadioStations) radioList.AddItem(item); Mp.Ui.Controls.TouchControls.Button btAdd, btRemove, btEdit; panel.AddChild(btAdd = new ImageButton(Images.GetBitmap(Images.BitmapResources.imgAdd), radioList.X + radioList.Width + margin, radioList.Y, btSize, btSize)); panel.AddChild(btRemove = new ImageButton(Images.GetBitmap(Images.BitmapResources.imgDelete), btAdd.X, btAdd.Y + btSize + margin, btSize, btSize)); panel.AddChild(btEdit = new ImageButton(Images.GetBitmap(Images.BitmapResources.imgEdit), btAdd.X, btRemove.Y + btSize + margin, btSize, btSize)); EditStationDialog esd = new EditStationDialog(); #region add/remove/edit events btAdd.ButtonPressed += (s) => { new Thread(() => { RadioStationItem item = new RadioStationItem("http://", string.Empty); if (esd.Show("New Radio Station", item)) { radioList.AddItem(item); AppSettings.Instance.RadioStations.Add(item); AppSettings.Instance.Save(); } }).Start(); }; btRemove.ButtonPressed += (s) => { if (radioList.SelectedIndex != -1) { RadioStationItem selItem = radioList.SelectedItem; new Thread(() => { using (MessageBox mb = new MessageBox("Are you sure you want to delete\n" + selItem.Name + "?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcons.Delete)) if (mb.Show() == MessageBoxResult.Yes) { radioList.RemoveItem(selItem); AppSettings.Instance.RadioStations.Remove(selItem); AppSettings.Instance.Save(); } }).Start(); } }; btEdit.ButtonPressed += (s) => { if (radioList.SelectedIndex != -1) { RadioStationItem selItem = radioList.SelectedItem; new Thread(() => { if (esd.Show("Edit Radio Station", selItem)) { radioList.Refresh(); AppSettings.Instance.Save(); } }).Start(); } }; #endregion ImageButton btPlay, btStop; panel.AddChild(btPlay = new ImageButton(Images.GetBitmap(Images.BitmapResources.btPlay), radioList.X, radioList.Y + radioList.Height + margin, btSize, btSize)); panel.AddChild(btStop = new ImageButton(Images.GetBitmap(Images.BitmapResources.btStop), btPlay.X + btPlay.Width + margin, radioList.Y + radioList.Height + margin, btSize, btSize)); RadioStationItem playingItem = null; int playingIndex = 0; VsDriver.VsStatusChanged += (status) => { if (playingItem == null) return; switch (status) { case VsStatus.Connecting: playingItem.IsConnecting = true; radioList.RefreshItem(playingIndex); break; case VsStatus.PlayingRadio: playingItem.IsConnecting = false; playingItem.IsPlaying = true; radioList.RefreshItem(playingIndex); break; case VsStatus.Stopped: playingItem.IsPlaying = false; playingItem.IsConnecting = false; radioList.RefreshItem(playingIndex); break; } }; string metaData = null; GetRadioInfo += () => { return VsDriver.Status == VsStatus.PlayingRadio ? metaData : null; }; char[] metadataSeparators = new char[] { ';' }; VsDriver.VsRadioMetaDataReceived += (newMetaData) => { string[] metaDataParams = newMetaData.Split(metadataSeparators); metaData = newMetaData; }; btPlay.ButtonPressed += (s) => { if (radioList.SelectedIndex == -1) return; if (playingItem == null) { playingItem = radioList.SelectedItem; playingIndex = radioList.SelectedIndex; } else { VsDriver.StopRadio(); playingItem = radioList.SelectedItem; playingIndex = radioList.SelectedIndex; } VsDriver.ConnectToStation(playingItem.Address); }; btStop.ButtonPressed += (s) => { VsDriver.StopRadio(); playingItem = null; }; panel.AddChild(radioList); }
private static void CreatePlayerPanel(Panel panel) { const int btSize = 50; const int margin = 5; Label title = new Label("Player", 5, 5, 200, Fonts.ArialMediumBold.Height) { Font = Fonts.ArialMediumBold }; panel.AddChild(title); FsList fsList = new FsList(5, title.Y + title.Height + 5, panel.Width - margin * 3 - btSize, panel.Height - (title.Y + title.Height + margin * 3 + btSize)); panel.AddChild(fsList); ArrayList rootItems = new ArrayList(); FsItem currentPlaying = null; SimpleActionParamString playFile = (filePath) => { switch (VsDriver.Status) { case VsStatus.Connecting: return false; case VsStatus.PlayingRadio: return false; case VsStatus.Playing: VsDriver.StopFile(); break; case VsStatus.Paused: VsDriver.ResumeFile(); VsDriver.StopFile(); break; } int tries = 5; Exception lastEx = null; while (tries-- != 0) { try { FileStream file = File.Open(filePath, FileMode.Open); VsDriver.PlayFile(file); lastEx = null; break; } catch (Exception ex) { lastEx = ex; } } if (lastEx != null) { new Thread(() => { using (MessageBox mb = new MessageBox(lastEx.Message, "Error", MessageBoxButtons.Ok, MessageBoxIcons.Error)) mb.Show(); }).Start(); return false; } return true; }; #region list double click fsList.OnDoubleClick += (s) => { FsItem selectedItem = fsList.SelectedItem; if (selectedItem == null) return; if (selectedItem.IsBack) { fsList.ClearItems(); fsList.Folder = selectedItem.Parent; if (fsList.Folder.Parent != null) fsList.AddItems(fsList.Folder.Parent.Childs); else fsList.AddItems(rootItems); } else if (selectedItem.IsDirectory) { if (selectedItem.Childs == null) { selectedItem.Childs = new ArrayList(); string[] dirs = Directory.GetDirectories(selectedItem.Path); selectedItem.Childs.Add(new FsItem(selectedItem) { IsBack = true, Name = "Back" }); foreach (string dir in dirs) selectedItem.Childs.Add(new FsItem(selectedItem) { IsBack = false, IsDirectory = true, IsMusicFile = false, Name = Path.GetFileName(dir), Path = dir }); string[] files = Directory.GetFiles(selectedItem.Path); foreach (string file in files) { string extension = Path.GetExtension(file).ToLower(); bool isMusicFile = extension.Equals(".mp3"); selectedItem.Childs.Add(new FsItem(selectedItem) { IsBack = false, IsDirectory = false, IsMusicFile = isMusicFile, Name = Path.GetFileName(file), Path = file }); } } fsList.Folder = selectedItem; fsList.ClearItems(); fsList.AddItems(selectedItem.Childs); } else if (selectedItem.IsMusicFile) { if (playFile(selectedItem.Path)) { currentPlaying = selectedItem; fsList.CurrentPlaying = currentPlaying; } } }; #endregion #region removable media events RemovableMedia.Insert += (s, e) => { FsItem newItem = new FsItem(null) { IsBack = false, IsDirectory = true, IsMusicFile = false, Name = e.Volume.Name, Path = e.Volume.RootDirectory }; rootItems.Add(newItem); if (fsList.Folder == null) fsList.AddItem(newItem); }; RemovableMedia.Eject += (s, e) => { FsItem toRemove = null; foreach (FsItem fsItem in rootItems) if (fsItem.Path == e.Volume.RootDirectory) { toRemove = fsItem; break; } if (toRemove != null) { rootItems.Remove(toRemove); if (fsList.Folder == null) fsList.RemoveItem(toRemove); else if (fsList.Folder.GetRootNode().Path == e.Volume.RootDirectory) { fsList.Folder = null; fsList.ClearItems(); fsList.AddItems(rootItems); } } }; #endregion ImageButton btDelete; panel.AddChild(btDelete = new ImageButton(Images.GetBitmap(Images.BitmapResources.imgDelete), fsList.X + fsList.Width + margin, fsList.Y, btSize, btSize)); btDelete.ButtonPressed += (s) => { FsItem selectedItem = fsList.SelectedItem; if (selectedItem == null || selectedItem.IsBack || selectedItem.Parent == null) return; if (selectedItem == currentPlaying) VsDriver.StopFile(); new Thread(new ThreadStart(() => { using (MessageBox mb = new MessageBox("Are you sure you want to delete\n" + selectedItem.Name + "?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcons.Delete)) { if (mb.Show() == MessageBoxResult.Yes) { try { if (selectedItem.IsDirectory) Directory.Delete(selectedItem.Path); else File.Delete(selectedItem.Path); selectedItem.Parent.Childs.Remove(selectedItem); fsList.RemoveItem(selectedItem); } catch (Exception) { using (MessageBox err = new MessageBox("Error deleting item", "Error", MessageBoxButtons.Ok, MessageBoxIcons.Error)) err.Show(); } } } })).Start(); }; ImageButton btPlay, btStop, btNext, btPrev; int x = fsList.X, y = fsList.Y + fsList.Height + margin; Bitmap imgPlay = Images.GetBitmap(Images.BitmapResources.btPlay), imgPause = Images.GetBitmap(Images.BitmapResources.btPause); panel.AddChild(btPrev = new ImageButton(Images.GetBitmap(Images.BitmapResources.btBack), x, y, btSize, btSize)); x += margin + btSize; panel.AddChild(btPlay = new ImageButton(Images.GetBitmap(Images.BitmapResources.btPlay), x, y, btSize, btSize)); x += margin + btSize; panel.AddChild(btNext = new ImageButton(Images.GetBitmap(Images.BitmapResources.btForward), x, y, btSize, btSize)); x += margin + btSize; panel.AddChild(btStop = new ImageButton(Images.GetBitmap(Images.BitmapResources.btStop), x, y, btSize, btSize)); #region prev/play/next/stop events bool ignore = false; UiEventHandler actionPrev = (s) => { if (ignore) return; try { ignore = true; if (currentPlaying == null) return; FsItem aux = currentPlaying; int stIndex = aux.Parent.Childs.IndexOf(aux); if (stIndex == -1) return; FsItem nextToPlay; do { stIndex--; if (stIndex < 0) stIndex = aux.Parent.Childs.Count - 1; nextToPlay = (FsItem)aux.Parent.Childs[stIndex]; if (nextToPlay.IsMusicFile) break; } while (true); if (playFile(nextToPlay.Path)) { currentPlaying = nextToPlay; fsList.CurrentPlaying = currentPlaying; } } finally { ignore = false; } }; UiEventHandler actionNext = (s) => { if (ignore) return; try { ignore = true; if (currentPlaying == null) return; FsItem aux = currentPlaying; int stIndex = aux.Parent.Childs.IndexOf(aux); if (stIndex == -1) return; FsItem nextToPlay; do { stIndex++; if (stIndex == aux.Parent.Childs.Count) stIndex = 0; nextToPlay = (FsItem)aux.Parent.Childs[stIndex]; if (nextToPlay.IsMusicFile) break; } while (true); if (playFile(nextToPlay.Path)) { currentPlaying = nextToPlay; fsList.CurrentPlaying = currentPlaying; } } finally { ignore = false; } }; btPrev.ButtonPressed += actionPrev; btNext.ButtonPressed += actionNext; btPlay.ButtonPressed += (s) => { switch (VsDriver.Status) { case VsStatus.Playing: VsDriver.PauseFile(); break; case VsStatus.Paused: VsDriver.ResumeFile(); break; case VsStatus.Stopped: FsItem selectedItem = fsList.SelectedItem; if (selectedItem != null && selectedItem.IsMusicFile) { if (playFile(selectedItem.Path)) { currentPlaying = selectedItem; fsList.CurrentPlaying = currentPlaying; } } break; } }; bool stopPressed = false; btStop.ButtonPressed += (s) => { switch (VsDriver.Status) { case VsStatus.Playing: stopPressed = true; VsDriver.StopFile(); break; case VsStatus.Paused: stopPressed = true; VsDriver.ResumeFile(); VsDriver.StopFile(); break; } }; #endregion VsDriver.VsStatusChanged += (status) => { if (status == VsStatus.Playing) btPlay.Image = imgPause; else { if (status == VsStatus.Stopped) { if (!stopPressed) { if (currentPlaying != null) actionNext(btNext); } else { currentPlaying = null; fsList.CurrentPlaying = currentPlaying; stopPressed = false; } } btPlay.Image = imgPlay; } }; }
public static void Main() { AppConfig.Init(); DesktopManager.Instance.InputManager = InputManager.Current; VsDriver.VsExceptionRaised += (msg) => { new Thread(() => { using (MessageBox mb = new MessageBox(msg, "Error", MessageBoxButtons.Ok, MessageBoxIcons.Error)) mb.Show(); }).Start(); }; CreateUi(); CreateScreenSaver(); StorageManager i = StorageManager.Instance; Thread.Sleep(Timeout.Infinite); Debug.Print(i.ToString()); }