Beispiel #1
0
        private async Task PlayNext(ulong id, IVoiceState state)
        {
            var guild = Program.GetGuild(id);
            var songs = guild.Songs;

            if (guild.IsPlaying || songs.Count == 0)
            {
                return;
            }
            //guild.Save();
            guild.IsPlaying = true;
            var video = songs.Dequeue();

            Program.GetGuild(id).UsersVoteSkipped.Clear();

            await guild.JoinAudio(Program.Client.GetGuild(id), state.VoiceChannel);

            _cancelToken = new CancellationTokenSource();
            _cancelToken.Token.ThrowIfCancellationRequested();

            guild.CurrentPlaying = video;
            Program.SetGame(video.Video.Title);

            var youtube   = new YoutubeClient();
            var streamObj = await youtube.GetVideoMediaStreamInfosAsync(video.Video.Id);

            var            streamurl  = streamObj.Audio.First().Url;
            AudioOutStream lastStream = null;

            if (guild.AudioChannel != null)
            {
                try
                {
                    var embed = Embeds.GetNowPlaying(video, guild, true);
                    await guild.MessageChannel.SendMessageAsync(string.Empty, embed : embed);

                    using (var ffmpeg = CreateStream(streamurl))
                        using (var stream = guild.AudioChannel.CreatePCMStream(AudioApplication.Music, 48000))
                        {
                            try
                            {
                                lastStream          = stream;
                                guild.SongStartedAt = DateTime.Now;
                                Program.Log($"Now Playing: {video.Video.Title} - {video.Video.Duration.ToString()} - [{video.AuthorUsername}]");
                                await ffmpeg.StandardOutput.BaseStream.CopyToAsync(stream, 81920, _cancelToken.Token);
                            }
                            catch (AggregateException e)
                            {
                            }
                            catch (OperationCanceledException e)
                            {
                            }
                            catch (Exception e)
                            {
                                throw e;
                            }
                            finally
                            {
                                CleanUpBeforeNext(stream, id, state);
                            }
                        }
                }
                catch (YoutubeExplode.Exceptions.VideoUnavailableException e)
                {
                    Program.GetGuild(id).IsPlaying = false;
                    CleanUpBeforeNext(lastStream, id, state);
                    return;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }