コード例 #1
0
        public async Task DisconnectAsync()
        {
            if (timer != null)
            {
                await timer.DisposeAsync();
            }

            Player.Stop();
            Player.Dispose();
            await audioClient.StopAsync();

            audioClient.Dispose();
        }
コード例 #2
0
        //[Command("come",RunMode =RunMode.Async)]
        //[Summary("Comes joins to the channel")]
        public async Task Come()
        {
            try
            {
                _audio?.Dispose();

                _voiceChannel = Context.Guild.GetVoiceChannelsAsync(CacheMode.AllowDownload).Result.FirstOrDefault(x => x.Name.ToLower() == "Music".ToLower());

                if (_voiceChannel == null)
                {
                    await Context.Channel.SendMessageAsync("Create a Voice Channel with the name Music");
                }
                _textChannel = Context.Guild.GetTextChannelsAsync(CacheMode.AllowDownload).Result.FirstOrDefault(x => x.Name.ToLower() == "Music".ToLower());
                if (_textChannel == null)
                {
                    await Context.Channel.SendMessageAsync("Create a Text Channel with the name Music");
                }

                _audio = await _voiceChannel.ConnectAsync();
            }
            catch (Exception ex)
            {
                CommonTools.ErrorReporting(ex);
            }
        }
コード例 #3
0
        private async Task PlayMusicAsync(IAudioChannel channel, IMessageChannel outputChannel, string filename, CancellationToken cancellationToken)
        {
            IAudioClient audioClient = null;

            try {
                audioClient = await channel.ConnectAsync();

                using (var ffmpeg = CreateConverterProc(filename))
                    using (var output = ffmpeg.StandardOutput.BaseStream)
                        using (var discord = audioClient.CreatePCMStream(AudioApplication.Mixed)) {
                            try {
                                await output.CopyToAsync(discord, cancellationToken);
                            }
                            catch (Exception e) {
                                Logger.Error(e);
                            }
                            finally {
                                ffmpeg?.Kill();
                                await discord.FlushAsync();

                                await channel.DisconnectAsync();
                            }
                        }
            }
            catch (Exception e) {
                Logger.Error(e);
            }
            finally {
                audioClient?.Dispose();
            }
        }
コード例 #4
0
ファイル: DiscordContext.cs プロジェクト: neat8/Uncord
        public async Task DisconnectCurrentVoiceChannel()
        {
            using (var releaser = await _VoiceChannelLock.LockAsync())
            {
                if (_CurrentVoiceAudioClient != null)
                {
                    _CurrentVoiceAudioClient.Dispose();
                    _CurrentVoiceAudioClient = null;
                }

                CurrentVoiceChannel = null;
                ConnectState        = UncordChannelConnectState.NotConnect;
            }
        }
コード例 #5
0
 public void Dispose()
 {
     if (_audioClient != null)
     {
         _audioClient.StopAsync().GetAwaiter().GetResult();
         _audioClient.Dispose();
     }
     _cancellationTokenSource?.Dispose();
     while (!_songQueue.IsEmpty)
     {
         _songQueue.TryDequeue(out _currentSong);
     }
     _audioClient             = null;
     _cancellationTokenSource = null;
     _currentSong             = null;
     _currentSongPath         = null;
 }
コード例 #6
0
 public void Dispose()
 {
     foreach (var v in _soundServices)
     {
         v.Dispose();
     }
     _soundServices = null;
     if (_outStream != null)
     {
         _outStream.Dispose();
     }
     if (_audioClient != null)
     {
         _audioClient.Dispose();
     }
     Logging.Log("VoiceChannelService unloaded");
 }
コード例 #7
0
 public static void Disable()
 {
     playing = false;
     if (youtubedlThread != null)
     {
         youtubedlThread.Abort();
     }
     if (sendingThread != null)
     {
         sendingThread.Abort();
     }
     if (client != null)
     {
         client.StopAsync().Wait();
     }
     if (client != null)
     {
         client.Dispose();
     }
     Enabled = false;
 }
コード例 #8
0
ファイル: MusicService.cs プロジェクト: informaniac/MC-Bot
            public async Task PlayMusic(string MusicPath, IVoiceChannel Chan, bool Change)
            {
                try
                {
                    if (_AudioClient == null)
                    {
                        _AudioClient = await Chan.ConnectAsync();
                    }
                    else
                    {
                        if (_Process != null)
                        {
                            _Process.Close();
                            _Process.Dispose();
                        }
                        if (Change == true)
                        {
                            await _AudioClient.StopAsync();

                            _AudioClient.Dispose();
                            _AudioClient = await Chan.ConnectAsync();
                        }
                    }
                }catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }



                CreateProcess(MusicPath);
                var _Stream      = _Process.StandardOutput.BaseStream;
                var _AudioStream = _AudioClient.CreatePCMStream(AudioApplication.Music, bufferMillis: 500);
                await _Stream.CopyToAsync(_AudioStream);

                await _AudioStream.FlushAsync();
            }
コード例 #9
0
ファイル: StandardListener.cs プロジェクト: F0903/RunBot
 public void Stop()
 {
     cancellationTokenSource?.Cancel();
     inputClient?.StopAsync();
     inputClient?.Dispose();
 }
コード例 #10
0
        private async Task PlayMusic(IVoiceChannel channel, Playlist playlist)
        {
            var token = tokenSource.Token;

            PlayerState  = PlayerState.Idle;
            VoiceChannel = channel;
            Playlist     = playlist;
            playedSongs.Clear();
            var logTag = $"[Music {VoiceChannel.GuildId}] ";

            logger.LogInformation($"{logTag}Starting music player in guild {Guild.Name}, channel {VoiceChannel.Name}");
            ConnectionState = ConnectionState.Connecting;

            IAudioClient   audioClient   = null;
            AudioOutStream discordStream = null;

            try
            {
                audioClient = await VoiceChannel.ConnectAsync();

                discordStream = audioClient.CreateOpusStream();
                logger.LogDebug($"{logTag}Connected");
                ConnectionState = ConnectionState.Connected;

                while (!token.IsCancellationRequested)
                {
                    var songId = playlist.GetNextSong();
                    if (songId == null)
                    {
                        break;
                    }
                    logger.LogDebug($"{logTag}Playing next song (Id: {songId})");
                    var song = await musicService.GetSong(songId);

                    if (song == null)
                    {
                        logger.LogWarning($"{logTag}Failed to get data for song id {songId}");
                        continue;
                    }
                    var playHistoryEntry = new PlayHistoryEntry {
                        song = song, state = SongState.Playing
                    };
                    var oggStream = await song.GetOggStream();

                    if (oggStream == null)
                    {
                        logger.LogWarning($"{logTag}Failed to get ogg stream for current song (Id: {songId})");
                        playHistoryEntry.state = SongState.Error;
                        playedSongs.Add(playHistoryEntry);
                        continue;
                    }
                    playedSongs.Add(playHistoryEntry);
                    try
                    {
                        var opusStream = new OpusOggReadStream(null, oggStream);
                        PlayerState = PlayerState.Playing;
                        while (opusStream.HasNextPacket && !token.IsCancellationRequested)
                        {
                            var packet = opusStream.RetrieveNextPacket();
                            if (packet == null)
                            {
                                break;
                            }
                            await discordStream.WriteAsync(packet, 0, packet.Length);
                        }
                        playHistoryEntry.state             = SongState.Finished;
                        playedSongs[playedSongs.Count - 1] = playHistoryEntry;
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, $"{logTag}Exception while playing, skipping to next track");
                        playHistoryEntry.state             = SongState.Error;
                        playedSongs[playedSongs.Count - 1] = playHistoryEntry;
                    }
                    finally
                    {
                        oggStream.Dispose();
                        await discordStream.FlushAsync();
                    }
                    PlayerState = PlayerState.Idle;
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"{logTag}Exception in music player");
            }
            finally
            {
                logger.LogInformation($"{logTag}Stopping music player");
                VoiceChannel    = null;
                Playlist        = null;
                ConnectionState = ConnectionState.Disconnecting;
                discordStream?.Dispose();
                if (audioClient != null)
                {
                    audioClient.Disconnected -= ClientDisconnected;
                    audioClient.Dispose();
                }
                ConnectionState = ConnectionState.Disconnected;
                PlayerState     = PlayerState.Disconnected;
                logger.LogDebug($"{logTag}Stopped music player");
            }


            Task ClientDisconnected(Exception ex)
            {
                return(Task.Run(() => {
                    if (ex != null)
                    {
                        logger.LogError(ex, "Audio client disconnected with exception");
                    }
                    tokenSource.Cancel();
                }));
            }
        }