public void Update()
 {
     if (CurrentSong != null && CurrentSong.IsStopped)
     {
         CurrentSong = SoundBank.GetCue(CurrentSong.Name);
         CurrentSong.Play();
     }
 }
 public void PlaySong(string cueName)
 {
     if (CurrentSong != null)
     {
         CurrentSong.Stop(AudioStopOptions.Immediate);
     }
     CurrentSong = SoundBank.GetCue(cueName);
     CurrentSong.Play();
 }
Beispiel #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() => {
                while (true)
                {
                    try {
                        _client = await PlaybackVoiceChannel.JoinAudio();
                    }
                    catch {
                        await Task.Delay(1000);
                        continue;
                    }
                    CurrentSong = GetNextSong();
                    if (CurrentSong != null)
                    {
                        try {
                            OnStarted(CurrentSong);
                            await CurrentSong.Play(_client, cancelToken);
                        }
                        catch (OperationCanceledException) {
                            Console.WriteLine("Song canceled");
                        }
                        catch (Exception ex) {
                            Console.WriteLine($"Exception in PlaySong: {ex}");
                        }
                        try {
                            OnCompleted(CurrentSong);
                        }
                        catch { }
                        SongCancelSource = new CancellationTokenSource();
                        cancelToken      = SongCancelSource.Token;
                    }
                    await Task.Delay(1000);
                }
            });
        }
Beispiel #4
0
        public MusicPlayer(IVoiceChannel startingVoiceChannel, ITextChannel outputChannel, float?defaultVolume)
        {
            if (startingVoiceChannel == null)
            {
                throw new ArgumentNullException(nameof(startingVoiceChannel));
            }

            OutputTextChannel = outputChannel;
            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() =>
            {
                while (!Destroyed)
                {
                    try
                    {
                        if (audioClient?.ConnectionState != ConnectionState.Connected)
                        {
                            if (audioClient != null)
                            {
                                try { await audioClient.DisconnectAsync().ConfigureAwait(false); } catch { }
                            }
                            audioClient = await PlaybackVoiceChannel.ConnectAsync().ConfigureAwait(false);
                            continue;
                        }

                        CurrentSong = GetNextSong();

                        if (CurrentSong == null)
                        {
                            continue;
                        }

                        var index = playlist.IndexOf(CurrentSong);
                        if (index != -1)
                        {
                            RemoveSongAt(index, true);
                        }

                        OnStarted(this, CurrentSong);
                        try
                        {
                            await CurrentSong.Play(audioClient, cancelToken);
                        }
                        catch (OperationCanceledException)
                        {
                            OnCompleted(this, CurrentSong);
                        }


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

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

            t.Start();
        }
Beispiel #5
0
 private void PlaySong()
 {
     CurrentSong.Open(_songUris[_songIndex]);
     CurrentSong.Play();
     SetIndex();
 }
Beispiel #6
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();
        }