Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DllSource.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2006-2014 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. public class DllSource implements MediaSource {
  29. /** Player name */
  30. private final String playerName;
  31. /** Use getArtistTitle */
  32. private final boolean useArtistTitle;
  33. /** Parent Plugin. */
  34. final WindowsMediaSourcePlugin parent;
  35. /**
  36. * Instantiates the media source.
  37. *
  38. * @param parent The plugin that owns this DllSource
  39. * @param playerName Name of Player and DLL
  40. */
  41. public DllSource(final WindowsMediaSourcePlugin parent, final String playerName) {
  42. this(parent, playerName, false);
  43. }
  44. /**
  45. * Instantiates the media source.
  46. *
  47. * @param parent The plugin that owns this DllSource
  48. * @param playerName Name of Player and DLL
  49. * @param useArtistTitle True if getArtistTitle should be parsed rather than using getArtist()
  50. * and getTitle()
  51. */
  52. public DllSource(final WindowsMediaSourcePlugin parent, final String playerName,
  53. final boolean useArtistTitle) {
  54. this.playerName = playerName;
  55. this.useArtistTitle = useArtistTitle;
  56. this.parent = parent;
  57. }
  58. @Override
  59. public String getAppName() {
  60. return playerName;
  61. }
  62. /**
  63. * Get the "goodoutput" from GetMediaInfo for the given command
  64. *
  65. * @param command Command to run
  66. *
  67. * @return "Good" Output
  68. */
  69. private String getOutput(final String command) {
  70. return parent.getOutput(playerName, command).getGoodOutput();
  71. }
  72. @Override
  73. public MediaSourceState getState() {
  74. final MediaInfoOutput result = parent.getOutput(playerName, "getPlayState");
  75. if (result.getExitCode() == 0) {
  76. final String output = result.getGoodOutput();
  77. if (output.equalsIgnoreCase("stopped")) {
  78. return MediaSourceState.STOPPED;
  79. } else if (output.equalsIgnoreCase("playing")) {
  80. return MediaSourceState.PLAYING;
  81. } else if (output.equalsIgnoreCase("paused")) {
  82. return MediaSourceState.PAUSED;
  83. } else {
  84. return MediaSourceState.NOTKNOWN;
  85. }
  86. } else {
  87. return MediaSourceState.CLOSED;
  88. }
  89. }
  90. @Override
  91. public String getArtist() {
  92. if (useArtistTitle) {
  93. return getOutput("getArtistTitle").split("\\s-\\s", 2)[0];
  94. } else {
  95. return getOutput("getArtist");
  96. }
  97. }
  98. @Override
  99. public String getTitle() {
  100. if (useArtistTitle) {
  101. final String[] bits = getOutput("getArtistTitle").split("\\s-\\s", 2);
  102. return (bits.length > 1) ? bits[1] : "";
  103. } else {
  104. return getOutput("getTitle");
  105. }
  106. }
  107. @Override
  108. public String getAlbum() {
  109. return getOutput("getAlbum");
  110. }
  111. /**
  112. * Get the duration in seconds as a string.
  113. *
  114. * @param secondsInput to get duration for
  115. *
  116. * @return Duration as a string
  117. */
  118. private String duration(final long secondsInput) {
  119. final StringBuilder result = new StringBuilder();
  120. final long hours = (secondsInput / 3600);
  121. final long minutes = (secondsInput / 60 % 60);
  122. final long seconds = (secondsInput % 60);
  123. if (hours > 0) {
  124. result.append(hours).append(':');
  125. }
  126. result.append(String.format("%0,2d:%0,2d", minutes, seconds));
  127. return result.toString();
  128. }
  129. @Override
  130. public String getLength() {
  131. try {
  132. final int seconds = Integer.parseInt(getOutput("getLength"));
  133. return duration(seconds);
  134. } catch (NumberFormatException nfe) {
  135. }
  136. return "Unknown";
  137. }
  138. @Override
  139. public String getTime() {
  140. try {
  141. final int seconds = Integer.parseInt(getOutput("getTime"));
  142. return duration(seconds);
  143. } catch (NumberFormatException nfe) {
  144. }
  145. return "Unknown";
  146. }
  147. @Override
  148. public String getFormat() {
  149. return getOutput("getFormat");
  150. }
  151. @Override
  152. public String getBitrate() {
  153. return getOutput("getBitrate");
  154. }
  155. }