Libmpc.Mpc.Status C# (CSharp) Method

Status() public method

Returns the current status of the MPD.
public Status ( ) : MpdStatus
return MpdStatus
        public MpdStatus Status()
        {
            MpdResponse response = this.getConnection().Exec("status");

              if (response.IsError)
            throw new MpdResponseException(response.ErrorCode, response.ErrorMessage);

              int volume = -1;
              bool repeat = false;
              bool random = false;
              int playlist = -1;
              int playlistLength = -1;
              int playlistQueue = -1;
              int xFade = -1;
              MpdState state = MpdState.Unknown;
              int song = -1;
              int songId = -1;
              int timeElapsed = -1;
              int timeTotal = -1;
              int bitrate = -1;
              int audioSampleRate = -1;
              int audioBits = -1;
              int audioChannels = -1;
              int updatingDb = -1;
              string error = null;

              foreach (KeyValuePair<string, string> line in response) {
            if ((line.Key != null) && (line.Value != null))
              switch (line.Key) {
            case "volume": {
                int tryValue;
                if (int.TryParse(line.Value, out tryValue)) {
                  volume = tryValue;
                  if (volume < 0)
                    volume = 0;
                  if (volume > 100)
                    volume = 100;
                }
              }
              break;
            case "repeat":
              repeat = (line.Value != null) && (line.Value.Equals("1"));
              break;
            case "random":
              random = (line.Value != null) && (line.Value.Equals("1"));
              break;
            case "playlist": {
                int tryValue;
                if (int.TryParse(line.Value, out tryValue))
                  playlist = tryValue;
              }
              break;
            case "playlistlength": {
                int tryValue;
                if (int.TryParse(line.Value, out tryValue))
                  playlistLength = tryValue;
              }
              break;
            case "playlistqueue": {
                int tryValue;
                if (int.TryParse(line.Value, out tryValue))
                  playlistQueue = tryValue;
              }
              break;
            case "xfade": {
                int tryValue;
                if (int.TryParse(line.Value, out tryValue))
                  xFade = tryValue;
              }
              break;
            case "state":
              switch (line.Value) {
                case "play":
                  state = MpdState.Play;
                  break;
                case "pause":
                  state = MpdState.Pause;
                  break;
                case "stop":
                  state = MpdState.Stop;
                  break;
              }
              break;
            case "song": {
                int tryValue;
                if (int.TryParse(line.Value, out tryValue))
                  song = tryValue;
              }
              break;
            case "songid": {
                int tryValue;
                if (int.TryParse(line.Value, out tryValue))
                  songId = tryValue;
              }
              break;
            case "time":
              int index = line.Value.IndexOf(':');
              if (index >= 0) {
                int tryValue;
                if (int.TryParse(line.Value.Substring(0, index), out tryValue))
                  timeElapsed = tryValue;
                if (int.TryParse(line.Value.Substring(index + 1), out tryValue))
                  timeTotal = tryValue;
              }
              break;
            case "bitrate": {
                int tryValue;
                if (int.TryParse(line.Value, out tryValue))
                  bitrate = tryValue;
              }
              break;
            case "audio":
              Match match = STATUS_AUDIO_REGEX.Match(line.Value);
              if (match.Success) {
                int tryValue;
                if (int.TryParse(match.Result("$sampleRate"), out tryValue))
                  audioSampleRate = tryValue;
                if (int.TryParse(match.Result("$bits"), out tryValue))
                  audioBits = tryValue;
                if (int.TryParse(match.Result("$channels"), out tryValue))
                  audioChannels = tryValue;
              }
              break;
            case "updating_db": {
                int tryValue;
                if (int.TryParse(line.Value, out tryValue))
                  updatingDb = tryValue;
              }
              break;
            case "error":
              error = line.Value;
              break;
              }
              }

              return new MpdStatus(
              volume,
              repeat,
              random,
              playlist,
              playlistLength,
              xFade,
              state,
              song,
              songId,
              timeElapsed,
              timeTotal,
              bitrate,
              audioSampleRate,
              audioBits,
              audioChannels,
              updatingDb,
              error
              );
        }

Usage Example

コード例 #1
0
ファイル: ModdTunes.cs プロジェクト: MatthewCox/MoronBot
        public override List<IRCResponse> GetResponse(BotMessage message)
        {
            if (Regex.IsMatch(message.Command, "^(m(odd)?t(unes)?)$", RegexOptions.IgnoreCase))
            {
                Mpc mpc = new Mpc();

                var addresses = System.Net.Dns.GetHostAddresses("moddington.net");
                if (addresses.Length > 0)
                {
                    mpc.Connection = new MpcConnection(new IPEndPoint(addresses[0], 6600));

                    var song = mpc.CurrentSong();
                    string songMsg = "";
                    if (song.HasTitle || song.HasArtist)
                    {
                        if (song.HasTitle) songMsg += song.Title;
                        else songMsg += "<Unknown Title>";
                        if (song.HasArtist) songMsg += " - " + song.Artist;
                        else songMsg += "<Unknown Artist>";
                    }
                    else
                    {
                        songMsg += song.File;
                    }

                    var status = mpc.Status();
                    TimeSpan elapsed = TimeSpan.FromSeconds(status.TimeElapsed);
                    TimeSpan total = TimeSpan.FromSeconds(status.TimeTotal);
                    string timeMsg = (elapsed.Hours > 0 ? elapsed.Hours + ":" : "") + elapsed.Minutes + ":" + elapsed.Seconds.ToString("D2") + "/" +
                        (total.Hours > 0 ? total.Hours + ":" : "") + total.Minutes + ":" + total.Seconds.ToString("D2");

                    string output = "Playing: " + songMsg +
                        " [" + timeMsg + "] - Listen here: " + ChannelList.EvadeChannelLinkBlock(message, "http://moddington.net:8000/moddtunes.ogg");

                    return new List<IRCResponse>() { new IRCResponse(ResponseType.Say, output, message.ReplyTo) };
                }
                else
                {
                    return new List<IRCResponse>() { new IRCResponse(ResponseType.Say, "Moddington's internet radio seems to be down", message.ReplyTo) };
                }
            }

            return null;
        }