Last active
December 13, 2025 02:45
-
-
Save su8/14a0beddf5b0e1c64a6a4c71dac5f3f9 to your computer and use it in GitHub Desktop.
vlc3.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| #include <thread> | |
| #include <atomic> | |
| #include <chrono> | |
| #include <csignal> | |
| #include <vlc/vlc.h> // Requires libVLC development headers | |
| std::atomic<bool> running(true); | |
| // Event callback for metadata changes | |
| static void handle_event(const libvlc_event_t* event, void* user_data) { | |
| if (event->type == libvlc_MediaMetaChanged) { | |
| libvlc_media_t* media = static_cast<libvlc_media_t*>(user_data); | |
| const char* title = libvlc_media_get_meta(media, libvlc_meta_Title); | |
| if (title) { | |
| std::cout << "[Title Update] " << title << std::endl; | |
| } | |
| } | |
| } | |
| // Thread function to run VLC playback | |
| void vlc_playback_thread(const std::string& url) { | |
| const char* const vlc_args[] = { | |
| "--quiet", | |
| "--no-xlib" | |
| }; | |
| libvlc_instance_t* vlcInstance = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args); | |
| if (!vlcInstance) { | |
| std::cerr << "Failed to initialize libVLC." << std::endl; | |
| return; | |
| } | |
| libvlc_media_t* media = libvlc_media_new_location(vlcInstance, url.c_str()); | |
| if (!media) { | |
| std::cerr << "Failed to create media object." << std::endl; | |
| libvlc_release(vlcInstance); | |
| return; | |
| } | |
| libvlc_media_player_t* player = libvlc_media_player_new_from_media(media); | |
| if (!player) { | |
| std::cerr << "Failed to create media player." << std::endl; | |
| libvlc_media_release(media); | |
| libvlc_release(vlcInstance); | |
| return; | |
| } | |
| // Attach event listener for metadata changes | |
| libvlc_event_manager_t* eventManager = libvlc_media_event_manager(media); | |
| libvlc_event_attach(eventManager, libvlc_MediaMetaChanged, handle_event, media); | |
| // Start playback | |
| if (libvlc_media_player_play(player) != 0) { | |
| std::cerr << "Failed to start playback." << std::endl; | |
| libvlc_media_player_release(player); | |
| libvlc_media_release(media); | |
| libvlc_release(vlcInstance); | |
| return; | |
| } | |
| // Keep thread alive while running | |
| while (running) { | |
| libvlc_media_parse_with_options(media, libvlc_media_parse_network, 0); | |
| std::this_thread::sleep_for(std::chrono::milliseconds(500)); | |
| } | |
| // Cleanup | |
| libvlc_media_player_stop(player); | |
| libvlc_media_player_release(player); | |
| libvlc_media_release(media); | |
| libvlc_release(vlcInstance); | |
| } | |
| // Signal handler for Ctrl+C | |
| void signal_handler(int) { | |
| running = false; | |
| } | |
| int main() { | |
| std::signal(SIGINT, signal_handler); | |
| std::string streamUrl = "http://stream.live.vc.bbcmedia.co.uk/bbc_radio_one"; | |
| std::thread playerThread(vlc_playback_thread, streamUrl); | |
| std::cout << "Streaming in background. Press Ctrl+C to stop.\n"; | |
| // Main thread can do other work here | |
| while (running) { | |
| std::this_thread::sleep_for(std::chrono::seconds(1)); | |
| } | |
| playerThread.join(); | |
| std::cout << "Stopped streaming.\n"; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment