wxWidgets - wxMediaCtrl, how to loop media.
by apoorv569 from LinuxQuestions.org on (#5D690)
Can someone please explain how looping a media works with wxMediaCtrl. I am fairly new wxWidgets and C++ in general, but not a complete beginner at this point, I'm making a application for browsing and managing audio samples, so far with wxMediaCtrl I'm able to load, play and stop media at will, by simply calling MediaCtrl->Load() or MediaCtrl->Play(), but I can't understand how to loop a media after it finishes playing, reading the documentation and the provided sample from wxWidgets, which is far too long, I do understand that I need to catch wxEVT_MEDIA_FINISHED, and make a function that does when this event is fired. But I can't get it to work. I have a function to play media when a button is pressed like,
Code:void Browser::OnClickPlay(wxCommandEvent& event)
{
wxString selection = SampleListView->GetTextValue(SampleListView->GetSelectedRow(), 1);
wxString sample = db.GetSamplePathByFilename(std::string(selection));
MediaCtrl->Load(sample);
MediaCtrl->Play();
}Where SampleListView is a wxDataViewListCtrl, and db is SQLite3 database from where I'm grabbing the path to the file to load media in, and I have binded all event like,
Code: Bind(wxEVT_BUTTON, &Browser::OnClickPlay, this, BCID_Play);
Bind(wxEVT_TOGGLEBUTTON, &Browser::OnClickLoop, this, BCID_Loop);
Connect(BCID_Loop, wxEVT_MEDIA_FINISHED, (wxObjectEventFunction)(wxEventFunction)(wxMediaEventFunction) &Browser::OnMediaFinished);
Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &Browser::OnClickSampleView, this, BCID_SampleListView);This works, and I have similar function when a user click on SampleListView row it plays depending on the row number and filename, and I have a toggle button for loop that by it self just toggles a bool Looping to true or false, depending on the state of the button,
Code:void Browser::OnClickLoop(wxCommandEvent& event)
{
if (LoopButton->GetValue())
{
Looping = true;
}
else
{
Looping = false;
}
}and the function that deals with wxEVT_MEDIA_FINISHED is,
Code:void Browser::OnMediaFinished(wxMediaEvent& event)
{
if (Looping)
{
if ( !MediaCtrl->Play() )
{
wxLogDebug("Could not loop");
MediaCtrl->Load(db.GetSamplePathByFilename(SampleListView->GetTextValue(SampleListView->GetSelectedRow(), 1).ToStdString()));
MediaCtrl->Play();
}
}
}I cannot get it work or able to understand how to do it, if someone can maybe write a short example for looping media that would be great help.


Code:void Browser::OnClickPlay(wxCommandEvent& event)
{
wxString selection = SampleListView->GetTextValue(SampleListView->GetSelectedRow(), 1);
wxString sample = db.GetSamplePathByFilename(std::string(selection));
MediaCtrl->Load(sample);
MediaCtrl->Play();
}Where SampleListView is a wxDataViewListCtrl, and db is SQLite3 database from where I'm grabbing the path to the file to load media in, and I have binded all event like,
Code: Bind(wxEVT_BUTTON, &Browser::OnClickPlay, this, BCID_Play);
Bind(wxEVT_TOGGLEBUTTON, &Browser::OnClickLoop, this, BCID_Loop);
Connect(BCID_Loop, wxEVT_MEDIA_FINISHED, (wxObjectEventFunction)(wxEventFunction)(wxMediaEventFunction) &Browser::OnMediaFinished);
Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &Browser::OnClickSampleView, this, BCID_SampleListView);This works, and I have similar function when a user click on SampleListView row it plays depending on the row number and filename, and I have a toggle button for loop that by it self just toggles a bool Looping to true or false, depending on the state of the button,
Code:void Browser::OnClickLoop(wxCommandEvent& event)
{
if (LoopButton->GetValue())
{
Looping = true;
}
else
{
Looping = false;
}
}and the function that deals with wxEVT_MEDIA_FINISHED is,
Code:void Browser::OnMediaFinished(wxMediaEvent& event)
{
if (Looping)
{
if ( !MediaCtrl->Play() )
{
wxLogDebug("Could not loop");
MediaCtrl->Load(db.GetSamplePathByFilename(SampleListView->GetTextValue(SampleListView->GetSelectedRow(), 1).ToStdString()));
MediaCtrl->Play();
}
}
}I cannot get it work or able to understand how to do it, if someone can maybe write a short example for looping media that would be great help.