After quite some experiments with libVLC, I realized that, although the library is neat in many ways and very easy to use, it works in "asynchronous mode", meaning whatever libVLC command you issue it is actually executed later, asynchronously. This leads to strange problems, and makes the library almost impossible to use. For example, consider the following example:
Code: Select all
#include <iostream>
#include <vlc/vlc.h>
int main(void) {
libvlc_instance_t *vlc_instance;
libvlc_media_t *media;
libvlc_media_player_t *player;
vlc_instance = libvlc_new(0, NULL);
media = libvlc_media_new_path(vlc_instance,"long_sample.ogg");
player = libvlc_media_player_new_from_media(media);
libvlc_media_release(media);
libvlc_media_player_play(player);
std::cout<<"Volume: "<<libvlc_audio_get_volume(player)<<std::endl;
.......... rest of the program ..........
libvlc_media_player_release(player);
libvlc_release(vlc_instance);
return 0;
}
Code: Select all
Volume: -1
Code: Select all
#include <chrono>
#include <thread>
#include <iostream>
#include <vlc/vlc.h>
int main(void) {
libvlc_instance_t *vlc_instance;
libvlc_media_t *media;
libvlc_media_player_t *player;
vlc_instance = libvlc_new(0, NULL);
media = libvlc_media_new_path(vlc_instance,"long_sample.ogg");
player = libvlc_media_player_new_from_media(media);
libvlc_media_release(media);
libvlc_media_player_play(player);
std::this_thread::sleep_for(std::chrono::milliseconds(100)); //Delay.
std::cout<<"Volume: "<<libvlc_audio_get_volume(player)<<std::endl;
.......... rest of the program ..........
libvlc_media_player_release(player);
libvlc_release(vlc_instance);
return 0;
}
Code: Select all
Volume: 100
The problem is not only about libvlc_audio_set_volume(). It is actually about any other libVLC function. For example, modify the above programs so that you call, e.g., libvlc_audio_set_volume(), or libvlc_audio_set_channel(), or whatever affecting the media player object. It does NOT work, unless you set a small delay before issuing the command. This is extremely annoying, and as I said, it makes the library almost impossible to work properly.
I realize developers swiched to asynchronous mode for a reason. However, there must be a way to force libVLC executing commands synchronously, immediately, right at the time, not whenever the library wants to do so. Even if the libVLC command I issue takes some time to be executed, the program must wait for the time needed before proceeding. This way, you could ask for the current media player's volume or whatever else, and you would get precisely what you ask, not erroneous results. Is there any way to do that?