示例#1
0
 public Song Clone()
 {
     var s = new Song(SongInfo);
     s.MusicPlayer = MusicPlayer;
     s.State = StreamState.Queued;
     return s;
 }
示例#2
0
 public void RemoveSong(Song s)
 {
     if (s == null)
         throw new ArgumentNullException(nameof(s));
     actionQueue.Enqueue(() =>
     {
         playlist.Remove(s);
     });
 }
示例#3
0
 public void AddSong(Song s, int index)
 {
     if (s == null)
         throw new ArgumentNullException(nameof(s));
     actionQueue.Enqueue(() =>
     {
         playlist.Insert(index, s);
     });
 }
示例#4
0
 public void AddSong(Song s, string username)
 {
     if (s == null)
         throw new ArgumentNullException(nameof(s));
     ThrowIfQueueFull();
     actionQueue.Enqueue(() =>
     {
         s.MusicPlayer = this;
         s.QueuerName = username.TrimTo(10);
         playlist.Add(s);
     });
 }
示例#5
0
        public static async Task<Song> ResolveSong(string query, MusicType musicType = MusicType.Normal)
        {
            if (string.IsNullOrWhiteSpace(query))
                throw new ArgumentNullException(nameof(query));

            if (musicType != MusicType.Local && IsRadioLink(query))
            {
                musicType = MusicType.Radio;
                query = await HandleStreamContainers(query).ConfigureAwait(false) ?? query;
            }

            try
            {
                switch (musicType)
                {
                    case MusicType.Local:
                        return new Song(new SongInfo
                        {
                            Uri = "\"" + Path.GetFullPath(query) + "\"",
                            Title = Path.GetFileNameWithoutExtension(query),
                            Provider = "Local File",
                            ProviderType = musicType,
                            Query = query,
                        });
                    case MusicType.Radio:
                        return new Song(new SongInfo
                        {
                            Uri = query,
                            Title = $"{query}",
                            Provider = "Radio Stream",
                            ProviderType = musicType,
                            Query = query
                        })
                        { TotalLength = TimeSpan.MaxValue };
                }
                if (SoundCloud.Default.IsSoundCloudLink(query))
                {
                    var svideo = await SoundCloud.Default.ResolveVideoAsync(query).ConfigureAwait(false);
                    return new Song(new SongInfo
                    {
                        Title = svideo.FullName,
                        Provider = "SoundCloud",
                        Uri = svideo.StreamLink,
                        ProviderType = musicType,
                        Query = svideo.TrackLink,
                    })
                    { TotalLength = TimeSpan.FromMilliseconds(svideo.Duration) };
                }

                if (musicType == MusicType.Soundcloud)
                {
                    var svideo = await SoundCloud.Default.GetVideoByQueryAsync(query).ConfigureAwait(false);
                    return new Song(new SongInfo
                    {
                        Title = svideo.FullName,
                        Provider = "SoundCloud",
                        Uri = svideo.StreamLink,
                        ProviderType = MusicType.Normal,
                        Query = svideo.TrackLink,
                    })
                    { TotalLength = TimeSpan.FromMilliseconds(svideo.Duration) };
                }

                var link = (await NadekoBot.Google.GetVideosByKeywordsAsync(query).ConfigureAwait(false)).FirstOrDefault();
                if (string.IsNullOrWhiteSpace(link))
                    throw new OperationCanceledException("Not a valid youtube query.");
                var allVideos = await Task.Run(async () => { try { return await YouTube.Default.GetAllVideosAsync(link).ConfigureAwait(false); } catch { return Enumerable.Empty<YouTubeVideo>(); } }).ConfigureAwait(false);
                var videos = allVideos.Where(v => v.AdaptiveKind == AdaptiveKind.Audio);
                var video = videos
                    .Where(v => v.AudioBitrate < 256)
                    .OrderByDescending(v => v.AudioBitrate)
                    .FirstOrDefault();

                if (video == null) // do something with this error
                    throw new Exception("Could not load any video elements based on the query.");
                var m = Regex.Match(query, @"\?t=(?<t>\d*)");
                int gotoTime = 0;
                if (m.Captures.Count > 0)
                    int.TryParse(m.Groups["t"].ToString(), out gotoTime);
                var song = new Song(new SongInfo
                {
                    Title = video.Title.Substring(0, video.Title.Length - 10), // removing trailing "- You Tube"
                    Provider = "YouTube",
                    Uri = video.Uri,
                    Query = link,
                    ProviderType = musicType,
                });
                song.SkipTo = gotoTime;
                return song;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed resolving the link.{ex.Message}");
                return null;
            }
        }
 public void RemoveSong(Song s)
 {
     if (s == null)
         throw new ArgumentNullException(nameof(s));
     lock (playlistLock)
     {
         playlist.Remove(s);
     }
 }
 public void AddSong(Song s, int index)
 {
     if (s == null)
         throw new ArgumentNullException(nameof(s));
     lock (playlistLock)
     {
         playlist.Insert(index, s);
     }
 }
示例#8
0
文件: Song.cs 项目: Ryonez/Lucy
        public static async Task<Song> ResolveSong(string query, MusicType musicType = MusicType.Normal)
        {
            if (string.IsNullOrWhiteSpace(query))
                throw new ArgumentNullException(nameof(query));

            if (musicType != MusicType.Local && IsRadioLink(query))
            {
                musicType = MusicType.Radio;
                query = await HandleStreamContainers(query).ConfigureAwait(false) ?? query;
            }

            try
            {
                switch (musicType)
                {
                    case MusicType.Local:
                        return new Song(new SongInfo
                        {
                            Uri = "\"" + Path.GetFullPath(query) + "\"",
                            Title = Path.GetFileNameWithoutExtension(query),
                            Provider = "Local File",
                            ProviderType = musicType,
                            Query = query,
                        });
                    case MusicType.Radio:
                        return new Song(new SongInfo
                        {
                            Uri = query,
                            Title = $"{query}",
                            Provider = "Radio Stream",
                            ProviderType = musicType,
                            Query = query
                        });
                }
                if (SoundCloud.Default.IsSoundCloudLink(query))
                {
                    var svideo = await SoundCloud.Default.ResolveVideoAsync(query).ConfigureAwait(false);
                    return new Song(new SongInfo
                    {
                        Title = svideo.FullName,
                        Provider = "SoundCloud",
                        Uri = svideo.StreamLink,
                        ProviderType = musicType,
                        Query = svideo.TrackLink,
                    });
                }

                if (musicType == MusicType.Soundcloud)
                {
                    var svideo = await SoundCloud.Default.GetVideoByQueryAsync(query).ConfigureAwait(false);
                    return new Song(new SongInfo
                    {
                        Title = svideo.FullName,
                        Provider = "SoundCloud",
                        Uri = svideo.StreamLink,
                        ProviderType = MusicType.Normal,
                        Query = svideo.TrackLink,
                    });
                }

                var link = await SearchHelper.FindYoutubeUrlByKeywords(query).ConfigureAwait(false);
                if (string.IsNullOrWhiteSpace(link))
                    throw new OperationCanceledException("Not a valid youtube query.");
                var allVideos = await Task.Factory.StartNew(async () => await YouTube.Default.GetAllVideosAsync(link).ConfigureAwait(false)).Unwrap().ConfigureAwait(false);
                var videos = allVideos.Where(v => v.AdaptiveKind == AdaptiveKind.Audio);
                var video = videos
                    .Where(v => v.AudioBitrate < 192)
                    .OrderByDescending(v => v.AudioBitrate)
                    .FirstOrDefault();

                if (video == null) // do something with this error
                    throw new Exception("Could not load any video elements based on the query.");

                var m = Regex.Match(query, @"\?t=((?<h>\d*)h)?((?<m>\d*)m)?((?<s>\d*)s?)?");
                int gotoTime = 0;
                if (m.Captures.Count > 0)
                {
                    int hours;
                    int minutes;
                    int seconds;

                    int.TryParse(m.Groups["h"].ToString(), out hours);
                    int.TryParse(m.Groups["m"].ToString(), out minutes);
                    int.TryParse(m.Groups["s"].ToString(), out seconds);

                    gotoTime = hours * 60 * 60 + minutes * 60 + seconds;
                }

                var song = new Song(new SongInfo
                {
                    Title = video.Title.Substring(0, video.Title.Length - 10), // removing trailing "- You Tube"
                    Provider = "YouTube",
                    Uri = video.Uri,
                    Query = link,
                    ProviderType = musicType,
                });
                song.SkipTo = gotoTime;
                return song;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed resolving the link.{ex.Message}");
                return null;
            }
        }
示例#9
0
        public static async Task <Song> ResolveSong(string query, MusicType musicType = MusicType.Normal)
        {
            if (string.IsNullOrWhiteSpace(query))
            {
                throw new ArgumentNullException(nameof(query));
            }

            if (musicType != MusicType.Local && IsRadioLink(query))
            {
                musicType = MusicType.Radio;
                query     = await HandleStreamContainers(query).ConfigureAwait(false) ?? query;
            }

            try
            {
                switch (musicType)
                {
                case MusicType.Local:
                    return(new Song(new SongInfo
                    {
                        Uri = "\"" + Path.GetFullPath(query) + "\"",
                        Title = Path.GetFileNameWithoutExtension(query),
                        Provider = "Local File",
                        ProviderType = musicType,
                        Query = query,
                    }));

                case MusicType.Radio:
                    return(new Song(new SongInfo
                    {
                        Uri = query,
                        Title = $"{query}",
                        Provider = "Radio Stream",
                        ProviderType = musicType,
                        Query = query
                    }));
                }
                if (SoundCloud.Default.IsSoundCloudLink(query))
                {
                    var svideo = await SoundCloud.Default.GetVideoAsync(query).ConfigureAwait(false);

                    return(new Song(new SongInfo
                    {
                        Title = svideo.FullName,
                        Provider = "SoundCloud",
                        Uri = svideo.StreamLink,
                        ProviderType = musicType,
                        Query = query,
                    }));
                }
                var link = await SearchHelper.FindYoutubeUrlByKeywords(query).ConfigureAwait(false);

                if (link == String.Empty)
                {
                    throw new OperationCanceledException("Not a valid youtube query.");
                }
                var allVideos = await Task.Factory.StartNew(async() => await YouTube.Default.GetAllVideosAsync(link).ConfigureAwait(false)).Unwrap().ConfigureAwait(false);

                var videos = allVideos.Where(v => v.AdaptiveKind == AdaptiveKind.Audio);
                var video  = videos
                             .Where(v => v.AudioBitrate < 192)
                             .OrderByDescending(v => v.AudioBitrate)
                             .FirstOrDefault();

                if (video == null) // do something with this error
                {
                    throw new Exception("Could not load any video elements based on the query.");
                }
                var m        = Regex.Match(query, @"\?t=(?<t>\d*)");
                int gotoTime = 0;
                if (m.Captures.Count > 0)
                {
                    int.TryParse(m.Groups["t"].ToString(), out gotoTime);
                }
                var song = new Song(new SongInfo
                {
                    Title        = video.Title.Substring(0, video.Title.Length - 10), // removing trailing "- You Tube"
                    Provider     = "YouTube",
                    Uri          = video.Uri,
                    Query        = link,
                    ProviderType = musicType,
                });
                song.SkipTo = gotoTime;
                return(song);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed resolving the link.{ex.Message}");
                return(null);
            }
        }