public DownloadManager() { this.userManager = ServiceLocator.Usermanager; // Observe if new users will be added this.userManager.Users.CollectionChanged += (o, e) => { // and observe his youtube links var user = (o as ObservableCollection <User>).ElementAt(e.NewStartingIndex) as User; // beim ersten mussen wirs noch selber machen this.DownloadYoutubeLink(user.YoutubeLinks[0], user); user.YoutubeLinks.CollectionChanged += (ob, arg) => { if (arg.NewStartingIndex == -1) { return; } YoutubeFile youtubeFileToDownload = ((ob as ObservableCollection <YoutubeFile>).ElementAt(arg.NewStartingIndex) as YoutubeFile); if (youtubeFileToDownload.Downloaded == true) { return; } this.DownloadYoutubeLink(youtubeFileToDownload, user); }; }; }
private void PlayNextSong() { if (this.IsPaused) { return; } User user = null; YoutubeFile youtubeFile = null; for (int i = 0; i < this.userManager.Users.Count; i++) { user = this.ChooseNextUser(i); youtubeFile = user.YoutubeLinks.FirstOrDefault(); if (youtubeFile != null) { break; } } if (youtubeFile == null || string.IsNullOrEmpty(youtubeFile.Path)) { // there is nothing to play this.isPlaying = false; return; } else { this.isPlaying = true; user.TimePlayed += youtubeFile.Duration; this.CurrentPlayingSong = youtubeFile; Task.Run(() => { //this.soundDevice.Stop(); // not sure about this this.soundDevice.Initialize(CodecFactory.Instance.GetCodec(youtubeFile.Path)); this.soundDevice.Play(); }); var time = DateTime.Now; Debug.WriteLine(time.Hour + ":" + time.Minute + ":" + time.Second + " " + youtubeFile.Name); user.YoutubeLinks.Remove(youtubeFile); } }
public User(string name, YoutubeFile youtubeLink) { this.Name = name; this.YoutubeLinks.Add(youtubeLink); }
private void HandleWaveOutEventPlaybackStopped(object sender, PlaybackStoppedEventArgs e) { this.isPlaying = false; this.CurrentPlayingSong = null; this.PlayNextSong(); }
private void DownloadYoutubeLink(YoutubeFile youtubeFile, User user) { Task.Run(() => { VideoInfo video = null; try { IEnumerable <VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(youtubeFile.Url); video = videoInfos.First(info => info.VideoType == VideoType.Mp4); } catch (Exception e) { Console.WriteLine(e.ToString()); user.YoutubeLinks.Remove(youtubeFile); return; } if (video.RequiresDecryption) { DownloadUrlResolver.DecryptDownloadUrl(video); } if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/Downloaded")) { Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/Downloaded"); } var fileName = NormalizeFileName(video.Title); var videoDownloader = new VideoDownloader(video, Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/Downloaded", fileName + video.AudioExtension)); try { videoDownloader.Execute(); } catch (Exception e) { Console.WriteLine(e.ToString()); user.YoutubeLinks.Remove(youtubeFile); return; } // Convert to mp4 to mp3 // ------------------- var inputFile = new MediaFile { Filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/Downloaded", fileName + video.AudioExtension) }; var outputFile = new MediaFile { Filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/Downloaded", fileName + ".mp3") }; using (var engine = new Engine()) { engine.GetMetadata(inputFile); engine.Convert(inputFile, outputFile); } // ------------------- youtubeFile.Name = video.Title; youtubeFile.Duration = inputFile.Metadata.Duration.TotalSeconds; youtubeFile.Downloaded = true; youtubeFile.Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/Downloaded", fileName + ".mp3"); File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/Downloaded", fileName + video.AudioExtension)); this.OnYoutubeFileDownloadFinish(); }); }