LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   wxWidgets - wxMediaCtrl, how to loop media. (https://www.linuxquestions.org/questions/programming-9/wxwidgets-wxmediactrl-how-to-loop-media-4175689063/)

apoorv569 01-22-2021 10:41 AM

wxWidgets - wxMediaCtrl, how to loop media.
 
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.

apoorv569 01-24-2021 08:44 AM

Quote:

Originally Posted by apoorv569 (Post 6210714)
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.

Okay, I was able to fix it, it works now. All of this code above is correct its just I had wrong control ID in the Bind(), I put the ID of Loop button rather than MediaCtrl it self.

astrogeek 01-24-2021 01:45 PM

Thanks for updating us with your fix!

If your issue is solved please use the Thread Tools at top of thread to mark it as solved, and good luck!


All times are GMT -5. The time now is 11:52 PM.