コード例 #1
0
        private async void Client_Connected(object sender, EventArgs e)
        {
            try
            {
                //multi-threaded check
                if (InvokeRequired)
                {
                    //invoke form
                    BeginInvoke((MethodInvoker) delegate
                    {
                        Client_Connected(sender, e);
                    });
                }
                else
                {
                    //check if the client actually did connect
                    if (Service.ConnectedChromecast != null)
                    {
                        //setup GUI
                        btnCast.Text = @"Launching";

                        //try and launch Plex on the Chromecast device
                        Controller = await Service.ChromeCastClient.LaunchPlex();

                        //give it some time to launch Plex (4 seconds)
                        await Task.Delay(4000);

                        //if it's still null, wait it out until it isn't anymore.
                        while (Controller == null)
                        {
                            await Task.Delay(500);
                        }

                        //setup GUI
                        btnCast.Text = @"Queueing";

                        //try and load the media
                        //controller and the content.
                        var content = PlexMediaData.DataFromContent(StreamingContent);

                        //recast
                        var data = (CustomData)content.CustomData;

                        //debugging
                        //UIMessages.Info(JsonConvert.SerializeObject(data, Formatting.Indented));

                        //try and create a new playQueue
                        var queue = PlayQueueHandler.NewQueue(StreamingContent, ObjectProvider.Svr);

                        //did the connection request not succeed or was the queue ID empty
                        if (!queue.QueueSuccess || string.IsNullOrWhiteSpace(queue.QueueId))
                        {
                            //alert the user to the problem
                            UIMessages.Warning(
                                $"Couldn't create a new playQueue with media:\n\n" +
                                $"Title: {StreamingContent.StreamInformation.ContentTitle}\n" +
                                $"URI: {StreamingContent.ApiUri}");

                            //stop the application
                            await StopApplication();

                            //exit function
                            return;
                        }

                        //apply new playQueue URI to the container
                        data.containerKey = queue.QueueUri;

                        //UIMessages.Info(data.containerKey);

                        //load the Plex play queue for playback operation
                        await Controller.LoadMedia(content.Url, content.ContentType, null,
                                                   content.StreamType,
                                                   0D,
                                                   data);

                        //set UI
                        btnCast.Enabled      = true;
                        btnCast.Text         = @"Stop";
                        btnDiscover.Enabled  = false;
                        btnPlayPause.Enabled = true;
                        btnPlayPause.Text    = @"Play";

                        //set global flags
                        ConnectState = true;
                        PlayState    = false;
                    }
                    else
                    {
                        //alert the user; client is null which means it failed.
                        UIMessages.Warning(@"Failed to connect; null connection providers.");

                        //set UI
                        btnCast.Enabled = true;
                        btnCast.Text    = @"Cast";
                    }
                }
            }
            catch (Exception ex)
            {
                //log the error
                LoggingHelpers.RecordException(ex.Message, @"CastConnectedError");

                //alert the user
                UIMessages.Error($"Error occurred whilst handling post-connection event:\n\n{ex}");

                //stop application
                await StopApplication();
            }
        }
コード例 #2
0
ファイル: Cast.cs プロジェクト: mitchscobell/PlexDL
        private async void BtnCast_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ConnectState)
                {
                    if (lstDevices.SelectedItem != null && Devices.Count > 0)
                    {
                        //Index check. Makes sure that the index does exist within the array before
                        //trying to access it.
                        if (Devices.Count >= lstDevices.SelectedIndex + 1)
                        {
                            //set UI
                            btnCast.Enabled = false;
                            btnCast.Text    = @"Connecting";

                            //match list index to an actual stored chromecast
                            var i          = lstDevices.SelectedIndex;
                            var chromecast = Devices[i];

                            //attempt the connection
                            await Service.ConnectToChromecast(chromecast);

                            //give it some time to connect (5 seconds)
                            await Task.Delay(5000);

                            //check if the client actually did connect
                            if (Service.ConnectedChromecast != null && Service.ChromeCastClient != null)
                            {
                                btnCast.Text = @"Launching";

                                //set the client to the current service client
                                Client = Service.ChromeCastClient;

                                //try and launch Plex on the Chromecast device
                                Controller = await Client.LaunchPlex();

                                //give it some time to launch Plex (4 seconds)
                                await Task.Delay(4000);

                                //if it's still null, wait it out until it isn't anymore.
                                while (Controller == null)
                                {
                                    await Task.Delay(500);
                                }

                                //set UI
                                btnCast.Text = @"Queueing";

                                //try and load the media
                                //controller and the content.
                                var content = PlexMediaData.DataFromContent(StreamingContent);
                                var data    = (CustomData)content.CustomData;

                                //try and create a new playQueue
                                var queue = PlayQueueHandler.NewQueue(StreamingContent, ObjectProvider.Svr);
                                if (!queue.QueueSuccess || string.IsNullOrEmpty(queue.QueueId))
                                {
                                    UIMessages.Warning(
                                        $"Couldn't create a new playQueue with media:\n\nTitle: {StreamingContent.StreamInformation.ContentTitle}\nURI: {StreamingContent.ApiUri}");

                                    //stop the application
                                    await StopApplication();

                                    //exit function
                                    return;
                                }

                                //apply new playQueue URI to the container
                                data.containerKey = queue.QueueUri;

                                //UIMessages.Info(JsonConvert.SerializeObject(data, Formatting.Indented));

                                await Controller.LoadMedia(content.Url, content.ContentType, null, content.StreamType,
                                                           0D,
                                                           content.CustomData);

                                //set UI
                                btnCast.Enabled      = true;
                                btnCast.Text         = @"Stop";
                                btnDiscover.Enabled  = false;
                                btnPlayPause.Enabled = true;
                                btnPlayPause.Text    = @"Play";

                                //set global flags
                                ConnectState = true;
                                PlayState    = false;
                            }
                            else
                            {
                                //alert the user; client is null which means it failed.
                                UIMessages.Warning(@"Failed to connect; null connection providers.");

                                //set UI
                                btnCast.Enabled = true;
                                btnCast.Text    = @"Cast";
                            }
                        }
                        else
                        {
                            UIMessages.Error(
                                @"Indexing error: the selected index does not align with the current device list.", @"Validation Error");
                        }
                    }
                    else
                    {
                        UIMessages.Warning(
                            @"Please select a device from the list. To populate the device list, please press 'Discover'.");
                    }
                }
                else
                {
                    await StopApplication();
                }
            }
            catch (Exception ex)
            {
                UIMessages.Error($"An error occurred whilst initiating or terminating a connection:\n\n{ex}");
                LoggingHelpers.RecordException(ex.Message, @"CastInitError");

                //stop the application
                await StopApplication();
            }
        }