Advertisement
Guest User

Untitled

a guest
Mar 27th, 2014
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.17 KB | None | 0 0
  1. module player;
  2.  
  3. import std.stdio;
  4. import std.utf;
  5.  
  6. import glib.CharacterSet;
  7.  
  8. import gdk.Event;
  9.  
  10. import gtk.FileChooserDialog;
  11. import gtk.Main;
  12. import gtk.MainWindow;
  13. import gtk.Toolbar;
  14. import gtk.ToolButton;
  15. import gtk.VBox;
  16. import gtk.Widget;
  17.  
  18. import gstreamer.gstreamer;
  19. import gstreamer.Element;
  20. import gstreamer.ElementFactory;
  21. import gstreamer.Pipeline;
  22.  
  23. class Player
  24. {
  25.     void openFile(string filename)
  26.     {
  27.         writeln(filename);
  28.  
  29.         pipeline = new Pipeline("audio-player");
  30.  
  31.         source = ElementFactory.make("filesrc", "file-source");
  32.         decoder = ElementFactory.make("mad", "mad");
  33.         conv = ElementFactory.make("audioconvert", "converter");
  34.         sink = ElementFactory.make("autoaudiosink", "auto-output");
  35.  
  36.         source.location(filename);
  37.  
  38.         pipeline.add(source);
  39.         pipeline.add(decoder);
  40.         pipeline.add(conv);
  41.         pipeline.add(sink);
  42.  
  43.         source.link(decoder);
  44.  
  45.         decoder.link(conv);
  46.         conv.link(sink);
  47.  
  48.         pipeline.setState(GstState.PLAYING);
  49.     }
  50.  
  51.     void stop()
  52.     {
  53.         if (pipeline)
  54.             pipeline.setState(GstState.NULL);
  55.     }
  56.  
  57.     Pipeline pipeline;
  58.     Element source, decoder, conv, sink;
  59. }
  60.  
  61. class Application: MainWindow
  62. {
  63.     Player player;
  64.  
  65.     VBox vbox;
  66.     Toolbar toolbar;
  67.     ToolButton openButton;
  68.     ToolButton stopButton;
  69.  
  70.     this()
  71.     {
  72.         super("Player");
  73.         setDefaultSize(320, 240);
  74.  
  75.         addOnDelete(&onExit);
  76.  
  77.         player = new Player();
  78.  
  79.         toolbar = new Toolbar();
  80.  
  81.         openButton = new ToolButton(StockID.OPEN);
  82.         openButton.setIsImportant(true);
  83.         openButton.addOnClicked((ToolButton b)
  84.         {
  85.             open();
  86.         });
  87.  
  88.         stopButton = new ToolButton(StockID.STOP);
  89.         stopButton.setIsImportant(true);
  90.         stopButton.addOnClicked((ToolButton b)
  91.         {
  92.             player.stop();
  93.         });
  94.  
  95.         toolbar.insert(openButton, -1);
  96.         toolbar.insert(stopButton, -1);
  97.  
  98.         vbox = new VBox(false, 0);
  99.         vbox.packStart(toolbar, false, false, 0);
  100.         add(vbox);
  101.  
  102.         showAll();
  103.     }
  104.  
  105.     bool onExit(Event event, Widget widget)
  106.     {
  107.         writeln("Application terminated");
  108.         return false;
  109.     }
  110.  
  111.     string localeToUnicode(string input)
  112.     {
  113.         uint bytesRead, bytesWritten;
  114.         string res = CharacterSet.localeToUtf8(
  115.             input, input.length, bytesRead, bytesWritten);
  116.         validate(res);
  117.         return res;
  118.     }
  119.  
  120.     void open()
  121.     {
  122.         string filename;
  123.         bool goingToOpen = false;
  124.         auto fs = new FileChooserDialog("File Selection", this, FileChooserAction.OPEN,
  125.             ["Open", "Cancel"], [ResponseType.ACCEPT, ResponseType.CANCEL]);
  126.         if (fs.run() == ResponseType.ACCEPT)
  127.         {
  128.             filename = localeToUnicode(fs.getFilename());
  129.             goingToOpen = true;
  130.         }
  131.         fs.destroy();
  132.         if (goingToOpen)
  133.         {
  134.             player.openFile(filename);
  135.         }
  136.     }
  137. }
  138.  
  139. void main(string[] args)
  140. {
  141.     GStreamer.init(args);
  142.     Main.init(args);
  143.     auto app = new Application();
  144.     Main.run();
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement