Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using AudioSwitcher.AudioApi.CoreAudio;
- namespace AC
- {
- class Program
- {
- static void Main(string[] args)
- {
- CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice;
- int recv;
- byte[] data = new byte[1024];
- IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 6789);
- Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- newsock.Bind(ipep);
- newsock.Listen(10);
- Console.WriteLine("Waiting for a client...");
- Socket client = newsock.Accept();
- IPEndPoint clientep =
- (IPEndPoint)client.RemoteEndPoint;
- Console.WriteLine("Connected with {0} at port {1}",
- clientep.Address, clientep.Port);
- while(true)
- {
- data = new byte[1024];
- recv = client.Receive(data);
- if (recv == 0)
- break;
- var response = Encoding.ASCII.GetString(data, 0, recv);
- if (response.Equals("currentVolume"))
- {
- Console.WriteLine("Current volume: " + defaultPlaybackDevice.Volume);
- var vol = Encoding.ASCII.GetBytes(defaultPlaybackDevice.Volume.ToString());
- client.Send(vol, vol.Length, SocketFlags.None);
- }
- else
- {
- // TODO: Siryi, make this values configurable (also network properties: host, port, etc.)
- const int minVolume = 0;
- const int maxVolume = 100;
- int newVolume = 0;
- if (Int32.TryParse(response, out newVolume) && minVolume <= newVolume && maxVolume >= newVolume)
- {
- Console.WriteLine("Volume to set: " + response);
- defaultPlaybackDevice.Volume = newVolume;
- }
- else
- {
- Console.Write(String.Format("Invalid response received: {0}. ", response));
- Console.WriteLine(String.Format("New volume should be a valid integer between {0} and {1}", minVolume, maxVolume));
- }
- }
- }
- Console.WriteLine("Disconnected from {0}",
- clientep.Address);
- client.Close();
- newsock.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement