Help - Search - Members - Calendar
Full Version: Locating track index in playlist
Hydrogenaudio Forums > Hosted Forums > foobar2000 > Development - (fb2k)
snae
This may be a simple question, so please forgive me. I am attempting to locate a track in the current playlist, set the selection to that track and then play it. I seem to be having trouble finding out how to get the index of a track when I know it's name. Could anyone provide any pointers here..?

Thanks.
foosion
What do you mean by name? The filename or the value of some tag fields?

For the filename, it would be something like this:
CODE
// tracks are identified by filename and subsong index.
// the subsong index is usually 0 for files that contain only a single track.
const char * filename = "bla.mp3";
int subsong = 0;
// retrieve the unique metadb_handle for the wanted track
metadb_handle * handle = metadb::get()->handle_create(make_playable_location(filename, subsong);
// get pointer to playlist_oper service
playlist_oper * pl = playlist_oper::get();
// search track in playlist
int n, count = pl->get_count();
for (n = 0; n < count; n++)
{
   // metadb_handles can be compared by reference when checking for identity
   metadb_handle * handle2 = pl->get_item(n);
   if (handle == handle2)
   {
        pl->set_focus_sel(n);
        pl->play_item(n);
        n = count;
   }
   // we have to release all handles returned by get_item()
   handle2->handle_release();
}
// release the handle returned by handle_create()
handle->handle_release();

Please note that the above code is written from memory, so it may not compile as-is.
snae
I am going to use the file path if that will work correctly as that is guaranteed to be unique. I had started to make some progress towards it, but this helps a lot. Many Thanks.

Update:
OK, I have got exactly what I wanted working with a modified version of foo_uie_albumlist, where another option has been added to allow for double click/middle click to locate a track in the current playlist, or to locate and play the first track of an album if an album node is selected in the list. Thanks for the help.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.