Example #1
0
        public async Task RunAsync()
        {
            if (currentSong != null)
            {
                return;
            }
            await GuildHandler.MainHandler.Client.SetGameAsync(null);

            MusicContext music = MusicHandler.GetMusicQueue().GetNextSong();

            if (music == null)
            {
                return;
            }
            //await MusicHandler.JoinVoiceChannel(await Utils.findVoiceChannel((music.AskedBy as Discord.IGuildUser).Guild as Discord.WebSocket.SocketGuild, GuildHandler.MainHandler.GuildConfigHandler(GuildHandler.Guild).getMusic().VoiceChannel));
            currentSong = music;
            while (currentSong.Song.Status == MusicStatus.DOWNLOADING)
            {
                ;
            }
            if (currentSong.Song.Status == MusicStatus.BROKEN)
            {
                currentSong = null;
                await music.Channel.SendMessageAsync($"Something went wrong when downloading **{music.Song.Title}**. Skipping...");
                await RunAsync();

                return;
            }
            await PlayAudioAsync(music);
        }
Example #2
0
 public MusicPlayer(GuildHandler GuildHandler, MusicHandler MusicHandler)
 {
     this.GuildHandler = GuildHandler;
     this.MusicHandler = MusicHandler;
     audioClient       = null;
     currentSong       = null;
     mProcess          = null;
     volume            = 1.0f;
 }
Example #3
0
        public MusicContext GetNextSong()
        {
            MusicContext r = null;

            while (queue.Count != 0 && !queue.TryDequeue(out r))
            {
                ;
            }
            return(r);
        }
Example #4
0
        public async Task EnqueueAsync(MusicContext song)
        {
            MusicDownloader downloader = null;

            if (MusicHandler.ShouldDownload())
            {
                downloader = new MusicDownloader(song);
            }
            queue.Enqueue(song);
            if (downloader != null)
            {
                await downloader.RunAsync();
            }
        }
Example #5
0
 public MusicDownloader(MusicContext music)
 {
     context             = music;
     context.Song.Status = MusicStatus.DOWNLOADING;
 }
Example #6
0
        public async Task PlayAudioAsync(MusicContext m)
        {
            currentSong = m;
            string path = null;

            if (m.Song.Status == MusicStatus.NEW)
            {
                try
                {
                    string url = "https://www.youtube.com/watch?v=" + m.Song.VideoId;
                    IEnumerable <VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(url);
                    VideoInfo video = videoInfos.First(info => info.VideoType == VideoType.Mp4 && info.Resolution == 360);
                    if (video.RequiresDecryption)
                    {
                        DownloadUrlResolver.DecryptDownloadUrl(video);
                    }
                    path = video.DownloadUrl;
                }
                catch (Exception)
                {
                    await m.Channel.SendMessageAsync($"Something went wrong when reading **{m.Song.Title}**. Skipping...");

                    goto End;
                }
            }
            else if (m.Song.Status == MusicStatus.DOWNLOADED)
            {
                path = $"Temp{Path.DirectorySeparatorChar}{m.Song.VideoId}.mp4";
            }
            else
            {
                goto End;
            }
            using (var stream = audioClient.CreatePCMStream(AudioApplication.Music))
            {
                mProcess = Process.Start(new ProcessStartInfo
                {
                    FileName  = "ffmpeg",
                    Arguments = "-loglevel quiet " +
                                $"-i \"{path}\" " +
                                "-f s16le -ar 48000 -ac 2 pipe:1",
                    UseShellExecute        = false,
                    CreateNoWindow         = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = false
                });
                await Task.Delay(2000);

                if (m.AskedBy != null)
                {
                    await m.Channel.SendMessageAsync($"Hey {m.AskedBy.Mention}, your request of **{m.Song.Title}** will play now!");
                }
                await GuildHandler.MainHandler.Client.SetGameAsync(m.Song.Title);

                m.Song.StartTime = DateTime.Now;
                while (true)
                {
                    if (mProcess.HasExited)
                    {
                        break;
                    }
                    int    blockSize = 2880;
                    byte[] buffer    = new byte[blockSize];
                    int    byteCount;
                    byteCount = await mProcess.StandardOutput.BaseStream.ReadAsync(buffer, 0, blockSize);

                    if (byteCount == 0)
                    {
                        break;
                    }
                    await stream.WriteAsync(ScaleVolumeSafeAllocateBuffers(buffer, volume), 0, byteCount);
                }
                await stream.FlushAsync();
            }
End:
            await Task.Delay(500);

            if (File.Exists(path))
            {
                File.Delete(path);
            }
            mProcess    = null;
            currentSong = null;
            await RunAsync();
        }
 public MusicPreemptiveDownload(MusicContext m)
 {
     context = m;
 }
Example #8
0
 public MusicSearch(MusicContext m, string search)
 {
     context     = m;
     this.search = search;
 }