Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2006-2013 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.mediasource_windows;
  23. import com.dmdirc.addons.nowplaying.MediaSource;
  24. import com.dmdirc.addons.nowplaying.MediaSourceState;
  25. /**
  26. * Uses WindowsMediaSourcePlugin to retrieve now playing info.
  27. *
  28. * @author Shane
  29. */
  30. public class DllSource implements MediaSource {
  31. /** Player name */
  32. private final String playerName;
  33. /** Use getArtistTitle */
  34. private final boolean useArtistTitle;
  35. /** Parent Plugin. */
  36. final WindowsMediaSourcePlugin parent;
  37. /**
  38. * Instantiates the media source.
  39. *
  40. * @param parent The plugin that owns this DllSource
  41. * @param playerName Name of Player and DLL
  42. */
  43. public DllSource(final WindowsMediaSourcePlugin parent, final String playerName) {
  44. this(parent, playerName, false);
  45. }
  46. /**
  47. * Instantiates the media source.
  48. *
  49. * @param parent The plugin that owns this DllSource
  50. * @param playerName Name of Player and DLL
  51. * @param useArtistTitle True if getArtistTitle should be parsed rather than
  52. * using getArtist() and getTitle()
  53. */
  54. public DllSource(final WindowsMediaSourcePlugin parent, final String playerName, final boolean useArtistTitle) {
  55. this.playerName = playerName;
  56. this.useArtistTitle = useArtistTitle;
  57. this.parent = parent;
  58. }
  59. /** {@inheritDoc} */
  60. @Override
  61. public String getAppName() {
  62. return playerName;
  63. }
  64. /**
  65. * Get the "goodoutput" from GetMediaInfo for the given command
  66. *
  67. * @param command Command to run
  68. * @return "Good" Output
  69. */
  70. private String getOutput(final String command) {
  71. return parent.getOutput(playerName, command).getGoodOutput();
  72. }
  73. /** {@inheritDoc} */
  74. @Override
  75. public MediaSourceState getState() {
  76. final MediaInfoOutput result = parent.getOutput(playerName, "getPlayState");
  77. if (result.getExitCode() == 0) {
  78. final String output = result.getGoodOutput();
  79. if (output.equalsIgnoreCase("stopped")) {
  80. return MediaSourceState.STOPPED;
  81. } else if (output.equalsIgnoreCase("playing")) {
  82. return MediaSourceState.PLAYING;
  83. } else if (output.equalsIgnoreCase("paused")) {
  84. return MediaSourceState.PAUSED;
  85. } else {
  86. return MediaSourceState.NOTKNOWN;
  87. }
  88. } else {
  89. return MediaSourceState.CLOSED;
  90. }
  91. }
  92. /** {@inheritDoc} */
  93. @Override
  94. public String getArtist() {
  95. if (useArtistTitle) {
  96. return getOutput("getArtistTitle").split("\\s-\\s", 2)[0];
  97. } else {
  98. return getOutput("getArtist");
  99. }
  100. }
  101. /** {@inheritDoc} */
  102. @Override
  103. public String getTitle() {
  104. if (useArtistTitle) {
  105. final String[] bits = getOutput("getArtistTitle").split("\\s-\\s", 2);
  106. return (bits.length > 1) ? bits[1] : "";
  107. } else {
  108. return getOutput("getTitle");
  109. }
  110. }
  111. /** {@inheritDoc} */
  112. @Override
  113. public String getAlbum() {
  114. return getOutput("getAlbum");
  115. }
  116. /**
  117. * Get the duration in seconds as a string.
  118. *
  119. * @param secondsInput to get duration for
  120. * @return Duration as a string
  121. */
  122. private String duration(final long secondsInput) {
  123. final StringBuilder result = new StringBuilder();
  124. final long hours = (secondsInput / 3600);
  125. final long minutes = (secondsInput / 60 % 60);
  126. final long seconds = (secondsInput % 60);
  127. if (hours > 0) {
  128. result.append(hours + ":");
  129. }
  130. result.append(String.format("%0,2d:%0,2d", minutes, seconds));
  131. return result.toString();
  132. }
  133. /** {@inheritDoc} */
  134. @Override
  135. public String getLength() {
  136. try {
  137. final int seconds = Integer.parseInt(getOutput("getLength"));
  138. return duration(seconds);
  139. } catch (NumberFormatException nfe) {
  140. }
  141. return "Unknown";
  142. }
  143. /** {@inheritDoc} */
  144. @Override
  145. public String getTime() {
  146. try {
  147. final int seconds = Integer.parseInt(getOutput("getTime"));
  148. return duration(seconds);
  149. } catch (NumberFormatException nfe) {
  150. }
  151. return "Unknown";
  152. }
  153. /** {@inheritDoc} */
  154. @Override
  155. public String getFormat() {
  156. return getOutput("getFormat");
  157. }
  158. /** {@inheritDoc} */
  159. @Override
  160. public String getBitrate() {
  161. return getOutput("getBitrate");
  162. }
  163. }