Пример #1
0
        private async void Play()
        {
            string url       = selectedQuality.url;
            string lastindex = string.Empty;
            int    startLine = 9;

            try
            {
                while (isStreamDownloading)
                {
                    string response = await httpClient.GetStringAsync(url);

                    string[] playlist = M3U8.Build(response);
                    if (lastindex == string.Empty)
                    {
                        lastindex = playlist[19]; //?
                    }
                    bool isLastFound = false;
                    for (int i = 0; i < 6; i++)
                    {
                        string chunkName = playlist[startLine + (i * 2)];
                        if (!isLastFound)
                        {
                            if (lastindex != chunkName)
                            {
                                continue;
                            }
                            else
                            {
                                isLastFound = true;
                                continue;
                            }
                        }
                        lastindex = chunkName;
                        string chunkPath = url.ToString().Replace("index-live.m3u8", chunkName).Split('?')[0];
                        byte[] chunk     = await httpClient.GetByteArrayAsync(chunkPath);

                        lock ( chunks )
                        {
                            chunks.Enqueue(new TwitchDownload(chunkName, chunk));
                        }
                    }
                    lastindex = playlist[19];
                    Thread.Sleep(1000);
                }
            }
            catch
            {
                isStreamDownloading = false;
            }

            lock ( chunks )
            {
                chunks.Clear();
            }

            isStreamStopped = true;
        }
Пример #2
0
        private async Task <Dictionary <string, TwitchQuality> > GetQualityPlaylist(string channelName)
        {
            TwitchAccessToken accessToken;

            try
            {
                accessToken = await GetAccessToken(channelName);
            }
            catch
            {
                return(null);
            }

            if (accessToken == null)
            {
                return(null);
            }

            Uri site = new Uri("http://usher.twitch.tv/api/channel/hls/"
                               + channelName.ToLower()
                               + ".m3u8?player=twitchweb&token="
                               + ""
                               + accessToken.token
                               + "&sig="
                               + accessToken.sig
                               + "&allow_audio_only=true&allow_source=true&type=any&p=121222");

            string response;

            try
            {
                response = await httpClient.GetStringAsync(site);
            }
            catch
            {
                return(null);
            }

            string[] qualityPlaylist = M3U8.Build(response);

            return(ParseQualityPlaylist(qualityPlaylist));
        }