Hosted Forums => foobar2000 => Development - (fb2k) => Topic started by: Anopob on 2021-03-04 02:20:31

Title: Beginner playback_state question
Post by: Anopob on 2021-03-04 02:20:31
Hi, I am trying to implement the following feature: When starting to play a song, it starts at a random part in the song. This is similar to this old Preview component https://skipyrich.com/wiki/Foobar2000:Preview (https://skipyrich.com/wiki/Foobar2000:Preview) except I want to be able to accept random values, not just a fixed second/percentage value.

So first of all, I took the foo_sample component and am just modifying it to test things out as I am very new. I tried implementing my feature within a context menu command, which works perfectly.
Code: (myPlaybackFunction) [Select]
playback_control::ptr pbc = playback_control::get();
double songLength = pbc->playback_get_length();
double randomTime = rand() % (int)songLength;

However, I want to be able to do this automatically (eventually add a toggle feature too). So I added this code into playback_state.cpp (CPlaybackStateDemo class). This is the big part which is confusing me. I have tried putting the above code (and some variants) into:
but can't seem to get anything to work. From what I can tell, CPlaybackStateDemo inherits from play_callback_impl_base which registers itself on construction, yet I can't seem to get any calls to do anything. To be honest I have no idea where to go from here. Not sure if I'm missing something simple or if I need a much larger understanding of the foundation for this. I'm new to C++ (mostly a C# background). Any help would be appreciated, thanks.

P.S. I use Visual Studio to compile everything and was wondering if there was a nice way to debug. I've been building and then using the component, testing everything manually.
Title: Re: Beginner playback_state question
Post by: marc2k3 on 2021-03-04 07:18:35
I think that foo_sample code will only run when the dialog is open. You want your own class derived from play_callback_static

Code: [Select]
class MyClass: public play_callback_static
    uint32_t get_flags() override
         // take care to return the flags for the callbacks you intend to use.

   // all other overrides go here


As for debugging, I generally setup a junction from the VS output folder to a folder inside the user-components folder of a portable install. From an administrator command prompt, I might do something like..

Code: [Select]
mklink /D z:\foobar2000\profile\user-components\foo_sample z:\git\foobar2000-sdk\foobar2000\foo_sample\Release

Then inside VS itself, to to the Properties of your project>Debugging>Command and set it to the foobar instance above - in my case z:\foobar2000\foobar2000.exe

Also, make sure the project is set as the "startup project", Right click in solution explorer>set as startup project.
Title: Re: Beginner playback_state question
Post by: Anopob on 2021-03-04 18:44:05
That did it! The debug tip also helped a bunch and I will be working toward my toggle now. Thank you very much!
SimplePortal 1.0.0 RC1 © 2008-2021