Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

WindowsMediaSourceManager.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2006-2015 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.MediaSourceManager;
  25. import com.dmdirc.plugins.PluginDomain;
  26. import com.dmdirc.plugins.PluginInfo;
  27. import com.dmdirc.plugins.PluginManager;
  28. import com.dmdirc.plugins.implementations.PluginFilesHelper;
  29. import com.dmdirc.util.io.StreamUtils;
  30. import com.google.common.io.CharStreams;
  31. import java.io.IOException;
  32. import java.io.InputStreamReader;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. import javax.inject.Inject;
  36. /**
  37. * Manages all Windows based media sources.
  38. */
  39. public class WindowsMediaSourceManager implements MediaSourceManager {
  40. /** Media sources. */
  41. private final List<MediaSource> sources;
  42. /** Plugin files helper. */
  43. private final PluginFilesHelper filesHelper;
  44. @Inject
  45. public WindowsMediaSourceManager(
  46. final PluginManager pluginManager,
  47. @PluginDomain(WindowsMediaSourcePlugin.class) final PluginInfo pluginInfo) {
  48. this.filesHelper = new PluginFilesHelper(pluginManager, pluginInfo);
  49. sources = new ArrayList<>();
  50. sources.add(new DllSource(this, "Winamp", true));
  51. sources.add(new DllSource(this, "iTunes", false));
  52. }
  53. public void onLoad() {
  54. // Extract the .dlls and .exe
  55. try {
  56. filesHelper.extractResourcesEndingWith(".dll");
  57. filesHelper.extractResourcesEndingWith(".exe");
  58. } catch (IOException ex) {
  59. throw new IllegalStateException("Unable to extract needed files: " + ex.getMessage(),
  60. ex);
  61. }
  62. }
  63. @Override
  64. public List<MediaSource> getSources() {
  65. return sources;
  66. }
  67. /**
  68. * Get the output from GetMediaInfo.exe for the given player and method
  69. *
  70. * @param player Player to ask about
  71. * @param method Method to call
  72. *
  73. * @return a MediaInfoOutput with the results
  74. */
  75. protected MediaInfoOutput getOutput(final String player, final String method) {
  76. try {
  77. final Process myProcess = Runtime.getRuntime().exec(new String[]{
  78. filesHelper.getFilesDirString() + "GetMediaInfo.exe",
  79. player,
  80. method});
  81. StreamUtils.readStream(myProcess.getErrorStream());
  82. final String data = CharStreams
  83. .toString(new InputStreamReader(myProcess.getInputStream()));
  84. try {
  85. myProcess.waitFor();
  86. } catch (InterruptedException e) {
  87. }
  88. return new MediaInfoOutput(myProcess.exitValue(), data);
  89. } catch (SecurityException | IOException e) {
  90. }
  91. return new MediaInfoOutput(-1, "Error executing GetMediaInfo.exe");
  92. }
  93. }