private void mediaLoadVideo(string vidId) { TVideo currentVideo = currentTPlaylist.playlist.FirstOrDefault(TVideo => TVideo.videoID == vidId); if (currentVideo == null) { currentVideo = new TVideo() { videoID = vidId }; currentTPlaylist.playlist.Add(currentVideo); } currentlyPlayingVideo = currentVideo; Console.Write(string.Format("Media: Load '" + vidId + "'\n")); YTplayer_CallFlash("loadVideoById(" + vidId + ")"); if (currentlySelectedPlaylist == 1) { currentTPlaylist.isPlaying = true; if (currentVideo.duration == 0) { GetVideoInfo(currentVideo); } } }
public void AddVideoToRandomPlaylist(string youtubeLink) { TVideo video = new TVideo(); string vidID = TPlaylist.parseYTurl(youtubeLink); video = new TVideo { videoID = vidID }; //if (!GetVideoInfo(video)) // randomPlaylist.playlist.Add(); }
/// <summary> /// Queues a song for playing /// </summary> /// <param name="youtubeLink"></param> /// <returns> /// 0 - Success /// 1 - Invalid File /// 2 - Song too long /// </returns> public int QueueOrPlaySong(string youtubeLink, out TVideo video) { video = null; // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // If these threads are different, it returns true. if (this.textBox1.InvokeRequired) { PlaySongCallback d = new PlaySongCallback(QueueOrPlaySong); object[] args = new object[] { youtubeLink, video }; int retVal = (int)this.Invoke(d, args); video = (TVideo)args[1]; return(retVal); } else { string vidID = TPlaylist.parseYTurl(youtubeLink); if (!string.IsNullOrWhiteSpace(vidID)) { video = new TVideo { videoID = vidID }; if (!GetVideoInfo(video)) { return(1); } if (video.duration > 8 * 60) { return(2); } ; currentTPlaylist.playlist.Add(video); if (!currentTPlaylist.isPlaying) { currentTPlaylist.current = currentTPlaylist.playlist.Count - 1; mediaLoadVideo(vidID); } } else { return(1); } return(0); } }
private bool GetVideoInfo(TVideo video) { try { string str = (new WebClient()).DownloadString("https://www.googleapis.com/youtube/v3/videos?id=" + video.videoID + "&part=snippet,contentDetails&key=AIzaSyCxll2fv3MwHnA3SdQ7XFJWX5Vvbg67UF4"); JObject root = JObject.Parse(str); JToken firstItems = ((JArray)root["items"]).FirstOrDefault(); video.name = firstItems["snippet"]["title"].ToString(); string duration = firstItems["contentDetails"]["duration"].ToString(); video.duration = (int)XmlConvert.ToTimeSpan(duration).TotalSeconds; return(true); } catch (Exception) { Console.WriteLine("Exception thrown while getting youtube info."); return(false); } }
public void loadFromFile(bool showDialog = false, bool clearPlaylist = true, string[] ytpFiles = null) { if (clearPlaylist) { this.playlist = new BindingList <TVideo>(); } if ((ytpFiles == null && string.IsNullOrWhiteSpace(this.path)) || showDialog) { // Create an instance of the open file dialog box. OpenFileDialog loadTPlaylistFileDialog = new OpenFileDialog(); // Set filter options and filter index. loadTPlaylistFileDialog.Filter = "YouTube Playlist File (.ytp)|*.ytp|YouTube VideoIds File (.txt)|*.txt"; loadTPlaylistFileDialog.FilterIndex = 1; loadTPlaylistFileDialog.Multiselect = true; // Call the ShowDialog method to show the dialog box. DialogResult userClickedOK = loadTPlaylistFileDialog.ShowDialog(); // Process input if the user clicked OK. if (userClickedOK == DialogResult.OK) { TVideo TVideoEntry = new TVideo(); List <string> parsedLine = new List <string>(); if (loadTPlaylistFileDialog.FileNames.Length == 1) { this.path = loadTPlaylistFileDialog.FileNames[0].ToString(); this.name = Path.GetFileNameWithoutExtension(loadTPlaylistFileDialog.FileNames[0]); } foreach (string file in loadTPlaylistFileDialog.FileNames) { using (StreamReader reader = new StreamReader(file)) { while (!reader.EndOfStream) { parsedLine = parseDelimitedString(reader.ReadLine(), '\t'); string vidID = parsedLine.Count > 0 ? parseYTurl(parsedLine[0]) : ""; if (!string.IsNullOrWhiteSpace(vidID)) { switch (parsedLine.Count) { case 3: TVideoEntry = new TVideo { videoID = vidID, name = parsedLine[1], duration = int.Parse(parsedLine[2]) }; this.playlist.Add(TVideoEntry); break; //default playlist case 1: TVideoEntry = new TVideo { videoID = vidID }; this.playlist.Add(TVideoEntry); break; //only youtubeVideoId playlist default: Console.Write("Ignoring: Wrong line Format (" + parsedLine.Count.ToString() + " collumns)\r\n"); break; //error } } else { Console.Write("Ignoring: invalid video ID \"" + (parsedLine.Count > 0? parseYTurl(parsedLine[0]):"") + "\" \r\n"); } } } } } } else { if (ytpFiles == null) { ytpFiles[0] = this.path; } TVideo TVideoEntry = new TVideo(); List <string> parsedLine = new List <string>(); if (ytpFiles.Length == 1) { this.path = ytpFiles[1].ToString(); } foreach (string file in ytpFiles) { using (StreamReader reader = new StreamReader(file)) { while (!reader.EndOfStream) { parsedLine = parseDelimitedString(reader.ReadLine(), '\t'); string vidID = parsedLine.Count > 0 ? parseYTurl(parsedLine[0]) : ""; if (!string.IsNullOrWhiteSpace(vidID)) { switch (parsedLine.Count) { case 3: TVideoEntry = new TVideo { videoID = vidID, name = parsedLine[1], duration = int.Parse(parsedLine[2]) }; this.playlist.Add(TVideoEntry); break; //default playlist case 1: TVideoEntry = new TVideo { videoID = vidID }; this.playlist.Add(TVideoEntry); break; //only youtubeVideoId playlist default: Console.Write("Ignoring: Wrong line Format (" + parsedLine.Count.ToString() + " collumns)\r\n"); break; //error } } else { Console.Write("Ignoring: invalid video ID \"" + (parsedLine.Count > 0 ? parseYTurl(parsedLine[0]) : "") + "\" \r\n"); } } } } } this.updateCurrent(); }
public void loadFromFile(bool showDialog = false, string[] ytpFiles = null) { if ((ytpFiles == null && string.IsNullOrWhiteSpace(this.path)) || showDialog) { // Create an instance of the open file dialog box. OpenFileDialog loadTPlaylistFileDialog = new OpenFileDialog(); // Set filter options and filter index. loadTPlaylistFileDialog.Filter = "YouTube Playlist File (.ytp)|*.ytp|YouTube VideoIds File (.txt)|*.txt"; loadTPlaylistFileDialog.FilterIndex = 1; loadTPlaylistFileDialog.Multiselect = true; // Call the ShowDialog method to show the dialog box. DialogResult userClickedOK = loadTPlaylistFileDialog.ShowDialog(); // Process input if the user clicked OK. if (userClickedOK == DialogResult.OK) { TVideo TVideoEntry = new TVideo(); List <string> parsedLine = new List <string>(); if (loadTPlaylistFileDialog.FileNames.Length == 1) { this.path = loadTPlaylistFileDialog.FileNames[0].ToString(); } foreach (string file in loadTPlaylistFileDialog.FileNames) { using (StreamReader reader = new StreamReader(file)) { while (!reader.EndOfStream) { parsedLine = parceDelimitedString(reader.ReadLine(), '\t'); switch (parsedLine.Count) { case 7: TVideoEntry = new TVideo { videoID = parsedLine[0], name = parsedLine[1], duration = int.Parse(parsedLine[2]), chanel = parsedLine[3], lastPlay = int.Parse(parsedLine[4]), playCount = int.Parse(parsedLine[5]), skipCount = int.Parse(parsedLine[6]) }; this.playlist.Add(TVideoEntry); break; //history playlist case 4: TVideoEntry = new TVideo { videoID = parsedLine[0], name = parsedLine[1], duration = int.Parse(parsedLine[2]), chanel = parsedLine[3] }; this.playlist.Add(TVideoEntry); break; //default playlist case 1: TVideoEntry = new TVideo { videoID = parsedLine[0] }; this.playlist.Add(TVideoEntry); break; //only youtubeVideoId playlist default: MessageBox.Show("Error: Wrong line Format (" + parsedLine.Count.ToString() + " collumns)"); break; //error } } } } } } else { if (ytpFiles == null) { ytpFiles[0] = this.path; } TVideo TVideoEntry = new TVideo(); List <string> parsedLine = new List <string>(); if (ytpFiles.Length == 1) { this.path = ytpFiles[1].ToString(); } foreach (string file in ytpFiles) { using (StreamReader reader = new StreamReader(file)) { while (!reader.EndOfStream) { parsedLine = parceDelimitedString(reader.ReadLine(), '\t'); switch (parsedLine.Count) { case 7: TVideoEntry = new TVideo { videoID = parsedLine[0], name = parsedLine[1], duration = int.Parse(parsedLine[2]), chanel = parsedLine[3], lastPlay = int.Parse(parsedLine[4]), playCount = int.Parse(parsedLine[5]), skipCount = int.Parse(parsedLine[6]) }; this.playlist.Add(TVideoEntry); break; //history playlist case 4: TVideoEntry = new TVideo { videoID = parsedLine[0], name = parsedLine[1], duration = int.Parse(parsedLine[2]), chanel = parsedLine[3] }; this.playlist.Add(TVideoEntry); break; //default playlist case 1: TVideoEntry = new TVideo { videoID = parsedLine[0] }; this.playlist.Add(TVideoEntry); break; //only youtubeVideoId playlist default: MessageBox.Show("Error: Wrong line Format (" + parsedLine.Count.ToString() + " collumns)"); break; //error } } } } } }