当前位置: 首页>>代码示例>>C#>>正文


C# Channel.JoinAudio方法代码示例

本文整理汇总了C#中Discord.Channel.JoinAudio方法的典型用法代码示例。如果您正苦于以下问题:C# Channel.JoinAudio方法的具体用法?C# Channel.JoinAudio怎么用?C# Channel.JoinAudio使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Discord.Channel的用法示例。


在下文中一共展示了Channel.JoinAudio方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MusicPlayer

        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);
                }
            });
        }
开发者ID:ZirconiumHacker,项目名称:NadekoBot,代码行数:57,代码来源:MusicControls.cs

示例2: MoveToVoiceChannel

 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();
 }
开发者ID:ZirconiumHacker,项目名称:NadekoBot,代码行数:7,代码来源:MusicControls.cs

示例3: MusicPlayer

        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();
        }
开发者ID:Ryonez,项目名称:Lucy,代码行数:93,代码来源:MusicControls.cs


注:本文中的Discord.Channel.JoinAudio方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。