コード例 #1
0
        private static void Run()
        {
            IList <SonosSpeaker> speakers = new SonosDiscoveryService().FindSpeakers().Result;

            foreach (SonosSpeaker sonosSpeaker in speakers)
            {
                Console.WriteLine(sonosSpeaker.Name + " (" + sonosSpeaker.Uuid + ")");
            }

            SonosSpeaker speaker = speakers.FirstOrDefault(x => x.Name.StartsWith("J"));

            speaker.Control.OnUpdate += Control_OnUpdate;
            Console.WriteLine("Controlling " + speaker.Name);


            UPnPServer server = new UPnPServer();

            server.Start(24452);

            speaker.Control.SubscribeToEvents(server);

            while (true)
            {
                string line = Console.ReadLine();

                if (line.StartsWith("a"))
                {
                    speaker.Control.Play().Wait();
                }
                else if (line.StartsWith("s"))
                {
                    speaker.Control.Pause().Wait();
                }
                else if (line.StartsWith("n"))
                {
                    speaker.Control.Next().Wait();
                }
                else if (line.StartsWith("v"))
                {
                    speaker.Control.SetVolume(int.Parse(line.Remove(0, 1).Trim())).Wait();
                }
                else if (line.StartsWith("b"))
                {
                    Console.WriteLine(speaker.Control.GetVolume().Result);
                }
                else if (line.StartsWith("u"))
                {
                    AddUriQueueResponse result =
                        speaker.Control.AddURITOQueue(
                            "x-sonos-spotify:spotify%3atrack%3a456lFrF5OrYuCffSHSaYfs?sid=9&amp;flags=8224&amp;sn=1", "", 0, true).Result;
                    Console.WriteLine("TRACK NR: " + result.FirstTrackNumberEnqueued);
                    speaker.Control.Seek(SeekUnitType.TrackNumber, result.FirstTrackNumberEnqueued.ToString()).Wait();
                    speaker.Control.Play().Wait();;
                }
                else if (line.StartsWith("q"))
                {
                    break;
                }
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: JonasMH/UPnPNet
        public static void Main()
        {
            UPnPDiscovery discovery = new UPnPDiscovery
            {
                SearchTarget = DiscoverySearchTargetFactory.ServiceTypeSearch("AVTransport", "1")
            };
            IList <UPnPDevice> devices = discovery.Search().Result;

            UPnPServer server = new UPnPServer();

            IList <UPnPDevice> sonosDevices =
                devices.Where(x => x.Properties["friendlyName"].ToLower().Contains("sonos")).ToList();

            IList <UPnPService> avServices = sonosDevices
                                             .SelectMany(x => x.SubDevices)
                                             .SelectMany(x => x.Services)
                                             .Where(x => x.Type == "urn:schemas-upnp-org:service:AVTransport:1").ToList();

            IList <AvTransportServiceControl> speakers = avServices.Select(x => new AvTransportServiceControl(x)).ToList();


            server.Start(24458);

            speakers.Foreach(x =>
            {
                server.SubscribeToControl(x);
                x.OnLastChangeEvent += (sender, args) => { Console.WriteLine("SOMETHING: " + args.TransportState); };
            });


            while (true)
            {
                ConsoleKeyInfo info = Console.ReadKey();

                switch (info.Key)
                {
                case ConsoleKey.Q:
                    return;

                case ConsoleKey.A:
                    speakers.Foreach(
                        x => x.SendAction("Play", new Dictionary <string, string>()
                    {
                        { "InstanceID", "0" }, { "Speed", "1" }
                    }).Wait());
                    break;

                case ConsoleKey.S:
                    speakers.Foreach(x => x.SendAction("Pause", new Dictionary <string, string>()
                    {
                        { "InstanceID", "0" }
                    }).Wait());
                    break;
                }
            }
        }