static async Task PlayAsync(SingleStreamSimulator simulator, CancellationToken cancellationToken) { var source = new Uri(Sources[0]); if (source.HasExtension(".pls")) { using (var client = new HttpClient()) using (var response = await client.GetAsync(source, HttpCompletionOption.ResponseContentRead, cancellationToken).ConfigureAwait(false)) { response.EnsureSuccessStatusCode(); using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) using (var reader = new StreamReader(stream)) { var pls = new PlsParser(response.RequestMessage.RequestUri); if (!await pls.ParseAsync(reader).ConfigureAwait(false)) throw new FileNotFoundException("Unable to parse PLS playlist"); var track = pls.Tracks.FirstOrDefault(); if (null == track) throw new FileNotFoundException("Empty PLS playlist"); Uri trackUrl; if (!Uri.TryCreate(pls.BaseUrl, track.File, out trackUrl)) throw new FileNotFoundException("Invalid URL in PLS playlist"); source = trackUrl; } } } await simulator.StartAsync(source, cancellationToken).ConfigureAwait(false); }
static void Main(string[] args) { TaskScheduler.UnobservedTaskException += (sender, eventArgs) => { Console.WriteLine("*** Unobserved task exception {0}", eventArgs.Exception.Message); if (Debugger.IsAttached) Debugger.Break(); }; try { using (var simulator = new SingleStreamSimulator()) { PlayAsync(simulator, CancellationToken.None).Wait(); Console.WriteLine("Press <enter> to exit"); Console.ReadLine(); } } catch (Exception ex) { Console.WriteLine(ex); if (Debugger.IsAttached) Debugger.Break(); } try { TaskCollector.Default.Wait(); } catch (Exception ex) { Console.WriteLine(ex); if (Debugger.IsAttached) Debugger.Break(); } }