| 31 | | Apps may be written to output audio by using **system** calls to perform commands such as above. However, it would be better to use gstreamer library calls in the app. An example of this will be added to this page at a later date. |
| | 31 | Apps may be written to output audio by using **system** calls to perform commands such as above. However, it would be better to use gstreamer library calls in the app. |
| | 32 | |
| | 33 | {{{ |
| | 34 | #include <gst/gst.h> |
| | 35 | #include <stdio.h> |
| | 36 | #include <stdlib.h> |
| | 37 | |
| | 38 | int main(int argc, char *argv[]) { |
| | 39 | GstElement *pipeline; |
| | 40 | GstBus *bus; |
| | 41 | GstMessage *msg; |
| | 42 | GError *error = NULL; |
| | 43 | |
| | 44 | // Check command-line arguments |
| | 45 | if (argc != 2) { |
| | 46 | fprintf(stderr, "Usage: %s <MP3 file path>\n", argv[0]); |
| | 47 | return EXIT_FAILURE; |
| | 48 | } |
| | 49 | |
| | 50 | // Initialize GStreamer |
| | 51 | gst_init(&argc, &argv); |
| | 52 | |
| | 53 | // Create the pipeline using playbin (handles decoding automatically) |
| | 54 | char *pipeline_desc; |
| | 55 | pipeline_desc = g_strdup_printf("playbin uri=file://%s", argv[1]); |
| | 56 | pipeline = gst_parse_launch(pipeline_desc, &error); |
| | 57 | g_free(pipeline_desc); |
| | 58 | |
| | 59 | if (!pipeline) { |
| | 60 | fprintf(stderr, "Failed to create pipeline: %s\n", error ? error->message : "Unknown error"); |
| | 61 | if (error) g_error_free(error); |
| | 62 | return EXIT_FAILURE; |
| | 63 | } |
| | 64 | |
| | 65 | // Start playing |
| | 66 | gst_element_set_state(pipeline, GST_STATE_PLAYING); |
| | 67 | |
| | 68 | // Wait until error or EOS (End Of Stream) |
| | 69 | bus = gst_element_get_bus(pipeline); |
| | 70 | msg = gst_bus_timed_pop_filtered( |
| | 71 | bus, |
| | 72 | GST_CLOCK_TIME_NONE, |
| | 73 | GST_MESSAGE_ERROR | GST_MESSAGE_EOS |
| | 74 | ); |
| | 75 | |
| | 76 | // Handle the message |
| | 77 | if (msg != NULL) { |
| | 78 | GError *err; |
| | 79 | gchar *debug_info; |
| | 80 | |
| | 81 | switch (GST_MESSAGE_TYPE(msg)) { |
| | 82 | case GST_MESSAGE_ERROR: |
| | 83 | gst_message_parse_error(msg, &err, &debug_info); |
| | 84 | fprintf(stderr, "Error: %s\n", err->message); |
| | 85 | fprintf(stderr, "Debug info: %s\n", debug_info ? debug_info : "none"); |
| | 86 | g_clear_error(&err); |
| | 87 | g_free(debug_info); |
| | 88 | break; |
| | 89 | case GST_MESSAGE_EOS: |
| | 90 | printf("Playback finished.\n"); |
| | 91 | break; |
| | 92 | default: |
| | 93 | // Should not reach here |
| | 94 | break; |
| | 95 | } |
| | 96 | gst_message_unref(msg); |
| | 97 | } |
| | 98 | |
| | 99 | // Free resources |
| | 100 | gst_object_unref(bus); |
| | 101 | gst_element_set_state(pipeline, GST_STATE_NULL); |
| | 102 | gst_object_unref(pipeline); |
| | 103 | |
| | 104 | return EXIT_SUCCESS; |
| | 105 | } |
| | 106 | }}} |
| | 107 | |
| | 108 | Also refer to the ID Storage app source code for class CGStreamer. This has the ability to queue audio files to play in a background thread. |