playmp3.pp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. program playmp3;
  2. {$mode objfpc}
  3. {$h+}
  4. uses sysutils, glib2, gst;
  5. Var
  6. pipeline : PGstElement = Nil;
  7. filesrc : PGstElement = Nil;
  8. msg: PGstMessage = Nil;
  9. bus : PGstBus = Nil;
  10. msgError : PGError = Nil;
  11. error : PGError = Nil;
  12. dbg : PAnsiChar;
  13. FN : String;
  14. begin
  15. if (ParamCount<>1) then
  16. begin
  17. Writeln ('usage: ',ExtractFileName(ParamSTr(0)),' <filename>');
  18. Halt(1);
  19. end;
  20. gst_init (@argc, @argv);
  21. pipeline:=gst_parse_launch ('filesrc name=my_filesrc ! mpegaudioparse ! mpg123audiodec ! audioconvert ! audioresample ! pulsesink', @error);
  22. if (pipeline=nil) then
  23. begin
  24. Writeln('Parse error: ', error^.message);
  25. Halt(2);
  26. end;
  27. filesrc := gst_bin_get_by_name (GST_BIN (pipeline), 'my_filesrc');
  28. FN:=ParamStr(1);
  29. g_object_set (filesrc, 'location', PAnsiChar(FN), NULL);
  30. g_object_unref (filesrc);
  31. gst_element_set_state (pipeline, GST_STATE_PLAYING);
  32. bus := gst_element_get_bus (pipeline);
  33. {* wait until we either get an EOS or an ERROR message. Note that in a real
  34. * program you would probably not use gst_bus_poll(), but rather set up an
  35. * async signal watch on the bus and run a main loop and connect to the
  36. * bus's signals to catch certain messages or all messages }
  37. msg:=gst_bus_poll (bus, TGstMessageType(Ord(GST_MESSAGE_EOS) or Ord (GST_MESSAGE_ERROR)), -1);
  38. case GST_MESSAGE_TYPE (msg) of
  39. GST_MESSAGE_EOS:
  40. Writeln('EOS');
  41. GST_MESSAGE_ERROR:
  42. begin
  43. gst_message_parse_error (msg, @msgError, @dbg);
  44. if (MsgError<>Nil) then
  45. begin
  46. Writeln('ERROR: ', MsgError^.message);
  47. g_error_free (MsgError);
  48. end;
  49. if (dbg<>Nil) then
  50. begin
  51. writeln('[Debug details: [', dbg,']');
  52. g_free (dbg);
  53. end;
  54. end;
  55. else
  56. Writeln('Unexpected message of type', GST_MESSAGE_TYPE (msg));
  57. end;
  58. gst_message_unref (msg);
  59. gst_element_set_state (pipeline, GST_STATE_NULL);
  60. gst_object_unref (pipeline);
  61. gst_object_unref (bus);
  62. end.