Ejemplo n.º 1
0
        public MusicPlayer(Channel startingVoiceChannel, float? defaultVolume)
        {
            if (startingVoiceChannel == null)
                throw new ArgumentNullException(nameof(startingVoiceChannel));
            if (startingVoiceChannel.Type != ChannelType.Voice)
                throw new ArgumentException("Channel must be of type voice");
            Volume = defaultVolume ?? 1.0f;

            PlaybackVoiceChannel = startingVoiceChannel;
            SongCancelSource = new CancellationTokenSource();
            cancelToken = SongCancelSource.Token;

            Task.Run(async () =>
            {
                while (!Destroyed)
                {
                    try
                    {
                        if (audioClient?.State != ConnectionState.Connected)
                            audioClient = await PlaybackVoiceChannel.JoinAudio().ConfigureAwait(false);
                    }
                    catch
                    {
                        await Task.Delay(1000).ConfigureAwait(false);
                        continue;
                    }
                    CurrentSong = GetNextSong();
                    var curSong = CurrentSong;
                    if (curSong != null)
                    {
                        try
                        {
                            OnStarted(this, curSong);
                            await curSong.Play(audioClient, cancelToken).ConfigureAwait(false);
                        }
                        catch (OperationCanceledException)
                        {
                            Console.WriteLine("Song canceled");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"Exception in PlaySong: {ex}");
                        }
                        OnCompleted(this, curSong);
                        curSong = CurrentSong; //to check if its null now
                        if (curSong != null)
                            if (RepeatSong)
                                playlist.Insert(0, curSong);
                            else if (RepeatPlaylist)
                                playlist.Insert(playlist.Count, curSong);
                        SongCancelSource = new CancellationTokenSource();
                        cancelToken = SongCancelSource.Token;
                    }
                    await Task.Delay(1000).ConfigureAwait(false);
                }
            });
        }
Ejemplo n.º 2
0
 internal Task MoveToVoiceChannel(Channel voiceChannel)
 {
     if (audioClient?.State != ConnectionState.Connected)
         throw new InvalidOperationException("Can't move while bot is not connected to voice channel.");
     PlaybackVoiceChannel = voiceChannel;
     return PlaybackVoiceChannel.JoinAudio();
 }
Ejemplo n.º 3
0
        public MusicPlayer(Channel startingVoiceChannel, float? defaultVolume)
        {
            if (startingVoiceChannel == null)
                throw new ArgumentNullException(nameof(startingVoiceChannel));
            if (startingVoiceChannel.Type != ChannelType.Voice)
                throw new ArgumentException("Channel must be of type voice");
            Volume = defaultVolume ?? 1.0f;

            PlaybackVoiceChannel = startingVoiceChannel;
            SongCancelSource = new CancellationTokenSource();
            cancelToken = SongCancelSource.Token;

            Task.Run(async () =>
            {
                try
                {
                    while (!Destroyed)
                    {
                        try
                        {
                            Action action;
                            if (actionQueue.TryDequeue(out action))
                            {
                                action();
                            }
                        }
                        finally
                        {
                            await Task.Delay(100).ConfigureAwait(false);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Action queue crashed");
                    Console.WriteLine(ex);
                }
            }).ConfigureAwait(false);

            var t = new Thread(new ThreadStart(async () =>
            {
                try
                {
                    while (!Destroyed)
                    {
                        try
                        {
                            if (audioClient?.State != ConnectionState.Connected)
                            {
                                audioClient = await PlaybackVoiceChannel.JoinAudio();
                                continue;
                            }

                            CurrentSong = GetNextSong();
                            RemoveSongAt(0);

                            if (CurrentSong == null)
                                continue;

                            
                            OnStarted(this, CurrentSong);
                            await CurrentSong.Play(audioClient, cancelToken);

                            OnCompleted(this, CurrentSong);

                            if (RepeatPlaylist)
                                AddSong(CurrentSong, CurrentSong.QueuerName);

                            if (RepeatSong)
                                AddSong(CurrentSong, 0);
                            
                        }
                        finally
                        {
                            if (!cancelToken.IsCancellationRequested)
                            {
                                SongCancelSource.Cancel();
                            }
                            SongCancelSource = new CancellationTokenSource();
                            cancelToken = SongCancelSource.Token;
                            CurrentSong = null;
                            await Task.Delay(300).ConfigureAwait(false);
                        }
                    }
                }
                catch (Exception ex) {
                    Console.WriteLine("Music thread crashed.");
                    Console.WriteLine(ex);
                }
            }));

            t.Start();
        }