public static async Task <ShoutcastStream> ConnectAsync(Uri serverUrl,
                                                                ShoutcastStreamFactoryConnectionSettings settings)
        {
            //http://www.smackfu.com/stuff/programming/shoutcast.html

            ShoutcastStream shoutStream = null;

            ShoutcastStreamFactoryInternalConnectResult result = await ConnectInternalAsync(serverUrl, settings);

            SocketWrapper socketWrapper = SocketWrapperFactory.CreateSocketWrapper(result);

            shoutStream = new ShoutcastStream(serverUrl, settings, socketWrapper);

            string httpLine = result.httpResponse.Substring(0, result.httpResponse.IndexOf('\n')).Trim();

            if (string.IsNullOrWhiteSpace(httpLine))
            {
                throw new InvalidOperationException("httpLine is null or whitespace");
            }

            var action = ParseHttpCodeAndResponse(httpLine, result.httpResponse, shoutStream);

            //todo handle when we get a text/html page.


            if (action != null)
            {
                switch (action.ActionType)
                {
                case ConnectionActionType.Success:
                    var headers = ParseResponse(result.httpResponse, shoutStream);
                    await shoutStream.HandleHeadersAsync(headers);

                    return(shoutStream);

                case ConnectionActionType.Fail:
                    throw action.ActionException;

                case ConnectionActionType.Redirect:
                {
                    //clean up.
                    shoutStream.Dispose();

                    return(await ConnectAsync(action.ActionUrl, settings));
                }

                default:
                    socketWrapper.Dispose();
                    throw new Exception("We weren't able to connect for some reason.");
                }
            }
            else
            {
                socketWrapper.Dispose();
                throw new Exception("We weren't able to connect for some reason.");
            }
        }
Exemplo n.º 2
0
        private async void playButton_Click(object sender, RoutedEventArgs e)
        {
            playButton.IsEnabled      = false;
            stationComboBox.IsEnabled = false;
            stopButton.IsEnabled      = true;

            var selectedStation = stationComboBox.SelectedItem as StationItem;

            if (selectedStation != null)
            {
                try
                {
                    shoutcastStream = await ShoutcastStreamFactory.ConnectAsync(selectedStation.Url,
                                                                                new ShoutcastStreamFactoryConnectionSettings()
                    {
                        RelativePath = selectedStation.RelativePath
                    });

                    shoutcastStream.MetadataChanged += StreamManager_MetadataChanged;
                    MediaPlayer.SetMediaStreamSource(shoutcastStream.MediaStreamSource);
                    MediaPlayer.Play();

                    SampleRateBox.Text  = "Sample Rate: " + shoutcastStream.AudioInfo.SampleRate;
                    BitRateBox.Text     = "Bit Rate: " + shoutcastStream.AudioInfo.BitRate;
                    AudioFormatBox.Text = "Audio Format: " + Enum.GetName(typeof(UWPShoutcastMSS.Streaming.StreamAudioFormat), shoutcastStream.AudioInfo.AudioFormat);
                }
                catch (Exception ex)
                {
                    playButton.IsEnabled      = true;
                    stationComboBox.IsEnabled = true;
                    stopButton.IsEnabled      = false;

                    if (shoutcastStream != null)
                    {
                        try
                        {
                            shoutcastStream.Disconnect();
                        }
                        catch (Exception)
                        {
                        }
                        finally
                        {
                            shoutcastStream.Dispose();
                        }
                    }

                    MessageDialog dialog = new MessageDialog("Unable to connect!");
                    await dialog.ShowAsync();
                }
            }
        }