123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.mediasource_windows;
-
- import com.dmdirc.addons.nowplaying.MediaSource;
- import com.dmdirc.addons.nowplaying.MediaSourceState;
-
- /**
- * Uses WindowsMediaSourcePlugin to retrieve now playing info.
- */
- public class DllSource implements MediaSource {
-
- /** Player name */
- private final String playerName;
- /** Use getArtistTitle */
- private final boolean useArtistTitle;
- /** Parent Plugin. */
- final WindowsMediaSourcePlugin parent;
-
- /**
- * Instantiates the media source.
- *
- * @param parent The plugin that owns this DllSource
- * @param playerName Name of Player and DLL
- */
- public DllSource(final WindowsMediaSourcePlugin parent, final String playerName) {
- this(parent, playerName, false);
- }
-
- /**
- * Instantiates the media source.
- *
- * @param parent The plugin that owns this DllSource
- * @param playerName Name of Player and DLL
- * @param useArtistTitle True if getArtistTitle should be parsed rather than using getArtist()
- * and getTitle()
- */
- public DllSource(final WindowsMediaSourcePlugin parent, final String playerName,
- final boolean useArtistTitle) {
- this.playerName = playerName;
- this.useArtistTitle = useArtistTitle;
- this.parent = parent;
- }
-
- @Override
- public String getAppName() {
- return playerName;
- }
-
- /**
- * Get the "goodoutput" from GetMediaInfo for the given command
- *
- * @param command Command to run
- *
- * @return "Good" Output
- */
- private String getOutput(final String command) {
- return parent.getOutput(playerName, command).getGoodOutput();
- }
-
- @Override
- public MediaSourceState getState() {
- final MediaInfoOutput result = parent.getOutput(playerName, "getPlayState");
-
- if (result.getExitCode() == 0) {
- final String output = result.getGoodOutput();
- if (output.equalsIgnoreCase("stopped")) {
- return MediaSourceState.STOPPED;
- } else if (output.equalsIgnoreCase("playing")) {
- return MediaSourceState.PLAYING;
- } else if (output.equalsIgnoreCase("paused")) {
- return MediaSourceState.PAUSED;
- } else {
- return MediaSourceState.NOTKNOWN;
- }
- } else {
- return MediaSourceState.CLOSED;
- }
- }
-
- @Override
- public String getArtist() {
- if (useArtistTitle) {
- return getOutput("getArtistTitle").split("\\s-\\s", 2)[0];
- } else {
- return getOutput("getArtist");
- }
- }
-
- @Override
- public String getTitle() {
- if (useArtistTitle) {
- final String[] bits = getOutput("getArtistTitle").split("\\s-\\s", 2);
- return (bits.length > 1) ? bits[1] : "";
- } else {
- return getOutput("getTitle");
- }
- }
-
- @Override
- public String getAlbum() {
- return getOutput("getAlbum");
- }
-
- /**
- * Get the duration in seconds as a string.
- *
- * @param secondsInput to get duration for
- *
- * @return Duration as a string
- */
- private String duration(final long secondsInput) {
- final StringBuilder result = new StringBuilder();
- final long hours = (secondsInput / 3600);
- final long minutes = (secondsInput / 60 % 60);
- final long seconds = (secondsInput % 60);
-
- if (hours > 0) {
- result.append(hours).append(':');
- }
- result.append(String.format("%0,2d:%0,2d", minutes, seconds));
-
- return result.toString();
- }
-
- @Override
- public String getLength() {
- try {
- final int seconds = Integer.parseInt(getOutput("getLength"));
- return duration(seconds);
- } catch (NumberFormatException nfe) {
- }
- return "Unknown";
- }
-
- @Override
- public String getTime() {
- try {
- final int seconds = Integer.parseInt(getOutput("getTime"));
- return duration(seconds);
- } catch (NumberFormatException nfe) {
- }
- return "Unknown";
- }
-
- @Override
- public String getFormat() {
- return getOutput("getFormat");
- }
-
- @Override
- public String getBitrate() {
- return getOutput("getBitrate");
- }
-
- }
|