LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-22-2021, 10:41 AM   #1
apoorv569
Member
 
Registered: Jun 2020
Distribution: Arch Linux
Posts: 61

Rep: Reputation: 0
Post 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.

Last edited by apoorv569; 01-22-2021 at 10:48 AM.
 
Old 01-24-2021, 08:44 AM   #2
apoorv569
Member
 
Registered: Jun 2020
Distribution: Arch Linux
Posts: 61

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by apoorv569 View Post
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.
 
Old 01-24-2021, 01:45 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,266
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
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!

Last edited by astrogeek; 01-24-2021 at 01:46 PM. Reason: typos
 
  


Reply

Tags
c++, gui, wxwidgets



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Why does /media/cdrecorder pop up with "cd /media/ + tab" when I have /media/usb too? SharpyWarpy Fedora 6 11-22-2012 01:05 AM
Media, media, and oh yeah......media. Morbid SUSE / openSUSE 2 03-11-2007 08:50 PM
undefined reference to `wxMediaCtrl::wxMediaCtrl[in-charge]()' breman Programming 1 08-30-2005 10:54 AM
wxwidgets and audacity slinky2004 Linux - Newbie 9 10-19-2004 02:03 PM
Cant work my wxWidgets prog. on windows sduffy89 Programming 1 08-30-2004 05:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration